-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoutput_logger_test.go
More file actions
126 lines (109 loc) · 3.58 KB
/
output_logger_test.go
File metadata and controls
126 lines (109 loc) · 3.58 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
package statsig
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestLogEventErrors(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusBadRequest)
}))
defer testServer.Close()
errs := make([]error, 0)
opts := &Options{
API: testServer.URL,
StatsigLoggerOptions: StatsigLoggerOptions{
DisableInitDiagnostics: true,
DisableSyncDiagnostics: true,
DisableApiDiagnostics: true,
},
OutputLoggerOptions: OutputLoggerOptions{
EnableDebug: true,
LogCallback: func(message string, err error) {
errs = append(errs, err)
},
},
}
diagnostics := newDiagnostics(opts)
transport := newTransport("secret", opts)
errorBoundary := newErrorBoundary("secret", opts, nil)
sdkConfigs := newSDKConfigs()
logger := newLogger(transport, opts, diagnostics, errorBoundary, sdkConfigs)
user := User{
UserID: "123",
}
event := Event{
EventName: "test_event",
User: user,
Value: "3",
}
stderrLogs := swallow_stderr(func() {
logger.logCustom(event)
logger.flush(true)
})
if stderrLogs == "" {
t.Errorf("Expected output to stderr")
}
InitializeGlobalOutputLogger(opts.OutputLoggerOptions, nil)
logger.logCustom(event)
logger.flush(true)
if len(errs) == 0 {
t.Errorf("Expected output to callback")
}
if !errors.Is(errs[0], ErrFailedLogEvent) {
t.Errorf("Expected error to match ErrFailedLogEvent")
}
if errs[0].Error() != "Failed to log 1 events: Failed request to /log_event after 0 retries: 400 Bad Request" {
t.Errorf("Expected error message")
}
}
func TestGetLoggableSDKKey(t *testing.T) {
if got := getLoggableSDKKey("secret-1234567890abcdef"); got != "secret-123456" {
t.Errorf("Expected long sdk key to be truncated, got %q", got)
}
if got := getLoggableSDKKey("secret-key"); got != "secret-key" {
t.Errorf("Expected short sdk key to remain unchanged, got %q", got)
}
if got := getLoggableSDKKey(""); got != "" {
t.Errorf("Expected empty sdk key to remain unchanged, got %q", got)
}
}
func TestLogPostInitAddsNormalizedMetricTags(t *testing.T) {
observabilityClient := NewObservabilityClientExample()
InitializeGlobalOutputLogger(OutputLoggerOptions{}, observabilityClient)
Logger().LogPostInit(&Options{}, "secret-1234567890abcdef", InitializeDetails{
Duration: 1500 * time.Millisecond,
Success: true,
Source: SourceNetwork,
SourceAPI: "https://api.statsig.com/v1",
StorePopulated: true,
})
distributionMetrics := observabilityClient.GetMetrics("distribution")
if len(distributionMetrics) == 0 {
t.Fatal("Expected initialization distribution metric to be emitted")
}
initMetric := distributionMetrics[len(distributionMetrics)-1]
if initMetric.Name != "statsig.sdk.initialization" {
t.Errorf("Expected initialization metric name, got %q", initMetric.Name)
}
if initMetric.Value != 1500 {
t.Errorf("Expected initialization metric value in milliseconds, got %v", initMetric.Value)
}
if initMetric.Tags["init_success"] != "true" {
t.Errorf("Expected init_success tag to be normalized to string true")
}
if initMetric.Tags["store_populated"] != "true" {
t.Errorf("Expected store_populated tag to be normalized to string true")
}
if initMetric.Tags["sdk_key"] != "secret-123456" {
t.Errorf("Expected sdk_key tag to be loggable prefix, got %v", initMetric.Tags["sdk_key"])
}
if initMetric.Tags["sdk_version"] == "" {
t.Errorf("Expected sdk_version tag to be present")
}
if initMetric.Tags["sdk_type"] != goSDKTypeTagValue {
t.Errorf("Expected sdk_type tag to be injected")
}
}