@@ -12,6 +12,9 @@ import (
12
12
"time"
13
13
14
14
"github.com/stretchr/testify/assert"
15
+ "go.opentelemetry.io/collector/pdata/pcommon"
16
+ "go.opentelemetry.io/collector/pdata/ptrace"
17
+ conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
15
18
16
19
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/correlations"
17
20
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/log"
@@ -26,17 +29,15 @@ func advanceTime(a *ActiveServiceTracker, minutes int64) {
26
29
a .timeNow = func () time.Time { return newNow }
27
30
}
28
31
29
- // mergeStringMaps merges n maps with a later map's keys overriding earlier maps
30
- func mergeStringMaps (maps ... map [string ]string ) map [string ]string {
31
- ret := map [string ]string {}
32
-
32
+ // newResourceWithAttrs creates a new resource with the given attributes.
33
+ func newResourceWithAttrs (maps ... map [string ]string ) pcommon.Resource {
34
+ res := pcommon .NewResource ()
33
35
for _ , m := range maps {
34
36
for k , v := range m {
35
- ret [ k ] = v
37
+ res . Attributes (). PutStr ( k , v )
36
38
}
37
39
}
38
-
39
- return ret
40
+ return res
40
41
}
41
42
42
43
func TestExpiration (t * testing.T ) {
@@ -46,32 +47,24 @@ func TestExpiration(t *testing.T) {
46
47
a := New (log .Nil , 5 * time .Minute , correlationClient , hostIDDims , DefaultDimsToSyncSource )
47
48
setTime (a , time .Unix (100 , 0 ))
48
49
49
- a .AddSpansGeneric (context .Background (), fakeSpanList {
50
- {
51
- serviceName : "one" ,
52
- tags : mergeStringMaps (hostIDDims , map [string ]string {"environment" : "environment1" }),
53
- },
54
- {
55
- serviceName : "two" ,
56
- tags : mergeStringMaps (hostIDDims , map [string ]string {"environment" : "environment2" }),
57
- },
58
- {
59
- serviceName : "three" ,
60
- tags : mergeStringMaps (hostIDDims , map [string ]string {"environment" : "environment3" }),
61
- },
62
- })
50
+ fakeTraces := ptrace .NewTraces ()
51
+ newResourceWithAttrs (hostIDDims , map [string ]string {conventions .AttributeServiceName : "one" , "environment" : "environment1" }).
52
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
53
+ newResourceWithAttrs (hostIDDims , map [string ]string {conventions .AttributeServiceName : "two" , "environment" : "environment2" }).
54
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
55
+ newResourceWithAttrs (hostIDDims , map [string ]string {conventions .AttributeServiceName : "three" , "environment" : "environment3" }).
56
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
57
+ a .ProcessTraces (context .Background (), fakeTraces )
63
58
64
59
assert .Equal (t , int64 (3 ), a .hostServiceCache .ActiveCount , "activeServiceCount is not properly tracked" )
65
60
assert .Equal (t , int64 (3 ), a .hostEnvironmentCache .ActiveCount , "activeEnvironmentCount is not properly tracked" )
66
61
67
62
advanceTime (a , 4 )
68
63
69
- a .AddSpansGeneric (context .Background (), fakeSpanList {
70
- {
71
- serviceName : "two" ,
72
- tags : mergeStringMaps (hostIDDims , map [string ]string {"environment" : "environment2" }),
73
- },
74
- })
64
+ fakeTraces = ptrace .NewTraces ()
65
+ newResourceWithAttrs (hostIDDims , map [string ]string {conventions .AttributeServiceName : "two" , "environment" : "environment2" }).
66
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
67
+ a .ProcessTraces (context .Background (), fakeTraces )
75
68
76
69
advanceTime (a , 2 )
77
70
a .Purge ()
@@ -148,11 +141,12 @@ func TestCorrelationEmptyEnvironment(t *testing.T) {
148
141
a := New (log .Nil , 5 * time .Minute , correlationClient , hostIDDims , DefaultDimsToSyncSource )
149
142
wg .Wait () // wait for the initial fetch of hostIDDims to complete
150
143
151
- a .AddSpansGeneric (context .Background (), fakeSpanList {
152
- {tags : mergeStringMaps (hostIDDims , containerLevelIDDims )},
153
- {tags : mergeStringMaps (hostIDDims , containerLevelIDDims )},
154
- {tags : mergeStringMaps (hostIDDims , containerLevelIDDims )},
155
- })
144
+ fakeTraces := ptrace .NewTraces ()
145
+ fakeResource := newResourceWithAttrs (hostIDDims , containerLevelIDDims )
146
+ fakeResource .CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
147
+ fakeResource .CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
148
+ fakeResource .CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
149
+ a .ProcessTraces (context .Background (), fakeTraces )
156
150
157
151
cors := correlationClient .getCorrelations ()
158
152
assert .Equal (t , 4 , len (cors ), "expected 4 correlations to be made" )
@@ -194,20 +188,14 @@ func TestCorrelationUpdates(t *testing.T) {
194
188
195
189
setTime (a , time .Unix (100 , 0 ))
196
190
197
- a .AddSpansGeneric (context .Background (), fakeSpanList {
198
- {
199
- serviceName : "one" ,
200
- tags : mergeStringMaps (hostIDDims , mergeStringMaps (containerLevelIDDims , map [string ]string {"environment" : "environment1" })),
201
- },
202
- {
203
- serviceName : "two" ,
204
- tags : mergeStringMaps (hostIDDims , mergeStringMaps (containerLevelIDDims , map [string ]string {"environment" : "environment2" })),
205
- },
206
- {
207
- serviceName : "three" ,
208
- tags : mergeStringMaps (hostIDDims , mergeStringMaps (containerLevelIDDims , map [string ]string {"environment" : "environment3" })),
209
- },
210
- })
191
+ fakeTraces := ptrace .NewTraces ()
192
+ newResourceWithAttrs (containerLevelIDDims , map [string ]string {conventions .AttributeServiceName : "one" , "environment" : "environment1" }).
193
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
194
+ newResourceWithAttrs (containerLevelIDDims , map [string ]string {conventions .AttributeServiceName : "two" , "environment" : "environment2" }).
195
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
196
+ newResourceWithAttrs (containerLevelIDDims , map [string ]string {conventions .AttributeServiceName : "three" , "environment" : "environment3" }).
197
+ CopyTo (fakeTraces .ResourceSpans ().AppendEmpty ().Resource ())
198
+ a .ProcessTraces (context .Background (), fakeTraces )
211
199
212
200
assert .Equal (t , int64 (3 ), a .hostServiceCache .ActiveCount , "activeServiceCount is not properly tracked" )
213
201
assert .Equal (t , int64 (3 ), a .hostEnvironmentCache .ActiveCount , "activeEnvironmentCount is not properly tracked" )
0 commit comments