Skip to content

Commit 134c10c

Browse files
authored
fix: set labels in grafana (#343)
1 parent 7d8d0bf commit 134c10c

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/charm.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import re
88
import time
9-
from typing import Dict, List, Optional, Set
9+
from typing import Any, Dict, List, Optional, Set
1010

1111
import jinja2
1212
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
@@ -126,8 +126,14 @@ def __init__(self, *args):
126126
self.tls = MongoDBTLS(self, Config.Relations.PEERS, Config.SUBSTRATE)
127127
self.backups = MongoDBBackups(self)
128128

129+
self.status = MongoDBStatusHandler(self)
130+
self.secrets = SecretCache(self)
131+
132+
self.shard = ConfigServerRequirer(self)
133+
self.config_server = ShardingProvider(self)
134+
self.cluster = ClusterProvider(self)
129135
self.metrics_endpoint = MetricsEndpointProvider(
130-
self, refresh_event=self.on.start, jobs=Config.Monitoring.JOBS
136+
self, refresh_event=[self.on.start, self.on.update_status], jobs=self.monitoring_jobs
131137
)
132138
self.grafana_dashboards = GrafanaDashboardProvider(self)
133139
self.loki_push = LogProxyConsumer(
@@ -136,12 +142,6 @@ def __init__(self, *args):
136142
relation_name=Config.Relations.LOGGING,
137143
container_name=Config.CONTAINER_NAME,
138144
)
139-
self.status = MongoDBStatusHandler(self)
140-
self.secrets = SecretCache(self)
141-
142-
self.shard = ConfigServerRequirer(self)
143-
self.config_server = ShardingProvider(self)
144-
self.cluster = ClusterProvider(self)
145145

146146
self.version_checker = CrossAppVersionChecker(
147147
self,
@@ -154,6 +154,20 @@ def __init__(self, *args):
154154

155155
# BEGIN: properties
156156

157+
@property
158+
def monitoring_jobs(self) -> list[dict[str, Any]]:
159+
"""Defines the labels and targets for metrics."""
160+
return [
161+
{
162+
"static_configs": [
163+
{
164+
"targets": [f"*:{Config.Monitoring.MONGODB_EXPORTER_PORT}"],
165+
"labels": {"cluster": self.get_config_server_name() or self.app.name},
166+
}
167+
]
168+
}
169+
]
170+
157171
@property
158172
def app_hosts(self) -> List[str]:
159173
"""Retrieve IP addresses associated with MongoDB application.

src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Monitoring:
7373
LOG_SLOTS = ["charmed-mongodb:logs"]
7474
URI_PARAM_NAME = "monitor-uri"
7575
SERVICE_NAME = "mongodb-exporter"
76-
JOBS = [{"static_configs": [{"targets": [f"*:{MONGODB_EXPORTER_PORT}"]}]}]
76+
JOBS = [{"static_configs": [{"targets": [f"*:{MONGODB_EXPORTER_PORT}"], "labels": []}]}]
7777
APP_SCOPE = "app"
7878
UNIT_SCOPE = "unit"
7979

0 commit comments

Comments
 (0)