Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workload tracing for grafana #317

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()]
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
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