Skip to content

Commit

Permalink
workload tracing for grafana (#317)
Browse files Browse the repository at this point in the history
* workload tracing for grafana

* static fix

* getvalue
  • Loading branch information
PietroPasotti committed Apr 8, 2024
1 parent 6caa776 commit b1af4e6
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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

#####################################
Expand Down

0 comments on commit b1af4e6

Please sign in to comment.