diff --git a/lib/charms/tempo_k8s/v1/charm_tracing.py b/lib/charms/tempo_k8s/v1/charm_tracing.py index fa926539..2dbdddd6 100644 --- a/lib/charms/tempo_k8s/v1/charm_tracing.py +++ b/lib/charms/tempo_k8s/v1/charm_tracing.py @@ -224,7 +224,6 @@ def _remove_stale_otel_sdk_packages(): _remove_stale_otel_sdk_packages() - import functools import inspect import logging @@ -271,7 +270,7 @@ def _remove_stale_otel_sdk_packages(): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 14 +LIBPATCH = 15 PYDEPS = ["opentelemetry-exporter-otlp-proto-http==1.21.0"] @@ -281,7 +280,6 @@ def _remove_stale_otel_sdk_packages(): # set this to 0 if you are debugging/developing this library source dev_logger.setLevel(logging.CRITICAL) - _CharmType = Type[CharmBase] # the type CharmBase and any subclass thereof _C = TypeVar("_C", bound=_CharmType) _T = TypeVar("_T", bound=type) @@ -333,9 +331,22 @@ def _get_tracer() -> Optional[Tracer]: try: return tracer.get() except LookupError: + # fallback: this course-corrects for a user error where charm_tracing symbols are imported + # from different paths (typically charms.tempo_k8s... and lib.charms.tempo_k8s...) try: ctx: Context = copy_context() if context_tracer := _get_tracer_from_context(ctx): + logger.warning( + "Tracer not found in `tracer` context var. " + "Verify that you're importing all `charm_tracing` symbols from the same module path. \n" + "For example, DO" + ": `from charms.lib...charm_tracing import foo, bar`. \n" + "DONT: \n" + " \t - `from charms.lib...charm_tracing import foo` \n" + " \t - `from lib...charm_tracing import bar` \n" + "For more info: https://python-notes.curiousefficiency.org/en/latest/python" + "_concepts/import_traps.html#the-double-import-trap" + ) return context_tracer.get() else: return None diff --git a/src/alertmanager.py b/src/alertmanager.py index 6f54f7fc..a0e37f3f 100644 --- a/src/alertmanager.py +++ b/src/alertmanager.py @@ -9,7 +9,6 @@ import re from typing import Callable, Dict, List, Optional, Tuple -from alertmanager_client import Alertmanager, AlertmanagerBadResponse from ops.framework import Object from ops.model import Container from ops.pebble import ( # type: ignore @@ -18,6 +17,8 @@ Layer, ) +from alertmanager_client import Alertmanager, AlertmanagerBadResponse + logger = logging.getLogger(__name__) diff --git a/src/charm.py b/src/charm.py index e6d433d2..a4b8b787 100755 --- a/src/charm.py +++ b/src/charm.py @@ -13,12 +13,6 @@ from urllib.parse import urlparse import yaml -from alertmanager import ( - ConfigFileSystemState, - ConfigUpdateFailure, - WorkloadManager, - WorkloadManagerError, -) from charms.alertmanager_k8s.v0.alertmanager_remote_configuration import ( RemoteConfigurationRequirer, ) @@ -38,7 +32,6 @@ from charms.tempo_k8s.v1.charm_tracing import trace_charm from charms.tempo_k8s.v2.tracing import TracingEndpointRequirer from charms.traefik_k8s.v2.ingress import IngressPerAppRequirer -from config_builder import ConfigBuilder, ConfigError from ops.charm import ActionEvent, CharmBase from ops.main import main from ops.model import ( @@ -51,6 +44,14 @@ ) from ops.pebble import PathError, ProtocolError # type: ignore +from alertmanager import ( + ConfigFileSystemState, + ConfigUpdateFailure, + WorkloadManager, + WorkloadManagerError, +) +from config_builder import ConfigBuilder, ConfigError + logger = logging.getLogger(__name__) diff --git a/tests/integration/test_persistence.py b/tests/integration/test_persistence.py index 5e74ecb9..29503b81 100644 --- a/tests/integration/test_persistence.py +++ b/tests/integration/test_persistence.py @@ -8,10 +8,11 @@ import pytest import yaml -from alertmanager_client import Alertmanager from helpers import get_unit_address, is_alertmanager_up, uk8s_group from pytest_operator.plugin import OpsTest +from alertmanager_client import Alertmanager + logger = logging.getLogger(__name__) METADATA = yaml.safe_load(Path("./metadata.yaml").read_text()) diff --git a/tests/scenario/conftest.py b/tests/scenario/conftest.py index 5a1cfa25..e241438f 100644 --- a/tests/scenario/conftest.py +++ b/tests/scenario/conftest.py @@ -4,9 +4,10 @@ from unittest.mock import patch import pytest +from scenario import Context + from alertmanager import WorkloadManager from charm import AlertmanagerCharm -from scenario import Context def tautology(*_, **__) -> bool: diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index cee6b7b4..50c6d8b4 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -6,13 +6,14 @@ import ops import yaml -from alertmanager import WorkloadManager -from charm import AlertmanagerCharm from helpers import k8s_resource_multipatch from ops import pebble from ops.model import ActiveStatus, BlockedStatus from ops.testing import Harness +from alertmanager import WorkloadManager +from charm import AlertmanagerCharm + ops.testing.SIMULATE_CAN_CONNECT = True diff --git a/tests/unit/test_external_url.py b/tests/unit/test_external_url.py index db177b44..30a281ca 100644 --- a/tests/unit/test_external_url.py +++ b/tests/unit/test_external_url.py @@ -8,11 +8,12 @@ import ops import yaml -from alertmanager import WorkloadManager -from charm import AlertmanagerCharm from helpers import cli_arg, k8s_resource_multipatch from ops.testing import Harness +from alertmanager import WorkloadManager +from charm import AlertmanagerCharm + logger = logging.getLogger(__name__) ops.testing.SIMULATE_CAN_CONNECT = True diff --git a/tests/unit/test_push_config_to_workload_on_startup.py b/tests/unit/test_push_config_to_workload_on_startup.py index d949ec70..af4142cb 100644 --- a/tests/unit/test_push_config_to_workload_on_startup.py +++ b/tests/unit/test_push_config_to_workload_on_startup.py @@ -10,13 +10,14 @@ import ops import validators import yaml -from alertmanager import WorkloadManager -from charm import AlertmanagerCharm from helpers import k8s_resource_multipatch from hypothesis import given from ops.model import ActiveStatus, BlockedStatus from ops.testing import Harness +from alertmanager import WorkloadManager +from charm import AlertmanagerCharm + logger = logging.getLogger(__name__) ops.testing.SIMULATE_CAN_CONNECT = True CONTAINER_NAME = "alertmanager" diff --git a/tests/unit/test_remote_configuration_requirer.py b/tests/unit/test_remote_configuration_requirer.py index 9c380d90..97741806 100644 --- a/tests/unit/test_remote_configuration_requirer.py +++ b/tests/unit/test_remote_configuration_requirer.py @@ -8,8 +8,6 @@ from unittest.mock import Mock, patch import yaml -from alertmanager import WorkloadManager -from charm import AlertmanagerCharm from charms.alertmanager_k8s.v0.alertmanager_remote_configuration import ( DEFAULT_RELATION_NAME, ) @@ -18,6 +16,9 @@ from ops import testing from ops.model import BlockedStatus +from alertmanager import WorkloadManager +from charm import AlertmanagerCharm + logger = logging.getLogger(__name__) testing.SIMULATE_CAN_CONNECT = True diff --git a/tests/unit/test_self_scrape_jobs.py b/tests/unit/test_self_scrape_jobs.py index 67c7005a..dfbd733e 100644 --- a/tests/unit/test_self_scrape_jobs.py +++ b/tests/unit/test_self_scrape_jobs.py @@ -4,11 +4,12 @@ import unittest from unittest.mock import PropertyMock, patch -from alertmanager import WorkloadManager -from charm import AlertmanagerCharm from helpers import k8s_resource_multipatch from ops.testing import Harness +from alertmanager import WorkloadManager +from charm import AlertmanagerCharm + class TestWithInitialHooks(unittest.TestCase): container_name: str = "alertmanager"