From 154aaa5d1e9bf4c334bc3312e90b35638711c9d9 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:27:33 -0700 Subject: [PATCH] [chore] move attributes func into separate file No functional change, just moving it into its own file to clean up the tracer config since the meter provider will also use attributes. Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/attributes.go | 24 ++++++++++++ service/telemetry/attributes_test.go | 56 ++++++++++++++++++++++++++++ service/telemetry/tracer.go | 18 --------- service/telemetry/tracer_test.go | 45 ---------------------- 4 files changed, 80 insertions(+), 63 deletions(-) create mode 100644 service/telemetry/attributes.go create mode 100644 service/telemetry/attributes_test.go diff --git a/service/telemetry/attributes.go b/service/telemetry/attributes.go new file mode 100644 index 00000000000..7b1b3b4ed99 --- /dev/null +++ b/service/telemetry/attributes.go @@ -0,0 +1,24 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry // import "go.opentelemetry.io/collector/service/telemetry" + +import semconv "go.opentelemetry.io/otel/semconv/v1.4.0" + +func attributes(set Settings, cfg Config) map[string]interface{} { + attrs := map[string]interface{}{ + string(semconv.ServiceNameKey): set.BuildInfo.Command, + string(semconv.ServiceVersionKey): set.BuildInfo.Version, + } + for k, v := range cfg.Resource { + if v != nil { + attrs[k] = *v + } + + // the new value is nil, delete the existing key + if _, ok := attrs[k]; ok && v == nil { + delete(attrs, k) + } + } + return attrs +} diff --git a/service/telemetry/attributes_test.go b/service/telemetry/attributes_test.go new file mode 100644 index 00000000000..630d48a1d7e --- /dev/null +++ b/service/telemetry/attributes_test.go @@ -0,0 +1,56 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry // import "go.opentelemetry.io/collector/service/telemetry" + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/service/telemetry/internal" +) + +func TestAttributes(t *testing.T) { + tests := []struct { + name string + cfg Config + buildInfo component.BuildInfo + wantAttributes map[string]interface{} + }{ + { + name: "no build info and no resource config", + cfg: Config{}, + wantAttributes: map[string]interface{}{"service.name": "", "service.version": ""}, + }, + { + name: "build info and no resource config", + cfg: Config{}, + buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, + wantAttributes: map[string]interface{}{"service.name": "otelcoltest", "service.version": "0.0.0-test"}, + }, + { + name: "no build info and resource config", + cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, + wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, + }, + { + name: "build info and resource config", + buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, + cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, + wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, + }, + { + name: "deleting a nil value", + buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, + cfg: Config{Resource: map[string]*string{"service.name": nil, "service.version": ptr("resource.version"), "test": ptr("test")}}, + wantAttributes: map[string]interface{}{"service.version": "resource.version", "test": "test"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + attrs := attributes(internal.Settings{BuildInfo: tt.buildInfo}, tt.cfg) + require.Equal(t, tt.wantAttributes, attrs) + }) + } +} diff --git a/service/telemetry/tracer.go b/service/telemetry/tracer.go index 11c735df625..ff17f296865 100644 --- a/service/telemetry/tracer.go +++ b/service/telemetry/tracer.go @@ -30,24 +30,6 @@ var ( errUnsupportedPropagator = errors.New("unsupported trace propagator") ) -func attributes(set Settings, cfg Config) map[string]interface{} { - attrs := map[string]interface{}{ - string(semconv.ServiceNameKey): set.BuildInfo.Command, - string(semconv.ServiceVersionKey): set.BuildInfo.Version, - } - for k, v := range cfg.Resource { - if v != nil { - attrs[k] = *v - } - - // the new value is nil, delete the existing key - if _, ok := attrs[k]; ok && v == nil { - delete(attrs, k) - } - } - return attrs -} - type noopNoContextTracer struct { embedded.Tracer } diff --git a/service/telemetry/tracer_test.go b/service/telemetry/tracer_test.go index 80184dac2e0..6c365351549 100644 --- a/service/telemetry/tracer_test.go +++ b/service/telemetry/tracer_test.go @@ -10,57 +10,12 @@ import ( "github.com/stretchr/testify/require" sdktrace "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/featuregate" "go.opentelemetry.io/collector/internal/globalgates" "go.opentelemetry.io/collector/service/telemetry/internal" ) -func TestAttributes(t *testing.T) { - tests := []struct { - name string - cfg Config - buildInfo component.BuildInfo - wantAttributes map[string]interface{} - }{ - { - name: "no build info and no resource config", - cfg: Config{}, - wantAttributes: map[string]interface{}{"service.name": "", "service.version": ""}, - }, - { - name: "build info and no resource config", - cfg: Config{}, - buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, - wantAttributes: map[string]interface{}{"service.name": "otelcoltest", "service.version": "0.0.0-test"}, - }, - { - name: "no build info and resource config", - cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, - wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, - }, - { - name: "build info and resource config", - buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, - cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, - wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, - }, - { - name: "deleting a nil value", - buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, - cfg: Config{Resource: map[string]*string{"service.name": nil, "service.version": ptr("resource.version"), "test": ptr("test")}}, - wantAttributes: map[string]interface{}{"service.version": "resource.version", "test": "test"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - attrs := attributes(internal.Settings{BuildInfo: tt.buildInfo}, tt.cfg) - require.Equal(t, tt.wantAttributes, attrs) - }) - } -} - func TestNewTracerProvider(t *testing.T) { tests := []struct { name string