|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package opentelemetry |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.opentelemetry.io/collector/confmap" |
| 12 | +) |
| 13 | + |
| 14 | +func TestResourceAttributesProcessor(t *testing.T) { |
| 15 | + // nil conf -> no processor |
| 16 | + assert.Nil(t, resourceAttributesProcessor(nil)) |
| 17 | + |
| 18 | + // no resource_attributes key -> no processor |
| 19 | + conf := confmap.NewFromStringMap(map[string]interface{}{ |
| 20 | + "opentelemetry": map[string]interface{}{"collect": map[string]interface{}{"otlp": map[string]interface{}{}}}, |
| 21 | + }) |
| 22 | + assert.Nil(t, resourceAttributesProcessor(conf)) |
| 23 | + |
| 24 | + // empty map -> no processor |
| 25 | + conf = confmap.NewFromStringMap(map[string]interface{}{ |
| 26 | + "opentelemetry": map[string]interface{}{"resource_attributes": map[string]interface{}{}}, |
| 27 | + }) |
| 28 | + assert.Nil(t, resourceAttributesProcessor(conf)) |
| 29 | + |
| 30 | + // populated map -> processor present, Translate succeeds |
| 31 | + conf = confmap.NewFromStringMap(map[string]interface{}{ |
| 32 | + "opentelemetry": map[string]interface{}{"resource_attributes": map[string]interface{}{"team": "cloudwatch"}}, |
| 33 | + }) |
| 34 | + tr := resourceAttributesProcessor(conf) |
| 35 | + require.NotNil(t, tr) |
| 36 | + assert.Equal(t, "resource/opentelemetry", tr.ID().String()) |
| 37 | + cfg, err := tr.Translate(conf) |
| 38 | + require.NoError(t, err) |
| 39 | + assert.NotNil(t, cfg) |
| 40 | +} |
| 41 | + |
| 42 | +func TestResourceAttributesProcessor_ReservedKeyRejected(t *testing.T) { |
| 43 | + for _, key := range reservedResourceAttributeKeys { |
| 44 | + t.Run(key, func(t *testing.T) { |
| 45 | + conf := confmap.NewFromStringMap(map[string]interface{}{ |
| 46 | + "opentelemetry": map[string]interface{}{ |
| 47 | + "resource_attributes": map[string]interface{}{key: "/hijacked"}, |
| 48 | + }, |
| 49 | + }) |
| 50 | + tr := resourceAttributesProcessor(conf) |
| 51 | + require.NotNil(t, tr) |
| 52 | + cfg, err := tr.Translate(conf) |
| 53 | + require.Error(t, err) |
| 54 | + assert.Nil(t, cfg) |
| 55 | + assert.Contains(t, err.Error(), "reserved") |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestResourceAttributesProcessor_EmptyKeyRejected(t *testing.T) { |
| 61 | + conf := confmap.NewFromStringMap(map[string]interface{}{ |
| 62 | + "opentelemetry": map[string]interface{}{ |
| 63 | + "resource_attributes": map[string]interface{}{"": "value"}, |
| 64 | + }, |
| 65 | + }) |
| 66 | + tr := resourceAttributesProcessor(conf) |
| 67 | + require.NotNil(t, tr) |
| 68 | + cfg, err := tr.Translate(conf) |
| 69 | + require.Error(t, err) |
| 70 | + assert.Nil(t, cfg) |
| 71 | + assert.Contains(t, err.Error(), "must not be empty") |
| 72 | +} |
0 commit comments