Skip to content

Commit

Permalink
STAC-21209: add telemetry sdk to service, test it
Browse files Browse the repository at this point in the history
  • Loading branch information
fvlankvelt committed Apr 29, 2024
1 parent 8c3f828 commit bc0b0fe
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
15 changes: 13 additions & 2 deletions exporter/ststopologyexporter/internal/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ func (c *ComponentsCollection) AddResource(attrs *pcommon.Map) bool {
withEnvironment(attrs).
withName(attrs, "service.name").
withVersion(attrs, "service.version").
withTag(attrs, "service.namespace"),
withTag(attrs, "service.namespace").
withTagPrefix(attrs, "telemetry.sdk"),
})
serviceInstanceIdentifier := fmt.Sprintf("urn:opentelemetry:namespace/%s:service/%s:serviceInstance/%s", serviceNamespace.AsString(), serviceName.AsString(), serviceInstanceId.AsString())
c.serviceInstances = append(c.serviceInstances, &Component{
serviceInstanceIdentifier,
ComponentType{
"service_instance",
"service-instance",
},
newComponentData().
withLayer("urn:stackpack:common:layer:containers").
Expand Down Expand Up @@ -187,6 +188,16 @@ func (c *ComponentData) withTag(attrs *pcommon.Map, key string) *ComponentData {
return c
}

func (c *ComponentData) withTagPrefix(attrs *pcommon.Map, prefix string) *ComponentData {
attrs.Range(func(k string, v pcommon.Value) bool {
if len(k) >= len(prefix) && k[:len(prefix)] == prefix {
c.Tags[k] = v.AsString()
}
return true
})
return c
}

func (c *ComponentData) withVersion(attrs *pcommon.Map, key string) *ComponentData {
value, ok := attrs.Get(key)
if ok {
Expand Down
87 changes: 87 additions & 0 deletions exporter/ststopologyexporter/internal/topology_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package internal

import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)

func TestTopology_addResource(t *testing.T) {
collection := NewCollection()
attrs := pcommon.NewMap()
attrs.PutStr("service.name", "demo 1")
attrs.PutStr("service.namespace", "demo")
attrs.PutStr("telemetry.sdk.language", "go")
attrs.PutStr("Resource Attributes 1", "value1")
collection.AddResource(&attrs)

components := collection.GetComponents()
require.Equal(t, []*Component{
{
ExternalId: "urn:opentelemetry:namespace/demo:service/demo 1",
Type: ComponentType{
Name: "service",
},
Data: &ComponentData{
Name: "demo 1",
Version: "",
Layer: "urn:stackpack:common:layer:services",
Domain: "",
Environment: "",
Tags: map[string]string{
"service.namespace": "demo",
"telemetry.sdk.language": "go",
},
},
},
{
ExternalId: "urn:opentelemetry:namespace/demo:service/demo 1:serviceInstance/demo 1",
Type: ComponentType{
Name: "service-instance",
},
Data: &ComponentData{
Name: "",
Version: "",
Layer: "urn:stackpack:common:layer:containers",
Domain: "",
Environment: "",
Tags: map[string]string{
"Resource Attributes 1": "value1",
"service.name": "demo 1",
"service.namespace": "demo",
"telemetry.sdk.language": "go",
},
},
},
{
ExternalId: "urn:opentelemetry:namespace/demo",
Type: ComponentType{
Name: "namespace",
},
Data: &ComponentData{
Name: "demo",
Version: "",
Layer: "urn:stackpack:common:layer:applications",
Domain: "",
Environment: "",
Tags: map[string]string{},
},
},
}, components)

relations := collection.GetRelations()
require.Equal(t, []*Relation{
{
ExternalId: "urn:opentelemetry:namespace/demo:service/demo 1-urn:opentelemetry:namespace/demo:service/demo 1:serviceInstance/demo 1",
SourceId: "urn:opentelemetry:namespace/demo:service/demo 1",
TargetId: "urn:opentelemetry:namespace/demo:service/demo 1:serviceInstance/demo 1",
Type: RelationType{
Name: "provided by",
},
Data: &RelationData{
Tags: map[string]string{},
},
},
}, relations)
}

0 comments on commit bc0b0fe

Please sign in to comment.