Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enrichments] Add agent name resource attr enrichment #66

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
9 changes: 9 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ package config

// Config configures the enrichment attributes produced.
type Config struct {
Resource ResourceConfig `mapstructure:"resource"`
Transaction ElasticTransactionConfig `mapstructure:"elastic_transaction"`
Span ElasticSpanConfig `mapstructure:"elastic_span"`
}

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

// ElasticTransactionConfig configures the enrichment attributes for the
// spans which are identified as elastic transaction.
type ElasticTransactionConfig struct {
Expand All @@ -49,6 +55,9 @@ type AttributeConfig struct {
// Enabled returns a config with all default enrichments enabled.
func Enabled() Config {
return Config{
Resource: ResourceConfig{
AgentName: AttributeConfig{Enabled: true},
},
Transaction: ElasticTransactionConfig{
Root: AttributeConfig{Enabled: true},
Name: AttributeConfig{Enabled: true},
Expand Down
1 change: 1 addition & 0 deletions enrichments/trace/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

func TestEnabled(t *testing.T) {
config := Enabled()
assertAllEnabled(t, reflect.ValueOf(config.Resource))
assertAllEnabled(t, reflect.ValueOf(config.Transaction))
assertAllEnabled(t, reflect.ValueOf(config.Span))
}
Expand Down
4 changes: 4 additions & 0 deletions enrichments/trace/internal/elastic/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
package elastic

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

// span attributes
AttributeTransactionRoot = "transaction.root"
AttributeTransactionName = "transaction.name"
AttributeTransactionType = "transaction.type"
Expand Down
83 changes: 83 additions & 0 deletions enrichments/trace/internal/elastic/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package elastic

import (
"fmt"

"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.22.0"
)

// EnrichResource derives and adds Elastic specific resource attributes.
func EnrichResource(resource pcommon.Resource, cfg config.Config) {
var c resourceEnrichmentContext
c.Enrich(resource, cfg.Resource)
}

type resourceEnrichmentContext struct {
telemetrySDKName string
telemetrySDKLanguage string
telemetryDistroName string
}

func (s *resourceEnrichmentContext) Enrich(resource pcommon.Resource, cfg config.ResourceConfig) {
resource.Attributes().Range(func(k string, v pcommon.Value) bool {
switch k {
case semconv.AttributeTelemetrySDKName:
s.telemetrySDKName = v.Str()
case semconv.AttributeTelemetrySDKLanguage:
s.telemetrySDKLanguage = v.Str()
case semconv.AttributeTelemetryDistroName:
s.telemetryDistroName = v.Str()
}
return true
})

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

func (s *resourceEnrichmentContext) setAgentName(resource pcommon.Resource) {
agentName := "otlp"
if s.telemetrySDKName != "" {
agentName = s.telemetrySDKName
}
switch {
case s.telemetryDistroName != "":
agentLang := "unknown"
if s.telemetrySDKLanguage != "" {
agentLang = s.telemetrySDKLanguage
}
agentName = fmt.Sprintf(
"%s/%s/%s",
agentName,
agentLang,
s.telemetryDistroName,
)
case s.telemetrySDKLanguage != "":
agentName = fmt.Sprintf(
"%s/%s",
agentName,
s.telemetrySDKLanguage,
)
}
resource.Attributes().PutStr(AttributeAgentName, agentName)
}
130 changes: 130 additions & 0 deletions enrichments/trace/internal/elastic/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package elastic

import (
"testing"

"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.25.0"
)

func TestResourceEnrich(t *testing.T) {
for _, tc := range []struct {
name string
input pcommon.Resource
config config.ResourceConfig
enrichedAttrs map[string]any
}{
{
name: "all_disabled",
input: pcommon.NewResource(),
enrichedAttrs: map[string]any{},
},
{
name: "empty",
input: pcommon.NewResource(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "otlp",
},
},
{
name: "sdk_name_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",
},
},
{
name: "sdk_name_distro_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetryDistroName, "elastic")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/unknown/elastic",
},
},
{
name: "sdk_name_distro_lang_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetrySDKLanguage, "cpp")
res.Attributes().PutStr(semconv.AttributeTelemetryDistroName, "elastic")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/cpp/elastic",
},
},
{
name: "lang_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKLanguage, "cpp")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "otlp/cpp",
},
},
{
name: "sdk_name_lang_set",
input: func() pcommon.Resource {
res := pcommon.NewResource()
res.Attributes().PutStr(semconv.AttributeTelemetrySDKName, "customflavor")
res.Attributes().PutStr(semconv.AttributeTelemetrySDKLanguage, "cpp")
return res
}(),
config: config.Enabled().Resource,
enrichedAttrs: map[string]any{
AttributeAgentName: "customflavor/cpp",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
// Merge existing resource attrs with the attrs added
// by enrichment to get the expected attributes.
expectedAttrs := tc.input.Attributes().AsRaw()
for k, v := range tc.enrichedAttrs {
expectedAttrs[k] = v
}

EnrichResource(tc.input, config.Config{
Resource: tc.config,
})

assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw()))
})
}
}
1 change: 1 addition & 0 deletions enrichments/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (e *Enricher) Enrich(pt ptrace.Traces) {
resSpans := pt.ResourceSpans()
for i := 0; i < resSpans.Len(); i++ {
resSpan := resSpans.At(i)
elastic.EnrichResource(resSpan.Resource(), e.Config)
scopeSpans := resSpan.ScopeSpans()
for j := 0; j < scopeSpans.Len(); j++ {
scopeSpan := scopeSpans.At(j)
Expand Down