Feat/prometheus metrics by elizandropacheco · Pull Request #1947 · EvolutionAPI/evolution-api · GitHub
Skip to content

Feat/prometheus metrics#1947

Merged
DavidsonGomes merged 4 commits intoEvolutionAPI:developfrom
elizandropacheco:feat/prometheus-metrics
Sep 17, 2025
Merged

Feat/prometheus metrics#1947
DavidsonGomes merged 4 commits intoEvolutionAPI:developfrom
elizandropacheco:feat/prometheus-metrics

Conversation

@elizandropacheco
Copy link
Copy Markdown
Contributor

@elizandropacheco elizandropacheco commented Sep 16, 2025

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

  • Added a new GET /metrics route in src/api/routes/index.router.ts, conditionally registered when process.env.PROMETHEUS_METRICS === 'true'.
  • The endpoint outputs Prometheus 0.0.4 text format (text/plain) with a minimal, stable metric set:
    • evolution_environment_info{version,clientName,serverUrl} 1 — environment metadata
    • evolution_instances_total — number of instances currently tracked
    • evolution_instance_up{instance,integration} — 1 if state == "open", else 0
    • evolution_instance_state{instance,integration,state} — labeled gauge with the current state
  • The route uses waMonitor.waInstances (no auth required) so Prometheus can scrape it by default.

Why

  • Provide first-party observability without sidecars.
  • Enable dashboards/alerts around instance states (e.g., detect disconnections).
  • Expose environment metadata for troubleshooting and inventory.

How to use

  1. Set:
PROMETHEUS_METRICS=true
  1. Start the server and scrape:
GET /metrics
Content-Type: text/plain; version=0.0.4; charset=utf-8
  1. Create/connect instances to see:
  • evolution_instances_total increase
  • evolution_instance_up flip to 1 when an instance state is open

Example output

# HELP evolution_environment_info Environment information
# TYPE evolution_environment_info gauge
evolution_environment_info{version="2.3.2",clientName="evolution_exchange",serverUrl="http://localhost:8080"} 1
# HELP evolution_instances_total Total number of instances
# TYPE evolution_instances_total gauge
evolution_instances_total 3
# HELP evolution_instance_up 1 if instance state is open, else 0
# TYPE evolution_instance_up gauge
# HELP evolution_instance_state Instance state as a labelled metric
# TYPE evolution_instance_state gauge
evolution_instance_up{instance="demo-1",integration="WHATSAPP-BAILEYS"} 0
evolution_instance_state{instance="demo-1",integration="WHATSAPP-BAILEYS",state="close"} 1

Notes

  • Auth is intentionally not required for Prometheus scraping. A follow-up PR can add optional guards or an allowlist.
  • Metric set is intentionally small and stable to avoid cardinality spikes; can be extended later.

Files changed

  • src/api/routes/index.router.ts

Checklist

  • Conditional registration via PROMETHEUS_METRICS
  • Prometheus 0.0.4 text output
  • Instance state reflected per instance/integration labels
  • README/CHANGELOG mention (if maintainers prefer)
  • Optional tests (enabled/disabled paths)

Summary 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:

  • Expose a Prometheus-compatible /metrics endpoint gated by the PROMETHEUS_METRICS environment flag
  • Emit environment metadata, total instances, and per-instance up/state gauges in Prometheus text format

Build:

  • Introduce Dockerfile.metrics and update docker-compose to use the evolution/api:metrics image

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Sep 16, 2025

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a 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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/api/routes/index.router.ts Outdated
@DavidsonGomes
Copy link
Copy Markdown
Collaborator

Please, fix the lint with npm run lint

@elizandropacheco
Copy link
Copy Markdown
Contributor Author

@DavidsonGomes DavidsonGomes merged commit e75dae3 into EvolutionAPI:develop Sep 17, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants