Skip to content

Commit

Permalink
[enrichments] Add agent.version (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar authored Aug 6, 2024
1 parent a7aae79 commit d6d93bf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 16 deletions.
6 changes: 4 additions & 2 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Config struct {

// ResourceConfig configures the enrichment of resource attributes.
type ResourceConfig struct {
AgentName AttributeConfig `mapstructure:"agent_name"`
AgentName AttributeConfig `mapstructure:"agent_name"`
AgentVersion AttributeConfig `mapstructure:"agent_version"`
}

// ElasticTransactionConfig configures the enrichment attributes for the
Expand Down Expand Up @@ -56,7 +57,8 @@ type AttributeConfig struct {
func Enabled() Config {
return Config{
Resource: ResourceConfig{
AgentName: AttributeConfig{Enabled: true},
AgentName: AttributeConfig{Enabled: true},
AgentVersion: AttributeConfig{Enabled: true},
},
Transaction: ElasticTransactionConfig{
Root: AttributeConfig{Enabled: true},
Expand Down
3 changes: 2 additions & 1 deletion enrichments/trace/internal/elastic/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package elastic

const (
// resource attributes
AttributeAgentName = "agent.name"
AttributeAgentName = "agent.name"
AttributeAgentVersion = "agent.version"

// span attributes
AttributeTransactionRoot = "transaction.root"
Expand Down
30 changes: 27 additions & 3 deletions enrichments/trace/internal/elastic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ func EnrichResource(resource pcommon.Resource, cfg config.Config) {
}

type resourceEnrichmentContext struct {
telemetrySDKName string
telemetrySDKLanguage string
telemetryDistroName string
telemetrySDKName string
telemetrySDKLanguage string
telemetrySDKVersion string
telemetryDistroName string
telemetryDistroVersion string
}

func (s *resourceEnrichmentContext) Enrich(resource pcommon.Resource, cfg config.ResourceConfig) {
Expand All @@ -44,15 +46,22 @@ func (s *resourceEnrichmentContext) Enrich(resource pcommon.Resource, cfg config
s.telemetrySDKName = v.Str()
case semconv.AttributeTelemetrySDKLanguage:
s.telemetrySDKLanguage = v.Str()
case semconv.AttributeTelemetrySDKVersion:
s.telemetrySDKVersion = v.Str()
case semconv.AttributeTelemetryDistroName:
s.telemetryDistroName = v.Str()
case semconv.AttributeTelemetryDistroVersion:
s.telemetryDistroVersion = v.Str()
}
return true
})

if cfg.AgentName.Enabled {
s.setAgentName(resource)
}
if cfg.AgentVersion.Enabled {
s.setAgentVersion(resource)
}
}

func (s *resourceEnrichmentContext) setAgentName(resource pcommon.Resource) {
Expand Down Expand Up @@ -81,3 +90,18 @@ func (s *resourceEnrichmentContext) setAgentName(resource pcommon.Resource) {
}
resource.Attributes().PutStr(AttributeAgentName, agentName)
}

func (s *resourceEnrichmentContext) setAgentVersion(resource pcommon.Resource) {
agentVersion := "unknown"
switch {
case s.telemetryDistroName != "":
// do not fallback to the Otel SDK version if we have a
// distro name available as this would only cause confusion
if s.telemetryDistroVersion != "" {
agentVersion = s.telemetryDistroVersion
}
case s.telemetrySDKVersion != "":
agentVersion = s.telemetrySDKVersion
}
resource.Attributes().PutStr(AttributeAgentVersion, agentVersion)
}
71 changes: 61 additions & 10 deletions enrichments/trace/internal/elastic/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,25 @@ func TestResourceEnrich(t *testing.T) {
input: pcommon.NewResource(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "otlp",
AttributeAgentName: "otlp",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdk_name_set",
name: "sdkname_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor",
AttributeAgentName: "customflavor",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdk_name_distro_set",
name: "sdkname_distro_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
Expand All @@ -69,11 +71,12 @@ func TestResourceEnrich(t *testing.T) {
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/unknown/elastic",
AttributeAgentName: "customflavor/unknown/elastic",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdk_name_distro_lang_set",
name: "sdkname_distro_lang_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
Expand All @@ -83,7 +86,8 @@ func TestResourceEnrich(t *testing.T) {
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/cpp/elastic",
AttributeAgentName: "customflavor/cpp/elastic",
AttributeAgentVersion: "unknown",
},
},
{
Expand All @@ -95,11 +99,12 @@ func TestResourceEnrich(t *testing.T) {
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "otlp/cpp",
AttributeAgentName: "otlp/cpp",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdk_name_lang_set",
name: "sdkname_lang_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
Expand All @@ -108,7 +113,53 @@ func TestResourceEnrich(t *testing.T) {
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/cpp",
AttributeAgentName: "customflavor/cpp",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdkname_sdkver_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetrySDKVersion, "9.999.9")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor",
AttributeAgentVersion: "9.999.9",
},
},
{
name: "sdkname_sdkver_distroname_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetrySDKVersion, "9.999.9")
res.Attributes().PutStr(semconv.AttributeTelemetryDistroName, "elastic")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/unknown/elastic",
AttributeAgentVersion: "unknown",
},
},
{
name: "sdkname_sdkver_distroname_distrover_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetrySDKVersion, "9.999.9")
res.Attributes().PutStr(semconv.AttributeTelemetryDistroName, "elastic")
res.Attributes().PutStr(semconv.AttributeTelemetryDistroVersion, "1.2.3")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/unknown/elastic",
AttributeAgentVersion: "1.2.3",
},
},
} {
Expand Down

0 comments on commit d6d93bf

Please sign in to comment.