Skip to content

Commit ee2f38a

Browse files
committed
cleanup
1 parent b283701 commit ee2f38a

File tree

6 files changed

+18
-1432
lines changed

6 files changed

+18
-1432
lines changed

ops/_main.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,11 @@ def main(charm_class: Type[_charm.CharmBase], use_juju_for_storage: Optional[boo
555555
556556
See `ops.main() <#ops-main-entry-point>`_ for details.
557557
"""
558-
_tracing.setup_tracing(charm_class.__name__)
559-
560-
try:
561-
with tracer.start_as_current_span('ops.main'):
562-
manager = _Manager(charm_class, use_juju_for_storage=use_juju_for_storage)
558+
with _tracing.setup_tracing(charm_class.__name__):
559+
try:
560+
with tracer.start_as_current_span('ops.main'):
561+
manager = _Manager(charm_class, use_juju_for_storage=use_juju_for_storage)
563562

564563
manager.run()
565-
except _Abort as e:
566-
sys.exit(e.exit_code)
567-
finally:
568-
_tracing.shutdown_tracing()
564+
except _Abort as e:
565+
sys.exit(e.exit_code)

ops/_tracing/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
from __future__ import annotations
2020

21+
from contextlib import contextmanager
22+
from typing import Generator
23+
2124
import opentelemetry.trace
2225

2326
import ops.version
@@ -30,19 +33,17 @@
3033
mark_observed,
3134
set_tracing_destination,
3235
setup_tracing,
33-
shutdown_tracing,
3436
)
3537
except ImportError:
3638

3739
def mark_observed() -> None: ...
3840
def set_tracing_destination(*, url: str | None, ca: str | None = None) -> None: ...
39-
def setup_tracing(charm_class_name: str) -> None: ...
40-
def shutdown_tracing() -> None: ...
41+
@contextmanager
42+
def setup_tracing(charm_class_name: str) -> Generator[None, None, None]: yield
4143

4244

4345
__all__ = [
4446
'mark_observed',
4547
'set_tracing_destination',
4648
'setup_tracing',
47-
'shutdown_tracing',
4849
]

ops/_tracing/export.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import urllib.error
2323
import urllib.request
2424
from pathlib import Path
25-
from typing import Sequence
25+
from typing import Generator, Sequence
2626

2727
import otlp_json
2828
from opentelemetry.instrumentation.urllib import URLLibInstrumentor
@@ -200,7 +200,8 @@ def suppress_juju_log_handler():
200200
juju_log_handler.drop.reset(token)
201201

202202

203-
def setup_tracing(charm_class_name: str) -> None:
203+
@contextlib.contextmanager
204+
def setup_tracing(charm_class_name: str) -> Generator[None, None, None]:
204205
global _exporter
205206
# FIXME would it be better to pass Juju context explicitly?
206207
juju_context = ops.jujucontext._JujuContext.from_dict(os.environ)
@@ -224,6 +225,10 @@ def setup_tracing(charm_class_name: str) -> None:
224225
span_processor = BatchSpanProcessor(_exporter)
225226
provider.add_span_processor(span_processor)
226227
set_tracer_provider(provider)
228+
try:
229+
yield
230+
finally:
231+
shutdown_tracing()
227232
# FIXME: in testing with tracing, we need a hack.
228233
# OpenTelemetry disallows setting the tracer provider twice,
229234
# a warning is issued and new provider is ignored.

0 commit comments

Comments
 (0)