|
| 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