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

Commit

Permalink
vbump and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Aug 13, 2024
1 parent 883c9d5 commit 8987360
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 65 deletions.
69 changes: 35 additions & 34 deletions lib/charms/tempo_k8s/v1/charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,24 @@ def _remove_stale_otel_sdk_packages():
import functools
import inspect
import logging
import opentelemetry
import ops
import os
from contextlib import contextmanager
from contextvars import Context, ContextVar, copy_context
from pathlib import Path
from typing import (
Any,
Callable,
Generator,
Optional,
Sequence,
Type,
TypeVar,
Union,
cast,
)

import opentelemetry
import ops
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Span, TracerProvider
Expand All @@ -247,18 +260,6 @@ def _remove_stale_otel_sdk_packages():
from opentelemetry.trace import get_current_span as otlp_get_current_span
from ops.charm import CharmBase
from ops.framework import Framework
from pathlib import Path
from typing import (
Any,
Callable,
Generator,
Optional,
Sequence,
Type,
TypeVar,
Union,
cast,
)

# The unique Charmhub library identifier, never change it
LIBID = "cb1705dcd1a14ca09b2e60187d1215c7"
Expand All @@ -269,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"]

Expand Down Expand Up @@ -371,9 +372,9 @@ class TLSError(TracingError):


def _get_tracing_endpoint(
tracing_endpoint_attr: str,
charm_instance: object,
charm_type: type,
tracing_endpoint_attr: str,
charm_instance: object,
charm_type: type,
):
_tracing_endpoint = getattr(charm_instance, tracing_endpoint_attr)
if callable(_tracing_endpoint):
Expand All @@ -395,9 +396,9 @@ def _get_tracing_endpoint(


def _get_server_cert(
server_cert_attr: str,
charm_instance: ops.CharmBase,
charm_type: Type[ops.CharmBase],
server_cert_attr: str,
charm_instance: ops.CharmBase,
charm_type: Type[ops.CharmBase],
):
_server_cert = getattr(charm_instance, server_cert_attr)
if callable(_server_cert):
Expand All @@ -419,10 +420,10 @@ def _get_server_cert(


def _setup_root_span_initializer(
charm_type: _CharmType,
tracing_endpoint_attr: str,
server_cert_attr: Optional[str],
service_name: Optional[str] = None,
charm_type: _CharmType,
tracing_endpoint_attr: str,
server_cert_attr: Optional[str],
service_name: Optional[str] = None,
):
"""Patch the charm's initializer."""
original_init = charm_type.__init__
Expand Down Expand Up @@ -545,10 +546,10 @@ def wrap_close():


def trace_charm(
tracing_endpoint: str,
server_cert: Optional[str] = None,
service_name: Optional[str] = None,
extra_types: Sequence[type] = (),
tracing_endpoint: str,
server_cert: Optional[str] = None,
service_name: Optional[str] = None,
extra_types: Sequence[type] = (),
) -> Callable[[_T], _T]:
"""Autoinstrument the decorated charm with tracing telemetry.
Expand Down Expand Up @@ -607,11 +608,11 @@ def _decorator(charm_type: _T) -> _T:


def _autoinstrument(
charm_type: _T,
tracing_endpoint_attr: str,
server_cert_attr: Optional[str] = None,
service_name: Optional[str] = None,
extra_types: Sequence[type] = (),
charm_type: _T,
tracing_endpoint_attr: str,
server_cert_attr: Optional[str] = None,
service_name: Optional[str] = None,
extra_types: Sequence[type] = (),
) -> _T:
"""Set up tracing on this charm class.
Expand Down
10 changes: 5 additions & 5 deletions tests/scenario/test_charm_tracer_multi_import_warning.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from ops import CharmBase
from scenario import Context, State
from unittest.mock import patch

# this test file is intentionally quite broken, don't modify the imports
# import autoinstrument and get_current_span from charms.[...]
from charms.tempo_k8s.v1.charm_tracing import _autoinstrument as autoinstrument
from ops import CharmBase
from scenario import Context, State

# import trace from lib.charms.[...]
from lib.charms.tempo_k8s.v1.charm_tracing import trace

Expand Down Expand Up @@ -38,11 +39,10 @@ def test_charm_tracer_multi_import_warning(caplog, monkeypatch):
See https://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-double-import-trap
for a great explanation of what's going on.
"""

import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSimpleEvent, meta=MyCharmSimpleEvent.META)
Expand All @@ -55,6 +55,6 @@ def test_charm_tracer_multi_import_warning(caplog, monkeypatch):
"function call: _my_fn",
"method call: MyCharmSimpleEvent._on_start",
"event: start",
"margherita/0: start event"
"margherita/0: start event",
]
assert "Tracer not found in `tracer` context var." in caplog.records[0].message
49 changes: 23 additions & 26 deletions tests/scenario/test_charm_tracing.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import functools
import logging
import os
import pytest
import scenario
from ops import EventBase, EventSource, Framework
from ops.charm import CharmBase, CharmEvents
from scenario import Context, State
from scenario.runtime import UncaughtCharmError
from unittest.mock import patch

from charms.tempo_k8s.v1.charm_tracing import (
CHARM_TRACING_ENABLED, _autoinstrument as autoinstrument, get_current_span, trace
)
import pytest
import scenario
from charms.tempo_k8s.v1.charm_tracing import CHARM_TRACING_ENABLED, get_current_span, trace
from charms.tempo_k8s.v1.charm_tracing import _autoinstrument as autoinstrument
from charms.tempo_k8s.v2.tracing import (
ProtocolType,
Receiver,
TracingEndpointRequirer,
TracingProviderAppData,
TracingRequirerAppData,
)
from ops import EventBase, EventSource, Framework
from ops.charm import CharmBase, CharmEvents
from scenario import Context, State
from scenario.runtime import UncaughtCharmError

os.environ[CHARM_TRACING_ENABLED] = "1"

Expand Down Expand Up @@ -54,7 +53,7 @@ def test_base_tracer_endpoint(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSimple, meta=MyCharmSimple.META)
Expand Down Expand Up @@ -95,7 +94,7 @@ def test_subobj_tracer_endpoint(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSubObject, meta=MyCharmSubObject.META)
Expand Down Expand Up @@ -123,7 +122,7 @@ def test_init_attr(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmInitAttr, meta=MyCharmInitAttr.META)
Expand All @@ -150,7 +149,7 @@ def test_base_tracer_endpoint_disabled(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSimpleDisabled, meta=MyCharmSimpleDisabled.META)
Expand Down Expand Up @@ -197,7 +196,7 @@ def test_base_tracer_endpoint_event(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSimpleEvent, meta=MyCharmSimpleEvent.META)
Expand All @@ -223,7 +222,7 @@ def test_juju_topology_injection(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmSimpleEvent, meta=MyCharmSimpleEvent.META)
Expand Down Expand Up @@ -272,7 +271,7 @@ def test_base_tracer_endpoint_methods(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmWithMethods, meta=MyCharmWithMethods.META)
Expand Down Expand Up @@ -326,7 +325,7 @@ def test_base_tracer_endpoint_custom_event(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmWithCustomEvents, meta=MyCharmWithCustomEvents.META)
Expand Down Expand Up @@ -461,9 +460,9 @@ def test_borky_tempo_return_value(borky_return_value, caplog):

# traceback from the TypeError raised by _get_tracing_endpoint
with pytest.raises(
UncaughtCharmError,
match=r"MyRemoteBorkyCharm\.tempo should resolve to a tempo "
r"endpoint \(string\); got (.*) instead\.",
UncaughtCharmError,
match=r"MyRemoteBorkyCharm\.tempo should resolve to a tempo "
r"endpoint \(string\); got (.*) instead\.",
):
ctx.run("start", State())

Expand Down Expand Up @@ -522,7 +521,7 @@ def test_trace_staticmethods(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmStaticMethods, meta=MyCharmStaticMethods.META)
Expand Down Expand Up @@ -551,7 +550,7 @@ def test_trace_staticmethods_bork(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmStaticMethods, meta=MyCharmStaticMethods.META)
Expand Down Expand Up @@ -585,7 +584,7 @@ def test_inheritance_tracing(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyInheritedCharm, meta=MyInheritedCharm.META)
Expand Down Expand Up @@ -640,13 +639,11 @@ def test_wrapped_method_wrapping(caplog):
import opentelemetry

with patch(
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
"opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter.export"
) as f:
f.return_value = opentelemetry.sdk.trace.export.SpanExportResult.SUCCESS
ctx = Context(MyCharmWrappedMethods, meta=MyCharmWrappedMethods.META)
ctx.run("start", State())
spans = f.call_args_list[0].args[0]
assert spans[0].name == "method call: MyCharmWrappedMethods.a"
assert spans[1].name == "method call: @bad_wrapper(MyCharmWrappedMethods.b)"


0 comments on commit 8987360

Please sign in to comment.