Skip to content

Commit cc857ba

Browse files
authored
Add scope enrichment for service.framework.{name, version} (#73)
1 parent 8eda486 commit cc857ba

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

enrichments/trace/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package config
2020
// Config configures the enrichment attributes produced.
2121
type Config struct {
2222
Resource ResourceConfig `mapstructure:"resource"`
23+
Scope ScopeConfig `mapstructure:"scope"`
2324
Transaction ElasticTransactionConfig `mapstructure:"elastic_transaction"`
2425
Span ElasticSpanConfig `mapstructure:"elastic_span"`
2526
}
@@ -30,6 +31,12 @@ type ResourceConfig struct {
3031
AgentVersion AttributeConfig `mapstructure:"agent_version"`
3132
}
3233

34+
// ScopeConfig configures the enrichment of scope attributes.
35+
type ScopeConfig struct {
36+
ServiceFrameworkName AttributeConfig `mapstructure:"service_framework_name"`
37+
ServiceFrameworkVersion AttributeConfig `mapstructure:"service_framework_version"`
38+
}
39+
3340
// ElasticTransactionConfig configures the enrichment attributes for the
3441
// spans which are identified as elastic transaction.
3542
type ElasticTransactionConfig struct {
@@ -60,6 +67,10 @@ func Enabled() Config {
6067
AgentName: AttributeConfig{Enabled: true},
6168
AgentVersion: AttributeConfig{Enabled: true},
6269
},
70+
Scope: ScopeConfig{
71+
ServiceFrameworkName: AttributeConfig{Enabled: true},
72+
ServiceFrameworkVersion: AttributeConfig{Enabled: true},
73+
},
6374
Transaction: ElasticTransactionConfig{
6475
Root: AttributeConfig{Enabled: true},
6576
Name: AttributeConfig{Enabled: true},

enrichments/trace/config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
func TestEnabled(t *testing.T) {
2828
config := Enabled()
2929
assertAllEnabled(t, reflect.ValueOf(config.Resource))
30+
assertAllEnabled(t, reflect.ValueOf(config.Scope))
3031
assertAllEnabled(t, reflect.ValueOf(config.Transaction))
3132
assertAllEnabled(t, reflect.ValueOf(config.Span))
3233
}

enrichments/trace/internal/elastic/attributes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const (
2222
AttributeAgentName = "agent.name"
2323
AttributeAgentVersion = "agent.version"
2424

25+
// scope attributes
26+
AttributeServiceFrameworkName = "service.framework.name"
27+
AttributeServiceFrameworkVersion = "service.framework.version"
28+
2529
// span attributes
2630
AttributeTransactionRoot = "transaction.root"
2731
AttributeTransactionName = "transaction.name"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package elastic
19+
20+
import (
21+
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
22+
"go.opentelemetry.io/collector/pdata/pcommon"
23+
)
24+
25+
// EnrichScope derives and adds Elastic specific scope attributes.
26+
func EnrichScope(scope pcommon.InstrumentationScope, cfg config.Config) {
27+
attrs := scope.Attributes()
28+
if cfg.Scope.ServiceFrameworkName.Enabled {
29+
if name := scope.Name(); name != "" {
30+
attrs.PutStr(AttributeServiceFrameworkName, name)
31+
attrs.PutStr(AttributeServiceFrameworkVersion, scope.Version())
32+
}
33+
}
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package elastic
19+
20+
import (
21+
"testing"
22+
23+
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
24+
"github.com/google/go-cmp/cmp"
25+
"github.com/stretchr/testify/assert"
26+
"go.opentelemetry.io/collector/pdata/pcommon"
27+
)
28+
29+
func TestScopeEnrich(t *testing.T) {
30+
for _, tc := range []struct {
31+
name string
32+
input pcommon.InstrumentationScope
33+
config config.ScopeConfig
34+
enrichedAttrs map[string]any
35+
}{
36+
{
37+
name: "all_disabled",
38+
input: pcommon.NewInstrumentationScope(),
39+
enrichedAttrs: map[string]any{},
40+
},
41+
{
42+
name: "with_scope_name",
43+
input: func() pcommon.InstrumentationScope {
44+
scope := pcommon.NewInstrumentationScope()
45+
scope.SetName("test")
46+
return scope
47+
}(),
48+
config: config.Enabled().Scope,
49+
enrichedAttrs: map[string]any{
50+
AttributeServiceFrameworkName: "test",
51+
AttributeServiceFrameworkVersion: "",
52+
},
53+
},
54+
{
55+
name: "with_scope_name_version",
56+
input: func() pcommon.InstrumentationScope {
57+
scope := pcommon.NewInstrumentationScope()
58+
scope.SetName("test")
59+
scope.SetVersion("v1.0.0")
60+
return scope
61+
}(),
62+
config: config.Enabled().Scope,
63+
enrichedAttrs: map[string]any{
64+
AttributeServiceFrameworkName: "test",
65+
AttributeServiceFrameworkVersion: "v1.0.0",
66+
},
67+
},
68+
} {
69+
t.Run(tc.name, func(t *testing.T) {
70+
// Merge existing resource attrs with the attrs added
71+
// by enrichment to get the expected attributes.
72+
expectedAttrs := tc.input.Attributes().AsRaw()
73+
for k, v := range tc.enrichedAttrs {
74+
expectedAttrs[k] = v
75+
}
76+
77+
EnrichScope(tc.input, config.Config{
78+
Scope: tc.config,
79+
})
80+
81+
assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw()))
82+
})
83+
}
84+
}

enrichments/trace/trace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (e *Enricher) Enrich(pt ptrace.Traces) {
4848
scopeSpans := resSpan.ScopeSpans()
4949
for j := 0; j < scopeSpans.Len(); j++ {
5050
scopeSpan := scopeSpans.At(j)
51+
elastic.EnrichScope(scopeSpan.Scope(), e.Config)
5152
spans := scopeSpan.Spans()
5253
for k := 0; k < spans.Len(); k++ {
5354
elastic.EnrichSpan(spans.At(k), e.Config)

0 commit comments

Comments
 (0)