/
tracker_test.go
210 lines (182 loc) · 8.59 KB
/
tracker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Originally copied from https://github.com/signalfx/signalfx-agent/blob/fbc24b0fdd3884bd0bbfbd69fe3c83f49d4c0b77/pkg/apm/tracetracker/tracker_test.go
package tracetracker
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/correlations"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/apm/log"
)
func setTime(a *ActiveServiceTracker, t time.Time) {
a.timeNow = func() time.Time { return t }
}
func advanceTime(a *ActiveServiceTracker, minutes int64) {
newNow := a.timeNow().Add(time.Duration(minutes) * time.Minute)
a.timeNow = func() time.Time { return newNow }
}
// newResourceWithAttrs creates a new resource with the given attributes.
func newResourceWithAttrs(maps ...map[string]string) pcommon.Resource {
res := pcommon.NewResource()
for _, m := range maps {
for k, v := range m {
res.Attributes().PutStr(k, v)
}
}
return res
}
func TestExpiration(t *testing.T) {
correlationClient := &correlationTestClient{}
hostIDDims := map[string]string{"host": "test", "AWSUniqueId": "randomAWSUniqueId"}
a := New(log.Nil, 5*time.Minute, correlationClient, hostIDDims, DefaultDimsToSyncSource)
setTime(a, time.Unix(100, 0))
fakeTraces := ptrace.NewTraces()
newResourceWithAttrs(hostIDDims, map[string]string{conventions.AttributeServiceName: "one", "environment": "environment1"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
newResourceWithAttrs(hostIDDims, map[string]string{conventions.AttributeServiceName: "two", "environment": "environment2"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
newResourceWithAttrs(hostIDDims, map[string]string{conventions.AttributeServiceName: "three", "environment": "environment3"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
a.ProcessTraces(context.Background(), fakeTraces)
assert.Equal(t, int64(3), a.hostServiceCache.ActiveCount, "activeServiceCount is not properly tracked")
assert.Equal(t, int64(3), a.hostEnvironmentCache.ActiveCount, "activeEnvironmentCount is not properly tracked")
advanceTime(a, 4)
fakeTraces = ptrace.NewTraces()
newResourceWithAttrs(hostIDDims, map[string]string{conventions.AttributeServiceName: "two", "environment": "environment2"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
a.ProcessTraces(context.Background(), fakeTraces)
advanceTime(a, 2)
a.Purge()
assert.Equal(t, int64(1), a.hostServiceCache.ActiveCount, "activeServiceCount is not properly tracked")
assert.Equal(t, int64(1), a.hostEnvironmentCache.ActiveCount, "activeEnvironmentCount is not properly tracked")
assert.Equal(t, int64(2), a.hostServiceCache.PurgedCount, "purgedServiceCount is not properly tracked")
assert.Equal(t, int64(2), a.hostEnvironmentCache.PurgedCount, "activeEnvironmentCount is not properly tracked")
}
type correlationTestClient struct {
sync.Mutex
cors []*correlations.Correlation
getPayload map[string]map[string][]string
getCallback func()
getCounter int64
deleteCounter int64
correlateCounter int64
}
func (c *correlationTestClient) Start() { /*no-op*/ }
func (c *correlationTestClient) Shutdown() { /*no-op*/ }
func (c *correlationTestClient) Get(_ string, dimValue string, cb correlations.SuccessfulGetCB) {
atomic.AddInt64(&c.getCounter, 1)
go func() {
cb(c.getPayload[dimValue])
if c.getCallback != nil {
c.getCallback()
}
}()
}
func (c *correlationTestClient) Correlate(cl *correlations.Correlation, cb correlations.CorrelateCB) {
c.Lock()
defer c.Unlock()
c.cors = append(c.cors, cl)
cb(cl, nil)
atomic.AddInt64(&c.correlateCounter, 1)
}
func (c *correlationTestClient) Delete(cl *correlations.Correlation, cb correlations.SuccessfulDeleteCB) {
c.Lock()
defer c.Unlock()
c.cors = append(c.cors, cl)
cb(cl)
atomic.AddInt64(&c.deleteCounter, 1)
}
func (c *correlationTestClient) getCorrelations() []*correlations.Correlation {
c.Lock()
defer c.Unlock()
return c.cors
}
var _ correlations.CorrelationClient = &correlationTestClient{}
func TestCorrelationEmptyEnvironment(t *testing.T) {
var wg sync.WaitGroup
correlationClient := &correlationTestClient{
getPayload: map[string]map[string][]string{
"testk8sPodUID": {
"sf_services": {"one"},
"sf_environments": {"environment1"},
},
"testContainerID": {
"sf_services": {"one"},
"sf_environments": {"environment1"},
},
},
getCallback: func() {
wg.Done()
},
}
hostIDDims := map[string]string{"host": "test", "AWSUniqueId": "randomAWSUniqueId"}
wg.Add(len(hostIDDims))
containerLevelIDDims := map[string]string{"kubernetes_pod_uid": "testk8sPodUID", "container_id": "testContainerID"}
a := New(log.Nil, 5*time.Minute, correlationClient, hostIDDims, DefaultDimsToSyncSource)
wg.Wait() // wait for the initial fetch of hostIDDims to complete
fakeTraces := ptrace.NewTraces()
fakeResource := newResourceWithAttrs(hostIDDims, containerLevelIDDims)
fakeResource.CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
fakeResource.CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
fakeResource.CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
a.ProcessTraces(context.Background(), fakeTraces)
cors := correlationClient.getCorrelations()
assert.Equal(t, 4, len(cors), "expected 4 correlations to be made")
for _, c := range cors {
assert.Contains(t, []string{"container_id", "kubernetes_pod_uid", "host", "AWSUniqueId"}, c.DimName)
assert.Contains(t, []string{"test", "randomAWSUniqueId", "testk8sPodUID", "testContainerID"}, c.DimValue)
assert.Equal(t, correlations.Type("environment"), c.Type)
assert.Equal(t, fallbackEnvironment, c.Value)
}
}
func TestCorrelationUpdates(t *testing.T) {
var wg sync.WaitGroup
correlationClient := &correlationTestClient{
getPayload: map[string]map[string][]string{
"test": {
"sf_services": {"one"},
"sf_environments": {"environment1"},
},
},
getCallback: func() {
wg.Done()
},
}
hostIDDims := map[string]string{"host": "test", "AWSUniqueId": "randomAWSUniqueId"}
wg.Add(len(hostIDDims))
containerLevelIDDims := map[string]string{"kubernetes_pod_uid": "testk8sPodUID", "container_id": "testContainerID"}
a := New(log.Nil, 5*time.Minute, correlationClient, hostIDDims, DefaultDimsToSyncSource)
wg.Wait()
assert.Equal(t, int64(1), a.hostServiceCache.ActiveCount, "activeServiceCount is not properly tracked")
assert.Equal(t, int64(1), a.hostEnvironmentCache.ActiveCount, "activeEnvironmentCount is not properly tracked")
advanceTime(a, 6)
a.Purge()
assert.Equal(t, int64(0), a.hostServiceCache.ActiveCount, "activeServiceCount is not properly tracked")
assert.Equal(t, int64(0), a.hostEnvironmentCache.ActiveCount, "activeEnvironmentCount is not properly tracked")
assert.Equal(t, int64(1), a.hostServiceCache.PurgedCount, "hostServiceCache purged count is not properly tracked")
assert.Equal(t, int64(1), a.hostEnvironmentCache.PurgedCount, "hostEnvironmentCache purged count is not properly tracked")
setTime(a, time.Unix(100, 0))
fakeTraces := ptrace.NewTraces()
newResourceWithAttrs(containerLevelIDDims, map[string]string{conventions.AttributeServiceName: "one", "environment": "environment1"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
newResourceWithAttrs(containerLevelIDDims, map[string]string{conventions.AttributeServiceName: "two", "environment": "environment2"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
newResourceWithAttrs(containerLevelIDDims, map[string]string{conventions.AttributeServiceName: "three", "environment": "environment3"}).
CopyTo(fakeTraces.ResourceSpans().AppendEmpty().Resource())
a.ProcessTraces(context.Background(), fakeTraces)
assert.Equal(t, int64(3), a.hostServiceCache.ActiveCount, "activeServiceCount is not properly tracked")
assert.Equal(t, int64(3), a.hostEnvironmentCache.ActiveCount, "activeEnvironmentCount is not properly tracked")
numEnvironments := 3
numServices := 3
numHostIDDimCorrelations := len(hostIDDims)*(numEnvironments+numServices) + 4 /* 4 deletes for service & environment fetched at startup */
numContainerLevelCorrelations := 2 * len(containerLevelIDDims)
totalExpectedCorrelations := numHostIDDimCorrelations + numContainerLevelCorrelations
assert.Equal(t, totalExpectedCorrelations, len(correlationClient.getCorrelations()), "# of correlation requests do not match")
}