|
| 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 | +} |
0 commit comments