Skip to content

Commit 6d6ff4e

Browse files
committed
squash
1 parent a80e143 commit 6d6ff4e

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

ops/_tracing/export.py

+24
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
106106

107107
return SpanExportResult.SUCCESS
108108
except Exception:
109+
# FIXME: I'm using this to catch bug during development.
110+
# OTEL must disable logging capture during export to avoid data loops.
111+
# At least during develpoment, we want to catch and report pure bugs.
112+
# Perhaps this part needs to be removed before merge/release.
113+
# Leaving here for now to decide how to test this code path.
109114
logger.exception('export')
110115
raise
111116

@@ -225,6 +230,12 @@ def setup_tracing(charm_class_name: str) -> None:
225230
span_processor = BatchSpanProcessor(_exporter)
226231
provider.add_span_processor(span_processor)
227232
set_tracer_provider(provider)
233+
# FIXME: in testing with tracing, we need a hack.
234+
# OpenTelemetry disallows setting the tracer provider twice,
235+
# a waring is issued and new provider is ignored.
236+
#
237+
# For example, we could reset the resource instead:
238+
# get_tracer_provider()._resource = resource
228239

229240

230241
def set_tracing_destination(
@@ -240,6 +251,19 @@ def set_tracing_destination(
240251
assert _exporter, 'tracing has not been set up'
241252
_exporter.settings = (url, ca)
242253

254+
# FIXME: this is not right
255+
# We recommend obsering the SetupTracingEvent unconditionally.
256+
# This means that this function is called whenever there's a relation.
257+
# That's regardless of deferred events being emitted or observed.
258+
# And regardless of the dispatched event being observed.
259+
#
260+
# There needs to be a separate function to mark this dispatch invocation
261+
# as observed. The logic would be somewhat complicated though.
262+
# In essense:
263+
# - any deferred event observed (perhaps simply emitted), or
264+
# - the dispatched event observed
265+
#
266+
# Arguably if a defferred event is emitted, it's almost always observed.
243267
_exporter.buffer.mark_observed()
244268

245269

ops/charm.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1180,13 +1180,24 @@ def add_status(self, status: model.StatusBase):
11801180
model_.unit._collected_statuses.append(status)
11811181

11821182

1183-
# FIXME: API design choice
1184-
# Should we make this more generic?
1185-
# - call this a TelemetryEvent / TelemeryConfigEvent / SetupTelemetryEvent
1186-
# - provide .set_tracing_destination() in this PR
1187-
# - later, perhaps .set_logging_xxx() and .set_metrics_yyy()?
11881183
class SetupTracingEvent(LifecycleEvent):
1189-
"""FIXME docstring."""
1184+
"""Event that allows the charm to configure tracing destination.
1185+
1186+
This event triggered before deferred events and the dispatched event.
1187+
1188+
Example use may look like this::
1189+
1190+
class MyCharm(ops.CharmBase):
1191+
def __init__(self, framework.ops.Framework):
1192+
super().__init__(framework)
1193+
self.framework.observe(self.on.setup_tracing, self._on_setup_tracing)
1194+
...
1195+
1196+
def _on_setup_tracing(self, event: ops.SetupTracingEvent) -> None:
1197+
url: str | None = get_tracing_url_from_relation_data()
1198+
ca: str | None = optionally_get_ca_for_a_tls_connection()
1199+
event.set_destination(url=url)
1200+
"""
11901201

11911202
def set_destination(self, *, url: Optional[str], ca: Optional[str] = None) -> None:
11921203
"""Configure the destination service for tracing data.
@@ -1362,7 +1373,6 @@ def __init__(self, framework: ops.Framework):
13621373
@property
13631374
def on(self) -> CharmEvents: ... # noqa
13641375

1365-
@tracer.start_as_current_span('ops.CharmBase')
13661376
def __init__(self, framework: Framework):
13671377
super().__init__(framework, None)
13681378

0 commit comments

Comments
 (0)