Skip to content

Commit 696e914

Browse files
Add service.name resource attribute to the collector's own telemetry
Resolves open-telemetry#6136 - The default service.name is set to "io.opentelemetry.collector". I think this is a good choice since it is unambiguous and is still short enough to be readable. This also matches the OpAMP recommendations: https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agentdescriptionidentifying_attributes - The service.name can be overridden by the end user via telemetry config setting.
1 parent 65ea4f0 commit 696e914

File tree

4 files changed

+109
-21
lines changed

4 files changed

+109
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
- Add AppendEmpty and EnsureCapacity method to primitive pdata slices (#6060)
4141
- Expose `AsRaw` and `FromRaw` `pcommon.Value` methods (#6090)
42+
- service.name Resource attribute is added to Collector's own telemetry and defaults to "io.opentelemetry.collector" (#6152)
4243

4344
## v0.60.0 Beta
4445

cmd/otelcorecol/builder-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dist:
44
description: Local OpenTelemetry Collector binary, testing only.
55
version: 0.60.0-dev
66
otelcol_version: 0.60.0
7+
service_name: io.opentelemetry.collector
78

89
receivers:
910
- import: go.opentelemetry.io/collector/receiver/otlpreceiver

service/telemetry.go

+38-21
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const (
6060
// supported trace propagators
6161
traceContextPropagator = "tracecontext"
6262
b3Propagator = "b3"
63+
64+
// Default value of "service.name" attribute in the telemetry
65+
// that the Collector produces about itself. This can be overridden
66+
// by the end user via telemetry.Config.Resource setting.
67+
defaultServiceName = "io.opentelemetry.collector"
6368
)
6469

6570
var (
@@ -112,27 +117,8 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
112117

113118
logger.Info("Setting up own telemetry...")
114119

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

137123
if tp, err := textMapPropagatorFromConfig(cfg.Traces.Propagators); err == nil {
138124
otel.SetTextMapPropagator(tp)
@@ -174,6 +160,37 @@ func (tel *telemetryInitializer) initOnce(buildInfo component.BuildInfo, logger
174160
return nil
175161
}
176162

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 Service name.
175+
telAttrs[semconv.AttributeServiceName] = defaultServiceName
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+
177194
func (tel *telemetryInitializer) initOpenCensus(cfg telemetry.Config, telAttrs map[string]string) (http.Handler, error) {
178195
tel.ocRegistry = ocmetric.NewRegistry()
179196
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, defaultServiceName, 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)