Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitoring and analytics #29

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions activity-tracking/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions activity-tracking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dotenv": "^16.3.1",
"express": "^4.19.2",
"moment": "^2.30.1",
"prom-client": "^15.0.0",
"mongoose": "^7.5.4",
"web-vitals": "^2.1.0"
},
Expand Down
20 changes: 19 additions & 1 deletion activity-tracking/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const config = require('./config.json');
const promClient = require('prom-client');
require('dotenv').config();

const app = express();
Expand All @@ -17,7 +18,7 @@ app.use(express.json());

// MongoDB connection
mongoose
.connect(mongoUri, { useNewUrlParser: true })
.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB database connection established successfully"))
.catch((error) => console.error("MongoDB connection error:", error));

Expand All @@ -28,6 +29,23 @@ connection.on('error', (error) => {
console.error("MongoDB connection error:", error);
});

// Create a registry to register the metrics
const register = new promClient.Registry();

// Enable the collection of default metrics
promClient.collectDefaultMetrics({ register });

// Add a route for metrics endpoint
app.get('/metrics', async (req, res) => {
try {
// Retrieve metrics from registry
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (error) {
res.status(500).end(error);
}
});

// Routes
const exercisesRouter = require('./routes/exercises');
app.use('/exercises', exercisesRouter);
Expand Down
6 changes: 6 additions & 0 deletions analytics/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from core.views import stats_page
from core.models import mongo
from flask_cors import CORS
from prometheus_flask_exporter import PrometheusMetrics


def create_app(config):
Expand All @@ -11,6 +12,11 @@ def create_app(config):

mongo.init_app(app)

if not app.testing:
# collect metrics when not in testing
metrics = PrometheusMetrics(app)
metrics.info('app_info', 'Application Info', version='1.2')

CORS(app, resources={r"/*": {"origins": "*"}},
methods="GET,HEAD,POST,OPTIONS,PUT,PATCH,DELETE")
app.register_blueprint(stats_page)
Expand Down
3 changes: 2 additions & 1 deletion analytics/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ python-dotenv==0.19.0
Flask-PyMongo==2.3.0
Flask-CORS==3.0.10
ariadne==0.22
graphql-core==3.2.3
graphql-core==3.2.3
prometheus-flask-exporter
4 changes: 4 additions & 0 deletions authservice/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
// Micrometer Prometheus registry for exposing metrics
implementation("io.micrometer:micrometer-registry-prometheus")
// Spring Boot Actuator for metrics endpoint
implementation ("org.springframework.boot:spring-boot-starter-actuator")
}

tasks.withType<Test> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected void configure(HttpSecurity http) throws Exception {
.cors().and() // Enable CORS (configure this based on your requirements)
.csrf().disable() // Disable CSRF (enable and configure this in production)
.authorizeRequests()
.antMatchers("/api/auth/signup", "/api/auth/login").permitAll() // Public access to signup and login
.antMatchers("/api/auth/signup", "/api/auth/login", "/actuator/prometheus").permitAll() // Public access to signup and login
.anyRequest().authenticated() // All other requests need authentication
.and()
.httpBasic();
Expand Down
3 changes: 2 additions & 1 deletion authservice/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
logging.level.org.springframework=DEBUG
logging.level.com.authservice=DEBUG
spring.data.mongodb.database=authservice
spring.data.mongodb.uri=mongodb://root:cfgmla23@localhost:27017
spring.data.mongodb.uri=mongodb://root:cfgmla23@localhost:27017
management.endpoints.web.exposure.include=prometheus
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,33 @@ services:
networks:
- app-network

prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus:/etc/prometheus
ports:
- "9090:9090"
networks:
- app-network

grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
volumes:
- grafana_data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=cfgmla23
- GF_USERS_ALLOW_SIGN_UP=false
depends_on:
- prometheus
networks:
- app-network

networks:
app-network:
driver: bridge

volumes:
mongodbdata:
grafana_data:
20 changes: 20 additions & 0 deletions prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
global:
scrape_interval: 15s

scrape_configs:
- job_name: "frontend"
static_configs:
- targets: ["frontend:80"]

- job_name: "activity-tracking"
static_configs:
- targets: ["activity-tracking:5300"]

- job_name: "analytics"
static_configs:
- targets: ["analytics:5050"]

- job_name: "authservice"
static_configs:
- targets: ["authservice:8080"]
metrics_path: "/actuator/prometheus"
Loading