|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package schemaprocessor |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "go.opentelemetry.io/collector/pdata/plog" |
| 12 | +) |
| 13 | + |
| 14 | +func TestLogs_RenameAttributes(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + in plog.Logs |
| 20 | + out plog.Logs |
| 21 | + transformations string |
| 22 | + targetVersion string |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "one_version_downgrade", |
| 26 | + in: func() plog.Logs { |
| 27 | + in := plog.NewLogs() |
| 28 | + in.ResourceLogs().AppendEmpty() |
| 29 | + in.ResourceLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.9.0") |
| 30 | + in.ResourceLogs().At(0).Resource().Attributes().PutStr("new.resource.name", "test-cluster") |
| 31 | + in.ResourceLogs().At(0).ScopeLogs().AppendEmpty() |
| 32 | + in.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().AppendEmpty() |
| 33 | + in.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("new.attr.name", "my-att-cluster") |
| 34 | + return in |
| 35 | + }(), |
| 36 | + out: func() plog.Logs { |
| 37 | + out := plog.NewLogs() |
| 38 | + out.ResourceLogs().AppendEmpty() |
| 39 | + out.ResourceLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.8.0") |
| 40 | + out.ResourceLogs().At(0).Resource().Attributes().PutStr("old.resource.name", "test-cluster") |
| 41 | + out.ResourceLogs().At(0).ScopeLogs().AppendEmpty() |
| 42 | + out.ResourceLogs().At(0).ScopeLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.8.0") |
| 43 | + out.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().AppendEmpty() |
| 44 | + out.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("old.attr.name", "my-att-cluster") |
| 45 | + |
| 46 | + return out |
| 47 | + }(), |
| 48 | + transformations: ` |
| 49 | + 1.9.0: |
| 50 | + all: |
| 51 | + changes: |
| 52 | + - rename_attributes: |
| 53 | + attribute_map: |
| 54 | + old.resource.name: new.resource.name |
| 55 | + logs: |
| 56 | + changes: |
| 57 | + - rename_attributes: |
| 58 | + attribute_map: |
| 59 | + old.attr.name: new.attr.name |
| 60 | + 1.8.0:`, |
| 61 | + targetVersion: "1.8.0", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "one_version_upgrade", |
| 65 | + in: func() plog.Logs { |
| 66 | + in := plog.NewLogs() |
| 67 | + in.ResourceLogs().AppendEmpty() |
| 68 | + in.ResourceLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.8.0") |
| 69 | + in.ResourceLogs().At(0).Resource().Attributes().PutStr("old.resource.name", "test-cluster") |
| 70 | + in.ResourceLogs().At(0).ScopeLogs().AppendEmpty() |
| 71 | + in.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().AppendEmpty() |
| 72 | + in.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("old.attr.name", "my-att-cluster") |
| 73 | + |
| 74 | + return in |
| 75 | + }(), |
| 76 | + out: func() plog.Logs { |
| 77 | + out := plog.NewLogs() |
| 78 | + out.ResourceLogs().AppendEmpty() |
| 79 | + out.ResourceLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.9.0") |
| 80 | + out.ResourceLogs().At(0).Resource().Attributes().PutStr("new.resource.name", "test-cluster") |
| 81 | + out.ResourceLogs().At(0).ScopeLogs().AppendEmpty() |
| 82 | + out.ResourceLogs().At(0).ScopeLogs().At(0).SetSchemaUrl("http://opentelemetry.io/schemas/1.9.0") |
| 83 | + out.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().AppendEmpty() |
| 84 | + out.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().PutStr("new.attr.name", "my-att-cluster") |
| 85 | + return out |
| 86 | + }(), |
| 87 | + transformations: ` |
| 88 | + 1.9.0: |
| 89 | + all: |
| 90 | + changes: |
| 91 | + - rename_attributes: |
| 92 | + attribute_map: |
| 93 | + old.resource.name: new.resource.name |
| 94 | + logs: |
| 95 | + changes: |
| 96 | + - rename_attributes: |
| 97 | + attribute_map: |
| 98 | + old.attr.name: new.attr.name |
| 99 | + 1.8.0:`, |
| 100 | + targetVersion: "1.9.0", |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tt := range tests { |
| 105 | + t.Run(tt.name, func(t *testing.T) { |
| 106 | + pr := newTestSchemaProcessor(t, tt.transformations, tt.targetVersion) |
| 107 | + ctx := context.Background() |
| 108 | + out, err := pr.processLogs(ctx, tt.in) |
| 109 | + if err != nil { |
| 110 | + t.Errorf("Error while processing logs: %v", err) |
| 111 | + } |
| 112 | + assert.Equal(t, tt.out, out, "Logs transformation failed") |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments