Skip to content

fix(monitoring): dedupe container metrics by task suffix, not name prefix#4843

Open
sensorialsn wants to merge 1 commit into
Dokploy:canaryfrom
sensorialsn:fix/monitoring-container-metrics-dedup
Open

fix(monitoring): dedupe container metrics by task suffix, not name prefix#4843
sensorialsn wants to merge 1 commit into
Dokploy:canaryfrom
sensorialsn:fix/monitoring-container-metrics-dedup

Conversation

@sensorialsn

Copy link
Copy Markdown

Problem

In the monitoring service, collectMetrics deduplicates the docker stats output by GetServiceName, which strips the last --separated segment of the container name:

parts := strings.Split(name, "-")
return strings.Join(parts[:len(parts)-1], "-")

Any set of containers sharing a dash prefix therefore collapses to a single deduplication key. With explicit compose container_names such as:

app-1425-app, app-1425-mysql, app-1425-redis, app-1425-queue

all four map to the key app-1425, so only the first container returned by docker stats is stored on each tick — the metrics of every sibling container are silently dropped, and which container "wins" depends on docker stats output order.

This is inconsistent with the storage and query layers, which are already keyed by the full container name:

  • SaveContainerMetric stores metric.Name (full name) in container_metrics.container_name;
  • GetLastNContainerMetrics filters with WHERE container_name = ? OR container_name LIKE ?||'.%'.

So the write-side dedup is the only place losing data.

Fix

Deduplicate by the swarm task prefix (the name up to the first .) instead of the last dash segment:

  • swarm replicas myapp.1.taskid / myapp.2.taskid still collapse to myapp — the original intent (one stored series per service and tick) is preserved, and reads keep matching through the existing LIKE name||'.%' clause;
  • compose containers (app-1425-mysql, project-web-1), which have no task suffix and are unique per container, keep distinct keys and are all stored.

Added a table-driven unit test for GetServiceName covering swarm replicas, explicit compose names, compose default naming, and the leading-slash case.

How to reproduce

  1. Enable container monitoring with services whose container names share a dash prefix (e.g. a compose stack with container_name: mystack-app, mystack-mysql, mystack-redis).
  2. Let the agent collect a few ticks.
  3. Query GET /metrics/containers?appName=mystack-mysql&limit=50 — empty (or nearly empty) results, while mystack-app (or whichever container docker stats lists first) has all the points.

🤖 Generated with Claude Code

…efix

collectMetrics dropped every sibling of the first container sharing a
dash prefix (app-x-mysql, app-x-redis... all mapped to "app-x"), while
storage and queries are keyed by full container_name. Dedupe on the
swarm task suffix instead: replicas of a service still collapse to one
stored series per tick, distinct compose containers all get stored.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sensorialsn
sensorialsn requested a review from Siumauricio as a code owner July 17, 2026 15:48
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Jul 17, 2026
AminDhouib added a commit to DevinoSolutions/dokploy-community that referenced this pull request Jul 18, 2026
port: fix(monitoring): dedupe container metrics by task suffix, not name prefix (upstream Dokploy#4843)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant