diff --git a/src/charm.py b/src/charm.py index 1433a75c..1ac52d78 100755 --- a/src/charm.py +++ b/src/charm.py @@ -193,6 +193,8 @@ def __init__(self, *args): self.cert_handler.on.cert_changed, # pyright: ignore ], ) + + # todo when we bump to v2, add otlp_http for charm_tracing and otlp_grpc for the workload. self.tracing = TracingEndpointRequirer(self) # -- standard events @@ -752,7 +754,7 @@ def _generate_grafana_config(self) -> str: can be set in ENV variables, but leave for expansion later so we can hide auth secrets """ - configs = [] + configs = [self._generate_tracing_config()] if self.has_db: configs.append(self._generate_database_config()) else: @@ -766,7 +768,38 @@ def _generate_grafana_config(self) -> str: data.seek(0) configs.append(data.read()) - return "\n".join(configs) + return "\n".join(filter(bool, configs)) + + def _generate_tracing_config(self) -> str: + """Generate tracing configuration. + + Returns: + A string containing the required tracing information to be stubbed into the config + file. + """ + tracing = self.tracing + if not tracing.is_ready(): + return "" + endpoint = tracing.otlp_grpc_endpoint() + if endpoint is None: + return "" + + config_ini = configparser.ConfigParser() + config_ini["tracing.opentelemetry"] = { + "sampler_type": "probabilistic", + "sampler_param": "0.01", + } + # ref: https://github.com/grafana/grafana/blob/main/conf/defaults.ini#L1505 + config_ini["tracing.opentelemetry.otlp"] = { + "address": endpoint, + } + + # This is silly, but a ConfigParser() handles this nicer than + # raw string manipulation + data = StringIO() + config_ini.write(data) + ret = data.getvalue() + return ret def _generate_database_config(self) -> str: """Generate a database configuration. @@ -798,13 +831,10 @@ def _generate_database_config(self) -> str: "url": db_url, } - # This is silly, but a ConfigParser() handles this nicer than - # raw string manipulation + # This is still silly data = StringIO() config_ini.write(data) - data.seek(0) - ret = data.read() - data.close() + ret = data.getvalue() return ret #####################################