-
Notifications
You must be signed in to change notification settings - Fork 419
/
telemetrytest.go
94 lines (82 loc) · 3.09 KB
/
telemetrytest.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
// Package telemetrytest provides a mock implementation of the telemetry client for testing purposes
package telemetrytest
import (
"sync"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"github.com/stretchr/testify/mock"
)
// MockClient implements Client and is used for testing purposes outside of the telemetry package,
// e.g. the tracer and profiler.
type MockClient struct {
mock.Mock
mu sync.Mutex
Started bool
Configuration []telemetry.Configuration
Integrations []string
ProfilerEnabled bool
AsmEnabled bool
Metrics map[telemetry.Namespace]map[string]float64
}
// ProductStart starts and adds configuration data to the mock client.
func (c *MockClient) ProductStart(namespace telemetry.Namespace, configuration []telemetry.Configuration) {
c.mu.Lock()
defer c.mu.Unlock()
c.Started = true
c.Configuration = append(c.Configuration, configuration...)
if len(c.Metrics) == 0 {
c.Metrics = make(map[telemetry.Namespace]map[string]float64)
}
c.productChange(namespace, true)
if namespace == telemetry.NamespaceTracers {
c.productChange(telemetry.NamespaceASM, true)
}
}
// ProductStop signals a product has stopped and disables that product in the mock client.
// ProductStop is NOOP for the tracer namespace, since the tracer is not considered a product.
func (c *MockClient) ProductStop(namespace telemetry.Namespace) {
c.mu.Lock()
defer c.mu.Unlock()
if namespace == telemetry.NamespaceTracers {
return
}
c.productChange(namespace, false)
}
// ProductChange signals that a certain product is enabled or disabled for the mock client.
func (c *MockClient) productChange(namespace telemetry.Namespace, enabled bool) {
switch namespace {
case telemetry.NamespaceASM:
c.AsmEnabled = enabled
case telemetry.NamespaceProfilers:
c.ProfilerEnabled = enabled
case telemetry.NamespaceTracers:
return
default:
panic("invalid product namespace")
}
}
// Record stores the value for the given metric. It is currently mocked for `Gauge` and `Distribution` metric types.
func (c *MockClient) Record(ns telemetry.Namespace, _ telemetry.MetricKind, name string, val float64, tags []string, common bool) {
c.On("Gauge", ns, name, val, tags, common).Return()
c.On("Record", ns, name, val, tags, common).Return()
_ = c.Called(ns, name, val, tags, common)
// record the val for tests that assert based on the value
if _, ok := c.Metrics[ns]; !ok {
c.Metrics[ns] = map[string]float64{}
}
c.Metrics[ns][name] = val
}
// Count counts the value for the given metric
func (c *MockClient) Count(ns telemetry.Namespace, name string, val float64, tags []string, common bool) {
c.On("Count", ns, name, val, tags, common).Return()
_ = c.Called(ns, name, val, tags, common)
}
// Stop is NOOP for the mock client.
func (c *MockClient) Stop() {
}
// ApplyOps is NOOP for the mock client.
func (c *MockClient) ApplyOps(_ ...telemetry.Option) {
}