Skip to content

Commit fc56e7d

Browse files
committed
ops[tracing] with a charm lib
1 parent 33884f9 commit fc56e7d

28 files changed

+1845
-297
lines changed

docs/howto/trace-the-charm-code.md

+3-18
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,15 @@
33

44
## Tracing from scratch
55

6-
FIXME: write this up
7-
8-
- depend on `ops[tracing]`
9-
- remove charm\_tracing charm lib, if it's installed
10-
- observe the `SetupTracingEvent`
11-
12-
```py
13-
class YourCharm(ops.CharmBase):
14-
def __init__(self, framework: ops.Framework):
15-
super().__init__(framework)
16-
self.framework.observe(self.on.setup_tracing, self._on_setup_tracing)
17-
...
18-
19-
def _on_setup_tracing(self, event: ops.SetupTracingEvent) -> None:
20-
# FIXME must get this from some relation
21-
event.set_destination(url='http://localhost:4318/v1/traces')
22-
```
6+
FIXME: copy from tracing/api.py
237

248
## Migrating from charm\_tracing
259

2610
- depend on `ops[tracing]`
11+
- remove direct dependencies on `opentelemetry-api, -sdk, etc.`
2712
- remove charm\_tracing charm lib, if it's installed
2813
- remove `@trace_charm` decorator
29-
- observe the `SetupTracingEvent`
14+
- include `ops._tracing.Tracing()` in your charm's `__init__`
3015
- instrument key functions in the charm
3116

3217
NOTE: charm\_tracing auto-instruments all public function on the class. `ops[tracing]` doesn't do that.

docs/requirements.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#
77
alabaster==1.0.0
88
# via sphinx
9+
annotated-types==0.7.0
10+
# via pydantic
911
anyio==4.8.0
1012
# via
1113
# starlette
@@ -101,12 +103,16 @@ opentelemetry-semantic-conventions==0.51b0
101103
# opentelemetry-sdk
102104
opentelemetry-util-http==0.51b0
103105
# via opentelemetry-instrumentation-urllib
104-
otlp-json==0.9.4
106+
otlp-json==0.9.7
105107
# via ops (pyproject.toml)
106108
packaging==24.2
107109
# via
108110
# opentelemetry-instrumentation
109111
# sphinx
112+
pydantic==2.10.6
113+
# via ops (pyproject.toml)
114+
pydantic-core==2.27.2
115+
# via pydantic
110116
pygments==2.19.1
111117
# via
112118
# furo
@@ -183,6 +189,8 @@ typing-extensions==4.12.2
183189
# via
184190
# anyio
185191
# opentelemetry-sdk
192+
# pydantic
193+
# pydantic-core
186194
uc-micro-py==1.0.3
187195
# via linkify-it-py
188196
urllib3==2.3.0

dont-merge/fake-charm.py

-96
This file was deleted.

dont-merge/metadata.yaml

-1
This file was deleted.

dont-merge/readme.md

-36
This file was deleted.

ops/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
'SecretExpiredEvent',
100100
'SecretRemoveEvent',
101101
'SecretRotateEvent',
102-
'SetupTracingEvent',
103102
'StartEvent',
104103
'StopEvent',
105104
'StorageAttachedEvent',
@@ -246,7 +245,6 @@
246245
SecretExpiredEvent,
247246
SecretRemoveEvent,
248247
SecretRotateEvent,
249-
SetupTracingEvent,
250248
StartEvent,
251249
StopEvent,
252250
StorageAttachedEvent,

ops/_main.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ def _make_framework(self, dispatcher: _Dispatcher):
491491

492492
def _emit(self):
493493
"""Emit the event on the charm."""
494-
_charm._setup_tracing(self.charm)
495494
# TODO: Remove the collect_metrics check below as soon as the relevant
496495
# Juju changes are made. Also adjust the docstring on
497496
# EventBase.defer().
@@ -556,14 +555,11 @@ def main(charm_class: Type[_charm.CharmBase], use_juju_for_storage: Optional[boo
556555
557556
See `ops.main() <#ops-main-entry-point>`_ for details.
558557
"""
559-
_tracing.setup_tracing(charm_class.__name__)
560-
561-
try:
562-
with tracer.start_as_current_span('ops.main'):
563-
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)
564562

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

ops/_tracing/__init__.py

+6-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,18 @@
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]:
43+
yield
4144

4245

4346
__all__ = [
4447
'mark_observed',
4548
'set_tracing_destination',
4649
'setup_tracing',
47-
'shutdown_tracing',
4850
]

0 commit comments

Comments
 (0)