-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Feat/prometheus metrics #1947
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
Feat/prometheus metrics #1947
Conversation
Reviewer's GuideThis PR adds a conditional Prometheus-compatible /metrics endpoint that streams environment and instance-level gauges in text format and updates the Docker setup to build and deploy a metrics‐enabled image. Sequence diagram for conditional /metrics endpoint registration and responsesequenceDiagram
participant Env as "process.env"
participant Router as "Express Router"
participant Prometheus as "Prometheus Scraper"
participant Server as "API Server"
participant waMonitor as "waMonitor"
Env->>Router: PROMETHEUS_METRICS === 'true'
Router->>Server: Register GET /metrics route
Prometheus->>Server: GET /metrics
Server->>waMonitor: Read waInstances
Server->>Prometheus: Return metrics text (environment info, instances, states)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider using a Prometheus client library (e.g., prom-client) instead of manually assembling metric strings for better maintainability and safety.
- Move the metrics formatting and escapeLabel logic into a separate module or service so the router remains focused on request handling.
- Pull static metrics definitions (HELP/TYPE lines and escapeLabel helper) out of the handler to avoid recreating them on every scrape.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using a Prometheus client library (e.g., prom-client) instead of manually assembling metric strings for better maintainability and safety.
- Move the metrics formatting and escapeLabel logic into a separate module or service so the router remains focused on request handling.
- Pull static metrics definitions (HELP/TYPE lines and escapeLabel helper) out of the handler to avoid recreating them on every scrape.
## Individual Comments
### Comment 1
<location> `src/api/routes/index.router.ts:60` </location>
<code_context>
+
+ const lines: string[] = [];
+
+ const clientName = process.env.DATABASE_CONNECTION_CLIENT_NAME || '';
+ const serverUrl = serverConfig.URL || '';
+
</code_context>
<issue_to_address>
**suggestion:** Defaulting to empty string for clientName may obscure missing configuration.
Using an empty string may hide configuration problems in metrics. A sentinel value like 'unknown' would make missing configuration clearer in Prometheus.
```suggestion
const clientName = process.env.DATABASE_CONNECTION_CLIENT_NAME || 'unknown';
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Please, fix the lint with |
|
Fixed |
Title
feat: add Prometheus-compatible /metrics endpoint (gated by PROMETHEUS_METRICS)
Description
This PR introduces a Prometheus-compatible metrics endpoint to expose runtime and instance-level insights for Evolution API. The endpoint is disabled by default and can be enabled via an environment flag.
What’s changed
src/api/routes/index.router.ts, conditionally registered whenprocess.env.PROMETHEUS_METRICS === 'true'.text/plain) with a minimal, stable metric set:evolution_environment_info{version,clientName,serverUrl} 1— environment metadataevolution_instances_total— number of instances currently trackedevolution_instance_up{instance,integration}— 1 ifstate == "open", else 0evolution_instance_state{instance,integration,state}— labeled gauge with the current statewaMonitor.waInstances(no auth required) so Prometheus can scrape it by default.Why
How to use
evolution_instances_totalincreaseevolution_instance_upflip to 1 when an instance state isopenExample output
Notes
Files changed
src/api/routes/index.router.tsChecklist
PROMETHEUS_METRICSSummary by Sourcery
Add an optional Prometheus metrics endpoint for environment and instance-level insights, and adjust the deployment setup with a dedicated Dockerfile and updated docker-compose configuration.
New Features:
Build: