Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

remove stale opentelemetry_sdk packages #151

Merged
merged 8 commits into from
Jul 30, 2024
27 changes: 26 additions & 1 deletion lib/charms/tempo_k8s/v1/charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ def my_tracing_endpoint(self) -> Optional[str]:
import inspect
import logging
import os
import shutil
from contextlib import contextmanager
from contextvars import Context, ContextVar, copy_context
from importlib.metadata import distributions
from pathlib import Path
from typing import (
Any,
Expand Down Expand Up @@ -217,7 +219,7 @@ def my_tracing_endpoint(self) -> Optional[str]:
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version

LIBPATCH = 11
LIBPATCH = 12

PYDEPS = ["opentelemetry-exporter-otlp-proto-http==1.21.0"]

Expand Down Expand Up @@ -359,6 +361,28 @@ def _get_server_cert(
return server_cert


def _remove_stale_otel_sdk_packages():
"""Hack to remove stale opentelemetry sdk packages from the environment.
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved

See https://github.com/canonical/grafana-agent-operator/issues/146 and
https://bugs.launchpad.net/juju/+bug/2058335 for more context. This patch can be removed after
this juju issue is resolved and sufficient time has passed to expect most users of this library
have migrated to the patched version of juju.

This only does something if executed on an upgrade-charm event.
"""
if os.getenv("JUJU_DISPATCH_PATH") == "hooks/upgrade-charm":
# Find any opentelemetry_sdk distributions
otel_sdk_distributions = list(distributions(name="opentelemetry_sdk"))
# If there are more than 2, inspect them and infer that any with 0 entrypoints are stale
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
if len(otel_sdk_distributions) > 1:
for d in otel_sdk_distributions:
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
if len(d.entry_points) == 0:
# Distribution appears to be empty. Remove it
logger.debug(f"Removing empty opentelemetry_sdk distribution at: {d._path}") # type: ignore
shutil.rmtree(d._path) # type: ignore


def _setup_root_span_initializer(
charm_type: _CharmType,
tracing_endpoint_attr: str,
Expand Down Expand Up @@ -391,6 +415,7 @@ def wrap_init(self: CharmBase, framework: Framework, *args, **kwargs):
_service_name = service_name or f"{self.app.name}-charm"

unit_name = self.unit.name
_remove_stale_otel_sdk_packages()
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
resource = Resource.create(
attributes={
"service.name": _service_name,
Expand Down
Loading