Skip to content

Commit eda948c

Browse files
authoredMar 11, 2025··
resources: introduce a resource detector sending server.instance.id (#259)
* resources: introduce a resource detector sending server.instance.id Introduce a resource detector sending a stringified UUIDv4 as value for `service.instance.id` attribute. And enable it by default. * Update docs
1 parent 6c0137e commit eda948c

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed
 

‎docs/configure.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ EDOT Python uses different defaults than OpenTelemetry Python for the following
4848

4949
| Option | EDOT Python default | OpenTelemetry Python default |
5050
|---|---|---|
51-
| `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `process_runtime,os,otel,telemetry_distro,_gcp,aws_ec2,aws_ecs,aws_elastic_beanstalk,azure_app_service,azure_vm` | `otel` |
51+
| `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` | `process_runtime,os,otel,telemetry_distro,service_instance,_gcp,aws_ec2,aws_ecs,aws_elastic_beanstalk,azure_app_service,azure_vm` | `otel` |
5252
| `OTEL_METRICS_EXEMPLAR_FILTER` | `always_off` | `trace_based` |
5353
| `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` | `DELTA` | `CUMULATIVE` |
5454

5555
> [!NOTE]
56-
> `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` cloud resource detectors are dynamically set. When running in a Kubernetes Pod it will be set to `process_runtime,os,otel,telemetry_distro,_gcp,aws_eks`.
56+
> `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` cloud resource detectors are dynamically set. When running in a Kubernetes Pod it will be set to `process_runtime,os,otel,telemetry_distro,service_instance,_gcp,aws_eks`.
5757
5858

5959
### Configuration options that are _only_ available in EDOT Python

‎pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ distro = "elasticotel.distro:ElasticOpenTelemetryDistro"
5252
[project.entry-points.opentelemetry_resource_detector]
5353
process_runtime = "elasticotel.sdk.resources:ProcessRuntimeResourceDetector"
5454
telemetry_distro = "elasticotel.sdk.resources:TelemetryDistroResourceDetector"
55+
service_instance = "elasticotel.sdk.resources:ServiceInstanceResourceDetector"
5556
_gcp = "opentelemetry.resourcedetector.gcp_resource_detector._detector:GoogleCloudResourceDetector"
5657

5758
[project.scripts]

‎src/elasticotel/distro/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ def _configure(self, **kwargs):
7575
# preference to use DELTA temporality as we can handle only this kind of Histograms
7676
os.environ.setdefault(OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE, "DELTA")
7777

78-
base_resource_detectors = ["process_runtime", "os", "otel", "telemetry_distro"]
78+
base_resource_detectors = ["process_runtime", "os", "otel", "telemetry_distro", "service_instance"]
7979
detectors = base_resource_detectors + get_cloud_resource_detectors()
8080
os.environ.setdefault(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, ",".join(detectors))

‎src/elasticotel/sdk/resources/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
# limitations under the License.
1616

1717
import sys
18+
import uuid
1819

1920
from opentelemetry.semconv._incubating.attributes import telemetry_attributes
21+
from opentelemetry.semconv._incubating.attributes import service_attributes
2022
from opentelemetry.sdk.resources import (
2123
Attributes,
2224
Resource,
@@ -51,6 +53,16 @@ def detect(self) -> "Resource":
5153
return Resource(resource_info)
5254

5355

56+
class ServiceInstanceResourceDetector(ResourceDetector):
57+
"""Resource detector to fill service instance attributes
58+
59+
NOTE: with multi-process services this is shared between processes"""
60+
61+
def detect(self) -> "Resource":
62+
resource_info: Attributes = {service_attributes.SERVICE_INSTANCE_ID: str(uuid.uuid4())}
63+
return Resource(resource_info)
64+
65+
5466
class TelemetryDistroResourceDetector(ResourceDetector):
5567
"""Resource detector to fill telemetry.distro attributes"""
5668

‎tests/distro/test_distro.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_default_configuration(self):
4242
self.assertEqual("otlp", os.environ.get(OTEL_LOGS_EXPORTER))
4343
self.assertEqual("grpc", os.environ.get(OTEL_EXPORTER_OTLP_PROTOCOL))
4444
self.assertEqual(
45-
"process_runtime,os,otel,telemetry_distro,_gcp,aws_ec2,aws_ecs,aws_elastic_beanstalk,azure_app_service,azure_vm",
45+
"process_runtime,os,otel,telemetry_distro,service_instance,_gcp,aws_ec2,aws_ecs,aws_elastic_beanstalk,azure_app_service,azure_vm",
4646
os.environ.get(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS),
4747
)
4848
self.assertEqual("always_off", os.environ.get(OTEL_METRICS_EXEMPLAR_FILTER))

‎tests/resources/test_resources.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from unittest import TestCase
17+
from unittest import TestCase, mock
1818

19-
from elasticotel.sdk.resources import ProcessRuntimeResourceDetector, TelemetryDistroResourceDetector
19+
from elasticotel.sdk.resources import (
20+
ProcessRuntimeResourceDetector,
21+
ServiceInstanceResourceDetector,
22+
TelemetryDistroResourceDetector,
23+
)
2024
from opentelemetry.sdk.resources import (
2125
PROCESS_RUNTIME_NAME,
2226
PROCESS_RUNTIME_DESCRIPTION,
@@ -39,6 +43,20 @@ def test_process_runtime_detector(self):
3943
)
4044

4145

46+
class TestServiceInstanceDetector(TestCase):
47+
def test_service_instance_detector(self):
48+
initial_resource = Resource(attributes={})
49+
aggregated_resource = get_aggregated_resources([ServiceInstanceResourceDetector()], initial_resource)
50+
51+
self.assertEqual(
52+
aggregated_resource.attributes,
53+
{
54+
"service.instance.id": mock.ANY,
55+
},
56+
)
57+
self.assertTrue(isinstance(aggregated_resource.attributes["service.instance.id"], str))
58+
59+
4260
class TestTelemetryDistroDetector(TestCase):
4361
def test_telemetry_distro_detector(self):
4462
initial_resource = Resource(attributes={})

0 commit comments

Comments
 (0)
Please sign in to comment.