Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions python/iot3/src/iot3/core/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ def __init__(
*,
service_name: str,
endpoint: str,
auth: Auth = Auth.NONE,
service_id: Optional[str] = None,
auth: Optional[Auth] = Auth.NONE,
username: Optional[str] = None,
password: Optional[str] = None,
batch_period: Optional[float] = None,
max_backlog: int = 1023,
compression: Compression = Compression.NONE,
max_backlog: Optional[int] = 1023,
compression: Optional[Compression] = Compression.NONE,
):
"""
Simple span exporter
Expand All @@ -94,6 +95,7 @@ def __init__(
pending spans not yet exported.

:param service_name: The name of the service.
:param service_id: The ID of this instance.
:param endpoint: The URL to the OTLP collector (without the trailing
/v1/traces).
:param auth: Type of HTTP authentication to employ.
Expand All @@ -110,6 +112,7 @@ def __init__(

self.service_name = service_name
self.endpoint = endpoint
self.service_id = service_id
if self.endpoint.endswith("/"):
self.endpoint = self.endpoint[:-1]

Expand Down Expand Up @@ -186,6 +189,11 @@ def span(self, *args, **kwargs) -> "Span":
kwargs["parent_span"] = current_span

s = Span(*args, **kwargs)
if self.service_id:
s.set_attribute(
key="iot3.core.service_id",
value=self.service_id,
)
try:
self.tls.spans.append(s)
yield s
Expand Down
12 changes: 12 additions & 0 deletions python/iot3/tests/test-iot3-core-otel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import time

o = iot3.core.otel.Otel(
service_name="test-service",
service_id="test_42",
endpoint="http://localhost:4318",
batch_period=15,
max_backlog=15,
Expand Down Expand Up @@ -33,4 +34,15 @@ with o.span(name="Root span") as s0:
with o.span(name="Root span with link", span_links=[s0]) as s10:
time.sleep(0.1)

attrs = [
attr["value"]["stringValue"]
for attr in s0.to_dict()["attributes"]
if (
attr["key"] == "iot3.core.service_id"
and "stringValue" in attr["value"]
)
]
assert len(attrs) == 1
assert attrs[0] == "test_42"

o.stop()
1 change: 1 addition & 0 deletions python/its-interqueuemanager/src/its_iqm/iqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
self.otel = iot3.core.otel.Otel(
service_name="its-interqueuemanager",
endpoint=cfg["telemetry"]["endpoint"],
service_id=self.instance_id,
auth=iot3.core.otel.Auth(cfg["telemetry"]["authentication"]),
username=cfg["telemetry"]["username"],
password=cfg["telemetry"]["password"],
Expand Down
1 change: 1 addition & 0 deletions python/its-vehicle/src/its_vehicle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def _set_default(section, key, default):
otel = iot3.core.otel.Otel(
service_name="its-vehicle",
endpoint=cfg["telemetry"]["endpoint"],
service_id=cfg["general"]["instance-id"],
auth=iot3.core.otel.Auth(cfg["telemetry"]["authentication"]),
username=cfg["telemetry"]["username"],
password=cfg["telemetry"]["password"],
Expand Down
Loading