Skip to content

metrics #12

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,31 @@ services:
container_name: opentelemetry_jaeger_redis
ports:
- '16686:16686'
- '4318:4318'

otel-collector:
image: otel/opentelemetry-collector-contrib:latest
command: ["--config=/etc/otel-collector.yaml"]
volumes:
- ./otel-collector.yaml:/etc/otel-collector.yaml
ports:
- "9464:9464"
- "4318:4318"
- "4317:4317"

prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yaml
command: ["--config.file=/etc/prometheus/prometheus.yaml"]
ports:
- "9090:9090"

grafana:
image: grafana/grafana-enterprise
container_name: grafana
restart: unless-stopped
ports:
- '3001:3000'
environment:
GF_SECURITY_ADMIN_USER: "${GRAFANA_USER}"
GF_SECURITY_ADMIN_PASSWORD: "${GRAFANA_PASSWORD}"
37 changes: 37 additions & 0 deletions otel-collector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
prometheus:
config:
scrape_configs:
- job_name: otel-collector
metrics_path: '/api/newsletter/metrics'
scrape_interval: 10s
static_configs:
- targets:
- host.docker.internal:3000

processors:
batch:

exporters:
otlp:
endpoint: jaeger:4317
tls:
insecure: true

prometheus:
endpoint: 0.0.0.0:9464

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp]
metrics:
receivers: [otlp, prometheus]
processors: [batch]
exporters: [prometheus]
10 changes: 5 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@opentelemetry/sdk-metrics": "^1.27.0",
"@opentelemetry/sdk-node": "^0.54.0",
"@opentelemetry/sdk-trace-node": "^1.27.0",
"bullmq": "^5.21.2",
"bullmq": "^5.40.0",
"bullmq-otel": "^1.0.1",
"dotenv": "^16.4.5",
"express": "^4.19.2",
Expand Down
7 changes: 7 additions & 0 deletions prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global:
scrape_interval: 10s

scrape_configs:
- job_name: 'otel-collector'
static_configs:
- targets: ['otel-collector:9464']
13 changes: 13 additions & 0 deletions src/controllers/newsletter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class NewsletterController {
constructor() {
this.subscribeToNewsletter = this.subscribeToNewsletter.bind(this);
this.unsubcribeFromNewsletter = this.unsubcribeFromNewsletter.bind(this);
this.exportMetrics = this.exportMetrics.bind(this);
}

async subscribeToNewsletter(req: Request, res: Response, next: NextFunction) {
Expand Down Expand Up @@ -32,6 +33,18 @@ class NewsletterController {
return next(err);
}
}

async exportMetrics(_: Request, res: Response, next: NextFunction) {
try {
const newsletterMetrics = await NewsletterService.exportMetrics();
return res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': Buffer.byteLength(newsletterMetrics)}
).end(newsletterMetrics);
} catch(err) {
return next(err);
}
}
}

export default new NewsletterController();
2 changes: 2 additions & 0 deletions src/routes/newsletter.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ router.post('/subscribe', newsletterController.subscribeToNewsletter);

router.post('/unsubscribe', newsletterController.unsubcribeFromNewsletter);

router.get('/metrics', newsletterController.exportMetrics);

export default router;
4 changes: 4 additions & 0 deletions src/services/newsletter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class NewsletterService {
this.subscribedUserCRUD = SubscribedUserCrud;
}

exportMetrics() {
return this.queue.exportPrometheusMetrics();
}

async subscribeToNewsletter(email: string) {
const subscribedUser = await this.subscribedUserCRUD.create(email);
if (!subscribedUser) {
Expand Down