Skip to content

Commit eac16d3

Browse files
authored
[chore][connector/routing] Refactor trace and metric routing by resource (#36098)
1 parent a9bc204 commit eac16d3

39 files changed

+4876
-40
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pmetricutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/pmetricutil"
5+
6+
import "go.opentelemetry.io/collector/pdata/pmetric"
7+
8+
// MoveResourcesIf calls f sequentially for each ResourceSpans present in the first pmetric.Metrics.
9+
// If f returns true, the element is removed from the first pmetric.Metrics and added to the second pmetric.Metrics.
10+
func MoveResourcesIf(from, to pmetric.Metrics, f func(pmetric.ResourceMetrics) bool) {
11+
from.ResourceMetrics().RemoveIf(func(rs pmetric.ResourceMetrics) bool {
12+
if !f(rs) {
13+
return false
14+
}
15+
rs.CopyTo(to.ResourceMetrics().AppendEmpty())
16+
return true
17+
})
18+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package pmetricutil_test
5+
6+
import (
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/collector/pdata/pmetric"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/pmetricutil"
15+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
16+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
17+
)
18+
19+
func TestMoveResourcesIf(t *testing.T) {
20+
testCases := []struct {
21+
name string
22+
condition func(pmetric.ResourceMetrics) bool
23+
}{
24+
{
25+
name: "move_none",
26+
condition: func(pmetric.ResourceMetrics) bool {
27+
return false
28+
},
29+
},
30+
{
31+
name: "move_all",
32+
condition: func(pmetric.ResourceMetrics) bool {
33+
return true
34+
},
35+
},
36+
{
37+
name: "move_one",
38+
condition: func(rl pmetric.ResourceMetrics) bool {
39+
rname, ok := rl.Resource().Attributes().Get("resourceName")
40+
return ok && rname.AsString() == "resourceA"
41+
},
42+
},
43+
{
44+
name: "move_to_preexisting",
45+
condition: func(rl pmetric.ResourceMetrics) bool {
46+
rname, ok := rl.Resource().Attributes().Get("resourceName")
47+
return ok && rname.AsString() == "resourceB"
48+
},
49+
},
50+
}
51+
52+
for _, tt := range testCases {
53+
t.Run(tt.name, func(t *testing.T) {
54+
// Load up a fresh copy of the input for each test, since it may be modified in place.
55+
from, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from.yaml"))
56+
require.NoError(t, err)
57+
58+
to, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to.yaml"))
59+
require.NoError(t, err)
60+
61+
fromModifed, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "from_modified.yaml"))
62+
require.NoError(t, err)
63+
64+
toModified, err := golden.ReadMetrics(filepath.Join("testdata", "resource", tt.name, "to_modified.yaml"))
65+
require.NoError(t, err)
66+
67+
pmetricutil.MoveResourcesIf(from, to, tt.condition)
68+
69+
assert.NoError(t, pmetrictest.CompareMetrics(fromModifed, from), "from not modified as expected")
70+
assert.NoError(t, pmetrictest.CompareMetrics(toModified, to), "to not as expected")
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)