@@ -20,13 +20,14 @@ package elastic
2020import (
2121 "testing"
2222
23- "github.com/elastic/opentelemetry-lib/elasticattr"
24- "github.com/elastic/opentelemetry-lib/enrichments/config"
2523 "github.com/google/go-cmp/cmp"
2624 "github.com/stretchr/testify/assert"
2725 "go.opentelemetry.io/collector/pdata/pcommon"
2826 semconv25 "go.opentelemetry.io/otel/semconv/v1.25.0"
2927 semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
28+
29+ "github.com/elastic/opentelemetry-lib/elasticattr"
30+ "github.com/elastic/opentelemetry-lib/enrichments/config"
3031)
3132
3233func TestResourceEnrich (t * testing.T ) {
@@ -174,10 +175,11 @@ func TestResourceEnrich(t *testing.T) {
174175 }(),
175176 config : config .Enabled ().Resource ,
176177 enrichedAttrs : map [string ]any {
177- string (semconv .HostNameKey ): "k8s-node" ,
178- string (semconv .K8SNodeNameKey ): "k8s-node" ,
179- elasticattr .AgentName : "otlp" ,
180- elasticattr .AgentVersion : "unknown" ,
178+ string (semconv .HostNameKey ): "k8s-node" ,
179+ string (semconv .K8SNodeNameKey ): "k8s-node" ,
180+ elasticattr .AgentName : "otlp" ,
181+ elasticattr .AgentVersion : "unknown" ,
182+ string (semconv25 .ServiceInstanceIDKey ): string ("test-host" ),
181183 },
182184 },
183185 {
@@ -245,6 +247,58 @@ func TestResourceEnrich(t *testing.T) {
245247 elasticattr .AgentVersion : "unknown" ,
246248 },
247249 },
250+ {
251+ name : "service_instance_id_derived_from_container_id" ,
252+ input : func () pcommon.Resource {
253+ res := pcommon .NewResource ()
254+ res .Attributes ().PutStr (string (semconv .ServiceInstanceIDKey ), "" )
255+ res .Attributes ().PutStr (string (semconv25 .ContainerIDKey ), "container-id" )
256+ res .Attributes ().PutStr (string (semconv25 .HostNameKey ), "k8s-node" )
257+ return res
258+ }(),
259+ config : config .Enabled ().Resource ,
260+ enrichedAttrs : map [string ]any {
261+ string (semconv25 .ServiceInstanceIDKey ): "container-id" ,
262+ string (semconv .ContainerIDKey ): "container-id" ,
263+ string (semconv .HostNameKey ): "k8s-node" ,
264+ elasticattr .AgentName : "otlp" ,
265+ elasticattr .AgentVersion : "unknown" ,
266+ },
267+ },
268+ {
269+ name : "service_instance_id_derived_from_host_name" ,
270+ input : func () pcommon.Resource {
271+ res := pcommon .NewResource ()
272+ res .Attributes ().PutStr (string (semconv .ServiceInstanceIDKey ), "" )
273+ res .Attributes ().PutStr (string (semconv25 .HostNameKey ), "k8s-node" )
274+ return res
275+ }(),
276+ config : config .Enabled ().Resource ,
277+ enrichedAttrs : map [string ]any {
278+ string (semconv25 .ServiceInstanceIDKey ): "k8s-node" ,
279+ string (semconv .HostNameKey ): "k8s-node" ,
280+ elasticattr .AgentName : "otlp" ,
281+ elasticattr .AgentVersion : "unknown" ,
282+ },
283+ },
284+ {
285+ name : "service_instance_id_already_set" ,
286+ input : func () pcommon.Resource {
287+ res := pcommon .NewResource ()
288+ res .Attributes ().PutStr (string (semconv .ServiceInstanceIDKey ), "node-name" )
289+ res .Attributes ().PutStr (string (semconv25 .ContainerIDKey ), "container-id" )
290+ res .Attributes ().PutStr (string (semconv25 .HostNameKey ), "k8s-node" )
291+ return res
292+ }(),
293+ config : config .Enabled ().Resource ,
294+ enrichedAttrs : map [string ]any {
295+ string (semconv25 .ServiceInstanceIDKey ): "node-name" ,
296+ string (semconv .ContainerIDKey ): "container-id" ,
297+ string (semconv .HostNameKey ): "k8s-node" ,
298+ elasticattr .AgentName : "otlp" ,
299+ elasticattr .AgentVersion : "unknown" ,
300+ },
301+ },
248302 } {
249303 t .Run (tc .name , func (t * testing.T ) {
250304 // Merge existing resource attrs with the attrs added
0 commit comments