-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobservability_config_test.go
More file actions
328 lines (307 loc) · 8.34 KB
/
observability_config_test.go
File metadata and controls
328 lines (307 loc) · 8.34 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package app
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTracingConfig_options(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg TracingConfig
wantErr bool
contains string
}{
{
name: "stdout provider",
cfg: TracingConfig{Provider: TracingStdout},
wantErr: false,
},
{
name: "otlp provider with default endpoint",
cfg: TracingConfig{Provider: TracingOTLP},
wantErr: false,
},
{
name: "otlp provider with custom endpoint and insecure",
cfg: TracingConfig{Provider: TracingOTLP, Endpoint: "host:4317", Insecure: true},
wantErr: false,
},
{
name: "otlp-http provider with default endpoint",
cfg: TracingConfig{Provider: TracingOTLPHTTP},
wantErr: false,
},
{
name: "noop provider",
cfg: TracingConfig{Provider: TracingNoop},
wantErr: false,
},
{
name: "empty provider defaults to noop",
cfg: TracingConfig{Provider: ""},
wantErr: false,
},
{
name: "sample rate in range adds option",
cfg: TracingConfig{Provider: TracingNoop, SampleRate: 0.5},
wantErr: false,
},
{
name: "unknown provider returns error",
cfg: TracingConfig{Provider: TracingProvider("invalid")},
wantErr: true,
contains: "unknown provider",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
opts, err := tt.cfg.options()
if tt.wantErr {
require.Error(t, err)
assert.ErrorContains(t, err, tt.contains)
return
}
require.NoError(t, err)
assert.NotEmpty(t, opts)
})
}
}
func TestMetricsConfig_options(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg MetricsConfig
wantErr bool
contains string
}{
{
name: "prometheus provider with defaults",
cfg: MetricsConfig{Provider: MetricsPrometheus},
wantErr: false,
},
{
name: "empty provider defaults to prometheus",
cfg: MetricsConfig{Provider: ""},
wantErr: false,
},
{
name: "prometheus with custom endpoint and path",
cfg: MetricsConfig{Provider: MetricsPrometheus, Endpoint: ":9091", Path: "/custom"},
wantErr: false,
},
{
name: "otlp provider with default endpoint",
cfg: MetricsConfig{Provider: MetricsOTLP},
wantErr: false,
},
{
name: "stdout provider",
cfg: MetricsConfig{Provider: MetricsStdout},
wantErr: false,
},
{
name: "unknown provider returns error",
cfg: MetricsConfig{Provider: MetricsProvider("invalid")},
wantErr: true,
contains: "unknown provider",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
opts, err := tt.cfg.options()
if tt.wantErr {
require.Error(t, err)
assert.ErrorContains(t, err, tt.contains)
return
}
require.NoError(t, err)
assert.NotEmpty(t, opts)
})
}
}
func TestLoggingConfig_options(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg LoggingConfig
wantErr bool
contains string
}{
{
name: "console handler with info level",
cfg: LoggingConfig{Handler: LoggingConsole, Level: LoggingInfo},
wantErr: false,
},
{
name: "empty handler defaults to console",
cfg: LoggingConfig{Handler: "", Level: LoggingInfo},
wantErr: false,
},
{
name: "json handler",
cfg: LoggingConfig{Handler: LoggingJSON, Level: LoggingInfo},
wantErr: false,
},
{
name: "debug level",
cfg: LoggingConfig{Handler: LoggingConsole, Level: LoggingDebug},
wantErr: false,
},
{
name: "warn level",
cfg: LoggingConfig{Handler: LoggingConsole, Level: LoggingWarn},
wantErr: false,
},
{
name: "error level",
cfg: LoggingConfig{Handler: LoggingConsole, Level: LoggingError},
wantErr: false,
},
{
name: "empty level defaults to info",
cfg: LoggingConfig{Handler: LoggingConsole, Level: ""},
wantErr: false,
},
{
name: "unknown handler returns error",
cfg: LoggingConfig{Handler: LoggingHandler("invalid"), Level: LoggingInfo},
wantErr: true,
contains: "unknown handler",
},
{
name: "unknown level returns error",
cfg: LoggingConfig{Handler: LoggingConsole, Level: LoggingLevel("invalid")},
wantErr: true,
contains: "unknown level",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
opts, err := tt.cfg.options()
if tt.wantErr {
require.Error(t, err)
assert.ErrorContains(t, err, tt.contains)
return
}
require.NoError(t, err)
assert.NotEmpty(t, opts)
})
}
}
func TestWithObservabilityFromConfig_validConfig(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Tracing: TracingConfig{Provider: TracingNoop},
Metrics: MetricsConfig{Provider: MetricsPrometheus},
Logging: LoggingConfig{Handler: LoggingConsole, Level: LoggingInfo},
}
app, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.NoError(t, err)
require.NotNil(t, app)
assert.NotNil(t, app.logging)
assert.NotNil(t, app.metrics)
assert.NotNil(t, app.tracing)
}
func TestWithObservabilityFromConfig_excludePathsApplied(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Tracing: TracingConfig{Provider: TracingNoop},
ExcludePaths: []string{"/livez", "/readyz"},
ExcludePrefixes: []string{"/admin/"},
}
app, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.NoError(t, err)
require.NotNil(t, app)
assert.True(t, app.config.observability.pathFilter.shouldExclude("/livez"))
assert.True(t, app.config.observability.pathFilter.shouldExclude("/readyz"))
assert.True(t, app.config.observability.pathFilter.shouldExclude("/admin/foo"))
}
func TestWithObservabilityFromConfig_invalidTracingProviderReturnsError(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Tracing: TracingConfig{Provider: TracingProvider("invalid")},
}
_, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.Error(t, err)
assert.ErrorContains(t, err, "observability")
assert.ErrorContains(t, err, "tracing")
assert.ErrorContains(t, err, "unknown provider")
}
func TestWithObservabilityFromConfig_invalidMetricsProviderReturnsError(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Metrics: MetricsConfig{Provider: MetricsProvider("invalid")},
}
_, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.Error(t, err)
assert.ErrorContains(t, err, "observability")
assert.ErrorContains(t, err, "metrics")
assert.ErrorContains(t, err, "unknown provider")
}
func TestWithObservabilityFromConfig_invalidLoggingConfigReturnsError(t *testing.T) {
t.Parallel()
t.Run("unknown handler", func(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Logging: LoggingConfig{Handler: LoggingHandler("invalid"), Level: LoggingInfo},
}
_, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.Error(t, err)
assert.ErrorContains(t, err, "observability")
assert.ErrorContains(t, err, "logging")
assert.ErrorContains(t, err, "unknown handler")
})
t.Run("unknown level", func(t *testing.T) {
t.Parallel()
cfg := ObservabilityConfig{
Logging: LoggingConfig{Handler: LoggingJSON, Level: LoggingLevel("invalid")},
}
_, err := New(
WithServiceName("test"),
WithServiceVersion("1.0.0"),
WithObservabilityFromConfig(cfg),
)
require.Error(t, err)
assert.ErrorContains(t, err, "observability")
assert.ErrorContains(t, err, "logging")
assert.ErrorContains(t, err, "unknown level")
})
}