-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability_test.go
More file actions
230 lines (199 loc) · 6.45 KB
/
observability_test.go
File metadata and controls
230 lines (199 loc) · 6.45 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
package gin_test
import (
"bytes"
"context"
"log"
"log/slog"
"testing"
gin "github.com/amikos-tech/ami-gin"
"github.com/amikos-tech/ami-gin/logging"
"github.com/amikos-tech/ami-gin/logging/slogadapter"
"github.com/amikos-tech/ami-gin/logging/stdadapter"
"github.com/amikos-tech/ami-gin/telemetry"
)
// --------------------------------------------------------------------------
// Task 1: Logging core tests
// --------------------------------------------------------------------------
func TestDefaultConfigObservabilityDefaults(t *testing.T) {
cfg := gin.DefaultConfig()
// Logger field must be set to a noop-compatible logger (not nil).
if cfg.Logger == nil {
t.Fatal("DefaultConfig().Logger must not be nil")
}
// Noop logger must not be enabled for any level.
for _, level := range []logging.Level{
logging.LevelDebug, logging.LevelInfo, logging.LevelWarn, logging.LevelError,
} {
if cfg.Logger.Enabled(level) {
t.Errorf("DefaultConfig().Logger.Enabled(%v) = true; want false", level)
}
}
// Signals must report disabled.
if cfg.Signals.Enabled() {
t.Fatal("DefaultConfig().Signals.Enabled() = true; want false")
}
}
func TestWithLoggerRejectsNil(t *testing.T) {
_, err := gin.NewConfig(gin.WithLogger(nil))
if err == nil {
t.Fatal("WithLogger(nil) must return an error")
}
}
func TestLoggingAttrErrorTypeUnknownFallsBackToOther(t *testing.T) {
a := logging.AttrErrorType("definitely_unknown_kind_xyz")
if a.Value != "other" {
t.Errorf("AttrErrorType(unknown).Value = %q; want %q", a.Value, "other")
}
}
// --------------------------------------------------------------------------
// Task 2: Adapter tests
// --------------------------------------------------------------------------
func TestSlogAdapterNilFallsBackToNoop(t *testing.T) {
l := slogadapter.New(nil)
for _, level := range []logging.Level{
logging.LevelDebug, logging.LevelInfo, logging.LevelWarn, logging.LevelError,
} {
if l.Enabled(level) {
t.Errorf("slogadapter.New(nil).Enabled(%v) = true; want false", level)
}
}
// Must not panic.
l.Log(logging.LevelInfo, "test msg", logging.AttrOperation("op"))
}
func TestStdAdapterNilFallsBackToNoop(t *testing.T) {
l := stdadapter.New(nil)
for _, level := range []logging.Level{
logging.LevelDebug, logging.LevelInfo, logging.LevelWarn, logging.LevelError,
} {
if l.Enabled(level) {
t.Errorf("stdadapter.New(nil).Enabled(%v) = true; want false", level)
}
}
// Must not panic.
l.Log(logging.LevelInfo, "test msg", logging.AttrOperation("op"))
}
func TestStdAdapterPrefixesSeverity(t *testing.T) {
var buf bytes.Buffer
stdl := log.New(&buf, "", 0)
l := stdadapter.New(stdl)
l.Log(logging.LevelInfo, "hello", logging.AttrOperation("op"))
got := buf.String()
if len(got) == 0 {
t.Fatal("stdadapter should have emitted output")
}
if got[:6] != "[INFO]" {
t.Errorf("expected output to start with [INFO], got: %q", got)
}
}
func TestSlogAdapterForwardsToSlog(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
sl := slog.New(handler)
l := slogadapter.New(sl)
if !l.Enabled(logging.LevelInfo) {
t.Fatal("slogadapter backed by debug-level handler must be enabled for LevelInfo")
}
l.Log(logging.LevelInfo, "test", logging.AttrOperation("test-op"))
if buf.Len() == 0 {
t.Fatal("slogadapter must forward log entries to the underlying slog.Logger")
}
}
// --------------------------------------------------------------------------
// Task 3: Telemetry / Signals tests
// --------------------------------------------------------------------------
func TestSignalsDisabledDefaults(t *testing.T) {
s := telemetry.Disabled()
if s.Enabled() {
t.Fatal("telemetry.Disabled().Enabled() = true; want false")
}
// Must not panic on Tracer/Meter access.
_ = s.Tracer("test-scope")
_ = s.Meter("test-scope")
}
func TestSignalsEnabledSemantics(t *testing.T) {
// Zero value must report disabled.
var zero telemetry.Signals
if zero.Enabled() {
t.Fatal("zero-value Signals.Enabled() = true; want false")
}
// Disabled() must report disabled.
if telemetry.Disabled().Enabled() {
t.Fatal("Disabled().Enabled() = true; want false")
}
// NewSignals with non-nil providers must report enabled.
s := telemetry.NewSignals(nil, nil, nil)
// NewSignals with all nil providers: enabled is based on explicit construction.
// Per plan D: "NewSignals(...) reports true once the runtime is intentionally constructed"
if !s.Enabled() {
t.Fatal("NewSignals(...) must report Enabled() = true")
}
}
func TestSignalsShutdownNilContextNoop(t *testing.T) {
s := telemetry.Disabled()
nilCtx := nilContext()
// Must not panic with nil context.
if err := s.Shutdown(nilCtx); err != nil {
t.Fatalf("Disabled().Shutdown(nil) returned error: %v", err)
}
}
func TestRunBoundaryOperationNoop(t *testing.T) {
s := telemetry.Disabled()
called := false
nilCtx := nilContext()
err := telemetry.RunBoundaryOperation(nilCtx, s, telemetry.BoundaryConfig{
Scope: "test-scope",
Operation: "test.op",
}, func(_ context.Context) error {
called = true
return nil
})
if err != nil {
t.Fatalf("RunBoundaryOperation noop returned error: %v", err)
}
if !called {
t.Fatal("RunBoundaryOperation must call the provided fn")
}
}
func nilContext() context.Context {
return nil
}
// --------------------------------------------------------------------------
// Task 4: Config round-trip tests
// --------------------------------------------------------------------------
func TestConfigRoundTripObservabilityDefaults(t *testing.T) {
idx, err := buildSmallIndex()
if err != nil {
t.Fatalf("build index: %v", err)
}
data, err := gin.Encode(idx)
if err != nil {
t.Fatalf("encode: %v", err)
}
decoded, err := gin.Decode(data)
if err != nil {
t.Fatalf("decode: %v", err)
}
if decoded.Config == nil {
// No config in index is acceptable; query paths must be nil-safe.
return
}
// After decode, logger must be noop (not nil, not panicking).
if decoded.Config.Logger == nil {
t.Fatal("decoded Config.Logger must not be nil")
}
// Signals must be disabled after decode.
if decoded.Config.Signals.Enabled() {
t.Fatal("decoded Config.Signals must be disabled")
}
}
// buildSmallIndex is a test helper that creates a minimal GIN index.
func buildSmallIndex() (*gin.GINIndex, error) {
b, err := gin.NewBuilder(gin.DefaultConfig(), 10)
if err != nil {
return nil, err
}
if err := b.AddDocument(0, []byte(`{"name":"alice"}`)); err != nil {
return nil, err
}
return b.Finalize(), nil
}