-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathobservability_client_adapter_test.go
More file actions
141 lines (123 loc) · 4.21 KB
/
observability_client_adapter_test.go
File metadata and controls
141 lines (123 loc) · 4.21 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
package statsig
import (
"context"
"testing"
)
type testRecordedCall struct {
MetricName string
Value float64
Tags map[string]interface{}
}
type testObservabilityClient struct {
BaseObservabilityClient
initCalled bool
incCall *testRecordedCall
gaugeCall *testRecordedCall
distCall *testRecordedCall
}
func (m *testObservabilityClient) Init(ctx context.Context) error {
m.initCalled = true
return nil
}
func (m *testObservabilityClient) Increment(metricName string, value int, tags map[string]interface{}) error {
m.incCall = &testRecordedCall{
MetricName: metricName,
Value: float64(value),
Tags: tags,
}
return nil
}
func (m *testObservabilityClient) Gauge(metricName string, value float64, tags map[string]interface{}) error {
m.gaugeCall = &testRecordedCall{
MetricName: metricName,
Value: value,
Tags: tags,
}
return nil
}
func (m *testObservabilityClient) Distribution(metricName string, value float64, tags map[string]interface{}) error {
m.distCall = &testRecordedCall{
MetricName: metricName,
Value: value,
Tags: tags,
}
return nil
}
func TestNewObservabilityClientFromHandlerBindsMethods(t *testing.T) {
mock := &testObservabilityClient{}
client := NewObservabilityClientFromHandler(mock)
_ = client.Init(context.Background())
_ = client.Increment("test_inc", 123, map[string]interface{}{
"test_tag": "inc_test_value",
})
_ = client.Gauge("test_gauge", 111, map[string]interface{}{
"test_tag": "gauge_test_value",
})
_ = client.Distribution("test_dist", 88, map[string]interface{}{
"test_tag": "dist_test_value",
})
if !mock.initCalled {
t.Error("expected init callback to be invoked")
}
if mock.incCall == nil || mock.incCall.MetricName != "test_inc" || mock.incCall.Value != 123 {
t.Error("expected increment callback to be invoked with metric/value")
}
if mock.gaugeCall == nil || mock.gaugeCall.MetricName != "test_gauge" || mock.gaugeCall.Value != 111 {
t.Error("expected gauge callback to be invoked with metric/value")
}
if mock.distCall == nil || mock.distCall.MetricName != "test_dist" || mock.distCall.Value != 88 {
t.Error("expected distribution callback to be invoked with metric/value")
}
if mock.incCall.Tags["sdk_type"] != goSDKTypeTagValue {
t.Error("expected increment tags to include sdk_type")
}
if mock.gaugeCall.Tags["sdk_type"] != goSDKTypeTagValue {
t.Error("expected gauge tags to include sdk_type")
}
if mock.distCall.Tags["sdk_type"] != goSDKTypeTagValue {
t.Error("expected distribution tags to include sdk_type")
}
}
func TestNewObservabilityClientDefaultsAreNoop(t *testing.T) {
client := NewObservabilityClient(ObservabilityClientFunctions{})
if err := client.Init(context.Background()); err != nil {
t.Errorf("expected no-op init to succeed: %v", err)
}
if err := client.Increment("test_inc", 1, nil); err != nil {
t.Errorf("expected no-op increment to succeed: %v", err)
}
if err := client.Gauge("test_gauge", 1, nil); err != nil {
t.Errorf("expected no-op gauge to succeed: %v", err)
}
if err := client.Distribution("test_dist", 1, nil); err != nil {
t.Errorf("expected no-op distribution to succeed: %v", err)
}
if err := client.Shutdown(context.Background()); err != nil {
t.Errorf("expected no-op shutdown to succeed: %v", err)
}
}
func TestNewObservabilityClientFromNilHandlerUsesNoopDefaults(t *testing.T) {
client := NewObservabilityClientFromHandler(nil)
if err := client.Init(context.Background()); err != nil {
t.Errorf("expected no-op init to succeed: %v", err)
}
if err := client.Increment("test_inc", 1, map[string]interface{}{}); err != nil {
t.Errorf("expected no-op increment to succeed: %v", err)
}
}
func TestInitializeGlobalOutputLoggerWrapsProvidedObservabilityClientWithSDKTypeTag(t *testing.T) {
mock := &testObservabilityClient{}
InitializeGlobalOutputLogger(OutputLoggerOptions{}, mock)
Logger().Increment("test_metric", 1, map[string]interface{}{
"source": "test",
})
if mock.incCall == nil {
t.Fatal("expected increment callback to be invoked")
}
if mock.incCall.Tags["source"] != "test" {
t.Error("expected existing tags to be preserved")
}
if mock.incCall.Tags["sdk_type"] != goSDKTypeTagValue {
t.Error("expected sdk_type tag to be injected")
}
}