Skip to content
Open
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
4 changes: 3 additions & 1 deletion agentex/src/utils/otel_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def _detected_resource() -> Resource:
def _unique_instance_id(resource: Resource) -> str:
"""Worker-unique service.instance.id (OTel #4390)."""
pid = os.getpid()
existing = resource.attributes.get("service.instance.id")
# Only honor an explicitly configured id: since opentelemetry-sdk 1.45,
# Resource.create() fills service.instance.id with a random UUID by default.
existing = OTELResourceDetector().detect().attributes.get("service.instance.id")
if existing:
existing = str(existing)
suffix = f".{pid}"
Expand Down
20 changes: 20 additions & 0 deletions agentex/tests/unit/utils/test_otel_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ def test_unique_instance_id_builds_when_missing(monkeypatch):
assert otel_metrics._unique_instance_id(base) == "agentex.my-pod.42"


@pytest.mark.unit
def test_unique_instance_id_ignores_sdk_generated_default(monkeypatch):
monkeypatch.setenv("OTEL_SERVICE_NAME", "agentex")
monkeypatch.setenv("OTEL_RESOURCE_ATTRIBUTES", "k8s.pod.name=my-pod")
monkeypatch.setattr(otel_metrics.os, "getpid", lambda: 42)
base = Resource.create({"service.instance.id": "sdk-generated-uuid"})
assert otel_metrics._unique_instance_id(base) == "agentex.my-pod.42"


@pytest.mark.unit
def test_unique_instance_id_is_idempotent(monkeypatch):
monkeypatch.setenv(
"OTEL_RESOURCE_ATTRIBUTES",
"k8s.pod.name=my-pod,service.instance.id=agentex.my-pod.agentex.42",
)
monkeypatch.setattr(otel_metrics.os, "getpid", lambda: 42)
base = get_aggregated_resources([OTELResourceDetector()])
assert otel_metrics._unique_instance_id(base) == "agentex.my-pod.agentex.42"


@pytest.mark.unit
def test_sync_instance_id_to_env_updates_env(monkeypatch):
monkeypatch.setenv("OTEL_SERVICE_NAME", "agentex")
Expand Down