Skip to content

Commit 3ecce47

Browse files
Add service.name resource attribute to the collector's own telemetry (#6152)
Resolves #6136 - The default service.name is set to BuildInfo.Command (equals to "otelcol" for this repo). - The service.name can be overridden by the end user via telemetry config setting.
1 parent 2829ed3 commit 3ecce47

File tree

3 files changed

+103
-21
lines changed

3 files changed

+103
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- Add AppendEmpty and EnsureCapacity method to primitive pdata slices (#6060)
6363
- Expose `AsRaw` and `FromRaw` `pcommon.Value` methods (#6090)
6464
- Convert `ValueTypeBytes` attributes in logging exporter (#6153)
65+
- service.name Resource attribute is added to Collector's own telemetry, defaults to the value of `BuildInfo.Command` and can be overridden in the config (#6152)
6566
- Updated how `telemetryInitializer` is created so it's instanced per Collector instance rather than global to the process (#6138)
6667

6768
## v0.60.0 Beta

service/telemetry.go

+33-21
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,8 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
117117

118118
logger.Info("Setting up own telemetry...")
119119

120-
// Construct telemetry attributes from resource attributes.
121-
telAttrs := map[string]string{}
122-
for k, v := range cfg.Resource {
123-
// nil value indicates that the attribute should not be included in the telemetry.
124-
if v != nil {
125-
telAttrs[k] = *v
126-
}
127-
}
128-
129-
if _, ok := cfg.Resource[semconv.AttributeServiceInstanceID]; !ok {
130-
// AttributeServiceInstanceID is not specified in the config. Auto-generate one.
131-
instanceUUID, _ := uuid.NewRandom()
132-
instanceID := instanceUUID.String()
133-
telAttrs[semconv.AttributeServiceInstanceID] = instanceID
134-
}
135-
136-
if _, ok := cfg.Resource[semconv.AttributeServiceVersion]; !ok {
137-
// AttributeServiceVersion is not specified in the config. Use the actual
138-
// build version.
139-
telAttrs[semconv.AttributeServiceVersion] = buildInfo.Version
140-
}
120+
// Construct telemetry attributes from build info and config's resource attributes.
121+
telAttrs := buildTelAttrs(buildInfo, cfg)
141122

142123
if tp, err := textMapPropagatorFromConfig(cfg.Traces.Propagators); err == nil {
143124
otel.SetTextMapPropagator(tp)
@@ -179,6 +160,37 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
179160
return nil
180161
}
181162

163+
func buildTelAttrs(buildInfo component.BuildInfo, cfg telemetry.Config) map[string]string {
164+
telAttrs := map[string]string{}
165+
166+
for k, v := range cfg.Resource {
167+
// nil value indicates that the attribute should not be included in the telemetry.
168+
if v != nil {
169+
telAttrs[k] = *v
170+
}
171+
}
172+
173+
if _, ok := cfg.Resource[semconv.AttributeServiceName]; !ok {
174+
// AttributeServiceName is not specified in the config. Use the default service name.
175+
telAttrs[semconv.AttributeServiceName] = buildInfo.Command
176+
}
177+
178+
if _, ok := cfg.Resource[semconv.AttributeServiceInstanceID]; !ok {
179+
// AttributeServiceInstanceID is not specified in the config. Auto-generate one.
180+
instanceUUID, _ := uuid.NewRandom()
181+
instanceID := instanceUUID.String()
182+
telAttrs[semconv.AttributeServiceInstanceID] = instanceID
183+
}
184+
185+
if _, ok := cfg.Resource[semconv.AttributeServiceVersion]; !ok {
186+
// AttributeServiceVersion is not specified in the config. Use the actual
187+
// build version.
188+
telAttrs[semconv.AttributeServiceVersion] = buildInfo.Version
189+
}
190+
191+
return telAttrs
192+
}
193+
182194
func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs map[string]string) (http.Handler, error) {
183195
tel.ocRegistry = ocmetric.NewRegistry()
184196
metricproducer.GlobalManager().AddProducer(tel.ocRegistry)

service/telemetry_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package service
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
"go.opentelemetry.io/collector/component"
23+
semconv "go.opentelemetry.io/collector/semconv/v1.5.0"
24+
"go.opentelemetry.io/collector/service/telemetry"
25+
)
26+
27+
func TestBuildTelAttrs(t *testing.T) {
28+
buildInfo := component.NewDefaultBuildInfo()
29+
30+
// Check default config
31+
cfg := telemetry.Config{}
32+
telAttrs := buildTelAttrs(buildInfo, cfg)
33+
34+
assert.Len(t, telAttrs, 3)
35+
assert.Equal(t, buildInfo.Command, telAttrs[semconv.AttributeServiceName])
36+
assert.Equal(t, buildInfo.Version, telAttrs[semconv.AttributeServiceVersion])
37+
38+
_, exists := telAttrs[semconv.AttributeServiceInstanceID]
39+
assert.True(t, exists)
40+
41+
// Check override by nil
42+
cfg = telemetry.Config{
43+
Resource: map[string]*string{
44+
semconv.AttributeServiceName: nil,
45+
semconv.AttributeServiceVersion: nil,
46+
semconv.AttributeServiceInstanceID: nil,
47+
},
48+
}
49+
telAttrs = buildTelAttrs(buildInfo, cfg)
50+
51+
// Attributes should not exist since we nil-ified all.
52+
assert.Len(t, telAttrs, 0)
53+
54+
// Check override values
55+
strPtr := func(v string) *string { return &v }
56+
cfg = telemetry.Config{
57+
Resource: map[string]*string{
58+
semconv.AttributeServiceName: strPtr("a"),
59+
semconv.AttributeServiceVersion: strPtr("b"),
60+
semconv.AttributeServiceInstanceID: strPtr("c"),
61+
},
62+
}
63+
telAttrs = buildTelAttrs(buildInfo, cfg)
64+
65+
assert.Len(t, telAttrs, 3)
66+
assert.Equal(t, "a", telAttrs[semconv.AttributeServiceName])
67+
assert.Equal(t, "b", telAttrs[semconv.AttributeServiceVersion])
68+
assert.Equal(t, "c", telAttrs[semconv.AttributeServiceInstanceID])
69+
}

0 commit comments

Comments
 (0)