forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_test.go
More file actions
276 lines (233 loc) · 8.78 KB
/
generator_test.go
File metadata and controls
276 lines (233 loc) · 8.78 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package generator
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"testing"
"time"
"github.com/go-kit/log"
"github.com/grafana/dskit/services"
"github.com/grafana/tempo/modules/generator/processor/spanmetrics"
"github.com/grafana/tempo/modules/generator/storage"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/pkg/tempopb"
common_v1 "github.com/grafana/tempo/pkg/tempopb/common/v1"
trace_v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/grafana/tempo/pkg/util/test"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
user1 = "user1"
user2 = "user2"
)
func TestGeneratorSpanMetrics_subprocessorConcurrency(t *testing.T) {
overridesFile := filepath.Join(t.TempDir(), "Overrides.yaml")
overridesConfig := overrides.Config{
Defaults: overrides.Overrides{
MetricsGenerator: overrides.MetricsGeneratorOverrides{
Processors: map[string]struct{}{
spanmetrics.Name: {},
},
CollectionInterval: 2 * time.Second,
},
},
PerTenantOverrideConfig: overridesFile,
PerTenantOverridePeriod: model.Duration(time.Second),
}
require.NoError(t, os.WriteFile(overridesFile, []byte(fmt.Sprintf(`
overrides:
%s:
metrics_generator:
collection_interval: 1s
processors:
- %s
`, user1, spanmetrics.Name)), os.ModePerm))
o, err := overrides.NewOverrides(overridesConfig, nil, prometheus.NewRegistry())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), o))
generatorConfig := &Config{}
generatorConfig.Storage.Path = t.TempDir()
generatorConfig.Ring.KVStore.Store = "inmemory"
generatorConfig.Processor.SpanMetrics.RegisterFlagsAndApplyDefaults("", nil)
g, err := New(generatorConfig, o, prometheus.NewRegistry(), nil, newTestLogger(t))
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), g))
t.Cleanup(func() {
require.NoError(t, services.StopAndAwaitTerminated(context.Background(), o))
require.NoError(t, services.StopAndAwaitTerminated(context.Background(), g))
})
allSubprocessors := map[spanmetrics.Subprocessor]bool{spanmetrics.Count: true, spanmetrics.Latency: true, spanmetrics.Size: true}
// All subprocessors should be enabled for user1
instance1, err := g.getOrCreateInstance(user1)
require.NoError(t, err)
verifySubprocessors(t, instance1, allSubprocessors)
// All subprocessors should be enabled for user2
instance2, err := g.getOrCreateInstance(user2)
require.NoError(t, err)
verifySubprocessors(t, instance2, allSubprocessors)
// Change overrides for user1
require.NoError(t, os.WriteFile(overridesFile, []byte(fmt.Sprintf(`
overrides:
%s:
metrics_generator:
collection_interval: 1s
processors:
- %s
`, user1, spanmetrics.Count.String())), os.ModePerm))
time.Sleep(15 * time.Second) // Wait for overrides to be applied. Reload is hardcoded to 10s :(
// Only Count should be enabled for user1
instance1, err = g.getOrCreateInstance(user1)
require.NoError(t, err)
verifySubprocessors(t, instance1, map[spanmetrics.Subprocessor]bool{spanmetrics.Count: true, spanmetrics.Latency: false, spanmetrics.Size: false})
// All subprocessors should be enabled for user2
instance2, err = g.getOrCreateInstance(user2)
require.NoError(t, err)
verifySubprocessors(t, instance2, allSubprocessors)
}
func verifySubprocessors(t *testing.T, instance *instance, expected map[spanmetrics.Subprocessor]bool) {
instance.processorsMtx.RLock()
defer instance.processorsMtx.RUnlock()
require.Len(t, instance.processors, 1)
processor, ok := instance.processors[spanmetrics.Name]
require.True(t, ok)
require.Equal(t, len(processor.(*spanmetrics.Processor).Cfg.Subprocessors), len(expected))
cfg := processor.(*spanmetrics.Processor).Cfg
for sub, enabled := range expected {
assert.Equal(t, enabled, cfg.Subprocessors[sub])
}
}
var _ log.Logger = (*testLogger)(nil)
type testLogger struct {
t *testing.T
}
func newTestLogger(t *testing.T) log.Logger {
return testLogger{t: t}
}
func (l testLogger) Log(keyvals ...interface{}) error {
l.t.Log(keyvals...)
return nil
}
func BenchmarkPushSpans(b *testing.B) {
var (
tenant = "test-tenant"
reg = prometheus.NewRegistry()
ctx = context.Background()
log = log.NewNopLogger()
cfg = &Config{}
walcfg = &storage.Config{
Path: b.TempDir(),
}
o = &mockOverrides{
processors: map[string]struct{}{
"span-metrics": {},
"service-graphs": {},
},
spanMetricsEnableTargetInfo: true,
spanMetricsTargetInfoExcludedDimensions: []string{"excluded}"},
}
)
cfg.RegisterFlagsAndApplyDefaults("", &flag.FlagSet{})
wal, err := storage.New(walcfg, o, tenant, reg, log)
require.NoError(b, err)
inst, err := newInstance(cfg, tenant, o, wal, reg, log, nil, nil)
require.NoError(b, err)
defer inst.shutdown()
req := &tempopb.PushSpansRequest{
Batches: []*trace_v1.ResourceSpans{
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
},
}
// Add more resource attributes to get closer to real data
// Add integer to increase cardinality.
// Currently this is about 80 active series
// TODO - Get more series
for i, b := range req.Batches {
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
{Key: "k8s.cluster.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.namespace.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.node.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.pod.ip", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.pod.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "excluded", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
}...)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
inst.pushSpans(ctx, req)
}
b.StopTimer()
runtime.GC()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
b.ReportMetric(float64(mem.HeapInuse), "heap_in_use")
}
func BenchmarkCollect(b *testing.B) {
var (
tenant = "test-tenant"
reg = prometheus.NewRegistry()
ctx = context.Background()
log = log.NewNopLogger()
cfg = &Config{}
walcfg = &storage.Config{
Path: b.TempDir(),
}
o = &mockOverrides{
processors: map[string]struct{}{
"span-metrics": {},
"service-graphs": {},
},
spanMetricsDimensions: []string{"k8s.cluster.name", "k8s.namespace.name"},
spanMetricsEnableTargetInfo: true,
spanMetricsTargetInfoExcludedDimensions: []string{"excluded}"},
nativeHistograms: overrides.HistogramMethodBoth,
}
)
cfg.RegisterFlagsAndApplyDefaults("", &flag.FlagSet{})
wal, err := storage.New(walcfg, o, tenant, reg, log)
require.NoError(b, err)
inst, err := newInstance(cfg, tenant, o, wal, reg, log, nil, nil)
require.NoError(b, err)
defer inst.shutdown()
req := &tempopb.PushSpansRequest{
Batches: []*trace_v1.ResourceSpans{
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
test.MakeBatch(100, nil),
},
}
// Add more resource attributes to get closer to real data
// Add integer to increase cardinality.
// Currently this is about 80 active series
// TODO - Get more series
for i, b := range req.Batches {
b.Resource.Attributes = append(b.Resource.Attributes, []*common_v1.KeyValue{
{Key: "k8s.cluster.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.namespace.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.node.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.pod.ip", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "k8s.pod.name", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
{Key: "excluded", Value: &common_v1.AnyValue{Value: &common_v1.AnyValue_StringValue{StringValue: "test" + strconv.Itoa(i)}}},
}...)
}
inst.pushSpans(ctx, req)
b.ResetTimer()
for i := 0; i < b.N; i++ {
inst.registry.CollectMetrics(ctx)
}
b.StopTimer()
runtime.GC()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
b.ReportMetric(float64(mem.HeapInuse), "heap_in_use")
}