Skip to content

feat: Metrics endpoint#179

Open
va4st wants to merge 5 commits intosleep3r:mainfrom
va4st:metrics_endpoint
Open

feat: Metrics endpoint#179
va4st wants to merge 5 commits intosleep3r:mainfrom
va4st:metrics_endpoint

Conversation

@va4st
Copy link
Copy Markdown
Contributor

@va4st va4st commented Apr 13, 2026

Summary

Add embedded Prometheus monitoring to mtproto-proxy and provide a ready-to-use Docker monitoring stack with Prometheus + Grafana.

related #169

This PR adds:

  • version module. all version consts moved into separated constant module to maintain it via one parameter
  • an in-process /metrics endpoint on a dedicated configurable port
  • global proxy traffic counters
  • optional cgroup RAM metrics for container environments
  • per-user traffic and active connection metrics
  • Grafana dashboard JSON for import
  • Docker Compose example and monitoring docs

What Changed

Embedded Prometheus endpoint

  • Add [monitoring] config section:
    • enabled
    • host
    • port
  • Start a dedicated HTTP listener inside mtproto-proxy
  • Expose GET /metrics in Prometheus text format

Global proxy metrics

  • Active connections
  • Handshakes in flight
  • Accepted / closed totals
  • Drop counters
  • Pause state gauges
  • Global traffic counters:
    • mtproto_client_to_upstream_bytes_total
    • mtproto_upstream_to_client_bytes_total

Memory metrics

  • Keep process metrics:
    • process_resident_memory_bytes
    • process_virtual_memory_bytes
    • process_cpu_seconds_total
    • process_open_fds
    • process_max_fds
  • Add optional cgroup memory metrics when available:
    • mtproto_cgroup_memory_usage_bytes
    • mtproto_cgroup_memory_limit_bytes

This works both on bare metal and in containers:

  • bare metal: process_* metrics still work
  • containers: cgroup memory metrics appear when exposed by the runtime

Per-user metrics

Pre-create per-user metric records from configured users and expose:

  • mtproto_user_connections_active{user="..."}
  • mtproto_user_client_to_upstream_bytes_total{user="..."}
  • mtproto_user_upstream_to_client_bytes_total{user="..."}

Per-user byte counters are updated on the actual write path, so they match the semantics of global traffic totals.

Dashboard and docs

  • Add Grafana dashboard JSON with:
    • memory usage
    • total throughput
    • total transferred
    • connection stats
    • traffic throughput
    • per-user active connections
    • top users by throughput
    • top users by transferred volume
  • Add hack/docker example with:
    • docker-compose.yml
    • prometheus.yml
    • dashboard JSON
    • README walkthrough
  • Link the Docker monitoring guide from the main README

Why

This makes monitoring usable in the common Docker deployment model without relying on the existing Python dashboard stack.

Key goals:

  • one proxy process exports its own metrics
  • no log parsing for metrics
  • no extra exporter sidecar required
  • works in both Docker and raw system deployments
  • supports operator visibility by configured user

Testing

Verified with:

  • zig build test
  • zig build
  • dashboard JSON validation via jq empty
  • compose file validation via docker compose config
  • deployment on staging with real load and usage

Notes

  • Counter totals are process-lifetime counters and reset on proxy restart, which is expected for Prometheus counters.
  • cgroup memory metrics are best-effort and only appear when the relevant cgroup files are available.
  • Per-user metrics are based on users from the current loaded config and update after proxy restart when config changes.

@va4st va4st force-pushed the metrics_endpoint branch from 9b762be to fa401e1 Compare April 13, 2026 18:03
@va4st
Copy link
Copy Markdown
Contributor Author

va4st commented Apr 13, 2026

@sleep3r Нужно ревью, пожалуйста)

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.

1 participant