-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups_test.go
More file actions
691 lines (636 loc) · 20.6 KB
/
Copy pathgroups_test.go
File metadata and controls
691 lines (636 loc) · 20.6 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package loglayer_test
import (
"errors"
"os"
"slices"
"testing"
"go.loglayer.dev/v2"
"go.loglayer.dev/v2/internal/lltest"
"go.loglayer.dev/v2/transport"
)
// twoTransports builds two named TestLoggingLibrary-backed transports for
// routing tests.
func twoTransports(ids ...string) ([]loglayer.Transport, []*lltest.TestLoggingLibrary) {
libs := make([]*lltest.TestLoggingLibrary, len(ids))
out := make([]loglayer.Transport, len(ids))
for i, id := range ids {
libs[i] = &lltest.TestLoggingLibrary{}
out[i] = lltest.New(lltest.Config{
BaseConfig: transport.BaseConfig{ID: id},
Library: libs[i],
})
}
return out, libs
}
func TestGroups_NoConfig_AllTransportsReceive(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
})
log.WithGroup("nonexistent").Info("hi")
if libs[0].Len() != 1 || libs[1].Len() != 1 {
t.Errorf("with no Groups config, both transports should receive: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_PerCallTagging_RoutesToGroupTransports(t *testing.T) {
tr, libs := twoTransports("console", "datadog")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"database": {Transports: []string{"datadog"}},
},
},
})
log.WithGroup("database").Error("connection lost")
if libs[0].Len() != 0 {
t.Errorf("console should not receive (not in group's transports): got %d", libs[0].Len())
}
if libs[1].Len() != 1 {
t.Errorf("datadog should receive: got %d", libs[1].Len())
}
}
func TestGroups_PersistentTagging_OnLogger(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"only-b": {Transports: []string{"b"}},
},
},
})
dbLog := log.WithGroup("only-b")
dbLog.Info("first")
dbLog.Warn("second")
if libs[0].Len() != 0 {
t.Errorf("transport 'a' shouldn't receive: got %d", libs[0].Len())
}
if libs[1].Len() != 2 {
t.Errorf("transport 'b' should receive both: got %d", libs[1].Len())
}
}
func TestGroups_MultipleGroups_UnionOfTransports(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g1": {Transports: []string{"a"}},
"g2": {Transports: []string{"b"}},
},
},
})
log.WithGroup("g1", "g2").Info("both")
if libs[0].Len() != 1 || libs[1].Len() != 1 {
t.Errorf("union of g1+g2 transports should receive: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_PerGroupLevelFilter(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"errors-only": {Transports: []string{"a"}, Level: loglayer.LogLevelError},
},
},
})
log.WithGroup("errors-only").Info("dropped by group level")
log.WithGroup("errors-only").Error("kept")
if libs[0].Len() != 1 {
t.Errorf("expected 1 line (error only): got %d", libs[0].Len())
}
}
func TestGroups_DisabledGroupOnly_Drops(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"db": {Transports: []string{"b"}, Disabled: true},
},
},
})
log.WithGroup("db").Info("disabled group only")
if libs[0].Len() != 0 || libs[1].Len() != 0 {
t.Errorf("disabled-only tag should drop (NOT fall back): a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_MixedDisabledAndEnabled_EnabledRoutes(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"off": {Transports: []string{"a"}, Disabled: true},
"on": {Transports: []string{"b"}},
},
},
})
log.WithGroup("off", "on").Info("hi")
if libs[0].Len() != 0 {
t.Errorf("disabled group's transport should not receive: got %d", libs[0].Len())
}
if libs[1].Len() != 1 {
t.Errorf("enabled group should still route: got %d", libs[1].Len())
}
}
func TestGroups_UnknownGroupOnly_FallsBackToUngrouped(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"defined": {Transports: []string{"a"}},
},
},
})
log.WithGroup("undefined").Info("hi")
if libs[0].Len() != 1 {
t.Errorf("entry tagged only with undefined group should fall back to ungrouped (default: all): got %d", libs[0].Len())
}
}
func TestGroups_UngroupedToNone_DropsUntaggedEntries(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"defined": {Transports: []string{"a"}},
},
Ungrouped: loglayer.UngroupedRouting{Mode: loglayer.UngroupedToNone},
},
})
log.Info("untagged")
if libs[0].Len() != 0 {
t.Errorf("untagged entry should be dropped under UngroupedToNone: got %d", libs[0].Len())
}
log.WithGroup("defined").Info("tagged")
if libs[0].Len() != 1 {
t.Errorf("tagged entry should still route: got %d", libs[0].Len())
}
}
func TestGroups_UngroupedToTransports(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"db": {Transports: []string{"b"}},
},
Ungrouped: loglayer.UngroupedRouting{
Mode: loglayer.UngroupedToTransports,
Transports: []string{"a"},
},
},
})
log.Info("untagged → only a")
if libs[0].Len() != 1 || libs[1].Len() != 0 {
t.Errorf("untagged should hit only allowlisted: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
log.WithGroup("db").Info("tagged → only b")
if libs[0].Len() != 1 || libs[1].Len() != 1 {
t.Errorf("tagged should hit group transport: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_ActiveGroupsFilter(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"db": {Transports: []string{"a"}},
"auth": {Transports: []string{"b"}},
},
ActiveGroups: []string{"db"},
},
})
log.WithGroup("db").Info("via db")
log.WithGroup("auth").Info("via auth (filtered out)")
if libs[0].Len() != 1 {
t.Errorf("db group should route: got %d", libs[0].Len())
}
if libs[1].Len() != 0 {
t.Errorf("auth group should be filtered: got %d", libs[1].Len())
}
}
func TestGroups_RuntimeMutators(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{DisableFatalExit: true, Transports: tr})
log.AddGroup("dynamic", loglayer.LogGroup{Transports: []string{"a"}, Level: loglayer.LogLevelWarn})
log.WithGroup("dynamic").Info("dropped by group level")
if libs[0].Len() != 0 {
t.Errorf("Info under Warn-min group should drop: got %d", libs[0].Len())
}
log.SetGroupLevel("dynamic", loglayer.LogLevelDebug)
log.WithGroup("dynamic").Info("kept after relaxing")
if libs[0].Len() != 1 {
t.Errorf("after SetGroupLevel(Debug): got %d", libs[0].Len())
}
log.DisableGroup("dynamic")
log.WithGroup("dynamic").Info("disabled → drops, no fall-back")
if libs[0].Len() != 1 {
t.Errorf("disabled group should drop entry, not route: got %d", libs[0].Len())
}
log.EnableGroup("dynamic")
log.WithGroup("dynamic").Info("re-enabled")
if libs[0].Len() != 2 {
t.Errorf("re-enabled group: got %d", libs[0].Len())
}
if !log.RemoveGroup("dynamic") {
t.Error("RemoveGroup should return true for existing group")
}
if log.RemoveGroup("dynamic") {
t.Error("RemoveGroup should return false for missing group")
}
}
func TestGroups_SetActiveGroupsAndClear(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g1": {Transports: []string{"a"}},
"g2": {Transports: []string{"a"}},
},
Ungrouped: loglayer.UngroupedRouting{Mode: loglayer.UngroupedToNone},
},
})
log.SetActiveGroups("g1")
log.WithGroup("g1").Info("g1 active")
log.WithGroup("g2").Info("g2 inactive → falls to ungrouped → dropped (UngroupedToNone)")
if libs[0].Len() != 1 {
t.Errorf("only g1 should pass: got %d", libs[0].Len())
}
log.ClearActiveGroups()
log.WithGroup("g2").Info("g2 active again")
if libs[0].Len() != 2 {
t.Errorf("after ClearActiveGroups, g2 should pass: got %d", libs[0].Len())
}
}
func TestGroups_WithGroupChainsAdditively(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g1": {Transports: []string{"a"}},
"g2": {Transports: []string{"b"}},
},
},
})
chained := log.WithGroup("g1").WithGroup("g2")
chained.Info("hits both")
if libs[0].Len() != 1 || libs[1].Len() != 1 {
t.Errorf("chained WithGroup should accumulate: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_BuilderWithGroupMergesWithLoggerGroups(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g1": {Transports: []string{"a"}},
"g2": {Transports: []string{"b"}},
},
},
})
persistent := log.WithGroup("g1")
persistent.WithGroup("g2").Info("two routes")
if libs[0].Len() != 1 || libs[1].Len() != 1 {
t.Errorf("builder.WithGroup should merge with logger.WithGroup: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
persistent.Info("only g1")
if libs[0].Len() != 2 || libs[1].Len() != 1 {
t.Errorf("plain emit should use only persistent groups: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_GetGroups_ReturnsCopy(t *testing.T) {
tr, _ := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g": {Transports: []string{"a"}, Level: loglayer.LogLevelInfo},
},
},
})
// (1) Replacing entries in the returned map should not bleed back.
got := log.GetGroups()
got["g"] = loglayer.LogGroup{Transports: []string{"hacked"}}
got["new"] = loglayer.LogGroup{Transports: []string{"injected"}}
live := log.GetGroups()
if len(live) != 1 {
t.Errorf("map-level mutation should not bleed: %v", live)
}
if live["g"].Transports[0] != "a" {
t.Errorf("transports should not be mutable via map replace: %v", live["g"])
}
// (2) Mutating the returned LogGroup's Transports slice in place
// should also not bleed (load-bearing for cloneLogGroup).
got2 := log.GetGroups()
got2["g"].Transports[0] = "in-place-hack"
live2 := log.GetGroups()
if live2["g"].Transports[0] != "a" {
t.Errorf("in-place Transports mutation bled through: %v", live2["g"])
}
}
func TestGroups_RawHonorsEntryGroups(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"only-b": {Transports: []string{"b"}},
},
},
})
log.Raw(loglayer.RawLogEntry{
LogLevel: loglayer.LogLevelInfo,
Messages: []any{"raw"},
Groups: []string{"only-b"},
})
if libs[0].Len() != 0 || libs[1].Len() != 1 {
t.Errorf("Raw with Groups should route: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_ChildInheritsAssignedGroups(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"only-b": {Transports: []string{"b"}},
},
},
})
tagged := log.WithGroup("only-b")
child := tagged.Child()
child.Info("inherits the group")
if libs[0].Len() != 0 || libs[1].Len() != 1 {
t.Errorf("child should inherit assigned groups: a=%d b=%d", libs[0].Len(), libs[1].Len())
}
}
func TestGroups_ChildGroupIsolation(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{DisableFatalExit: true, Transports: tr})
child := log.Child()
child.AddGroup("only-a", loglayer.LogGroup{Transports: []string{"a"}})
// Parent should not see the group: untagged emit on parent goes
// through ungrouped (UngroupedToAll) → reaches a.
log.Info("parent untagged")
if libs[0].Len() != 1 {
t.Errorf("parent should not see child's group config: got %d", libs[0].Len())
}
// And there's no parent group to tag, so this also goes via ungrouped.
log.WithGroup("only-a").Info("parent tries to tag")
// Parent has no Groups configured → no routing at all → all transports.
if libs[0].Len() != 2 {
t.Errorf("parent has no groups, should still emit: got %d", libs[0].Len())
}
}
func TestGroups_PluginShouldSendStillRuns(t *testing.T) {
tr, libs := twoTransports("a", "b")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"both": {Transports: []string{"a", "b"}},
},
},
})
log.AddPlugin(loglayer.NewSendGate("drop-b", func(p loglayer.ShouldSendParams) bool {
return p.TransportID != "b"
}))
log.WithGroup("both").Info("hi")
if libs[0].Len() != 1 {
t.Errorf("a should receive (group + ShouldSend allow): got %d", libs[0].Len())
}
if libs[1].Len() != 0 {
t.Errorf("b should be vetoed by ShouldSend even though group allows: got %d", libs[1].Len())
}
}
func TestGroups_Build_NoErrors(t *testing.T) {
tr, _ := twoTransports("a")
if _, err := loglayer.Build(loglayer.Config{
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{"g": {Transports: []string{"a"}}},
},
}); err != nil {
t.Errorf("Build with Groups should succeed: %v", err)
}
}
// Setting UngroupedRouting.Transports without bumping Mode to
// UngroupedToTransports is a footgun: the transports list is silently
// ignored. Build should reject it loudly.
func TestGroups_UngroupedTransportsWithoutMode_Errors(t *testing.T) {
tr, _ := twoTransports("a")
_, err := loglayer.Build(loglayer.Config{
Transports: tr,
Routing: loglayer.RoutingConfig{
Ungrouped: loglayer.UngroupedRouting{
// Mode left at zero value (UngroupedToAll).
Transports: []string{"a"},
},
},
})
if !errors.Is(err, loglayer.ErrUngroupedTransportsWithoutMode) {
t.Errorf("Build should return ErrUngroupedTransportsWithoutMode; got %v", err)
}
}
func TestActiveGroupsFromEnv(t *testing.T) {
const envName = "TEST_LOGLAYER_GROUPS_ENV"
t.Run("unset returns nil", func(t *testing.T) {
os.Unsetenv(envName)
if got := loglayer.ActiveGroupsFromEnv(envName); got != nil {
t.Errorf("unset env: got %v, want nil", got)
}
})
t.Run("empty returns nil", func(t *testing.T) {
t.Setenv(envName, "")
if got := loglayer.ActiveGroupsFromEnv(envName); got != nil {
t.Errorf("empty env: got %v, want nil", got)
}
})
t.Run("comma-separated parses with whitespace tolerance", func(t *testing.T) {
t.Setenv(envName, " db , auth, payments ")
got := loglayer.ActiveGroupsFromEnv(envName)
want := []string{"db", "auth", "payments"}
if len(got) != len(want) {
t.Fatalf("got %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("[%d]: got %q, want %q", i, got[i], want[i])
}
}
})
t.Run("only commas returns nil", func(t *testing.T) {
t.Setenv(envName, ",,,")
if got := loglayer.ActiveGroupsFromEnv(envName); got != nil {
t.Errorf("only commas: got %v, want nil", got)
}
})
}
// Smoke test that group routing doesn't break the WithError chain.
func TestGroups_WithErrorAndGroupsCompose(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
Routing: loglayer.RoutingConfig{
Groups: map[string]loglayer.LogGroup{
"g": {Transports: []string{"a"}},
},
},
})
log.WithError(errors.New("boom")).WithGroup("g").Error("explode")
line := libs[0].PopLine()
if line == nil {
t.Fatal("expected line")
}
if line.Data["err"] == nil {
t.Errorf("error should still be present: %v", line.Data)
}
}
// TransportParams.Groups carries the merged persistent + per-call WithGroup
// tags so transports shipping to a group-aware aggregator can include them
// in the wire payload.
func TestGroups_PropagatedToTransportParams(t *testing.T) {
cases := []struct {
name string
emit func(log *loglayer.LogLayer)
want [][]string // one slice per emitted line, nil = expect nil Groups
}{
{
name: "per-call",
emit: func(log *loglayer.LogLayer) { log.WithGroup("payments", "critical").Info("x") },
want: [][]string{{"payments", "critical"}},
},
{
name: "persistent then per-call",
emit: func(log *loglayer.LogLayer) {
scoped := log.WithGroup("payments")
scoped.Info("first")
scoped.WithGroup("critical").Warn("second")
},
want: [][]string{{"payments"}, {"payments", "critical"}},
},
{
name: "nil when WithGroup never called",
emit: func(log *loglayer.LogLayer) { log.Info("x") },
want: [][]string{nil},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{DisableFatalExit: true, Transports: tr})
c.emit(log)
lines := libs[0].Lines()
if len(lines) != len(c.want) {
t.Fatalf("expected %d lines, got %d", len(c.want), len(lines))
}
for i, want := range c.want {
if !slices.Equal(lines[i].Groups, want) {
t.Errorf("line %d Groups: got %v, want %v", i, lines[i].Groups, want)
}
}
})
}
}
// TransportParams.Schema carries the resolved assembly-shape knobs
// (FieldsKey, MetadataFieldName, ErrorFieldName, SourceFieldName) so
// transports and plugins can navigate Data and decide their own metadata
// placement.
func TestSchema_PropagatedToTransportParams(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
FieldsKey: "context",
MetadataFieldName: "metadata",
ErrorFieldName: "error",
// Source.FieldName not set → defaults to "source"
})
log.Info("hi")
line := libs[0].PopLine()
if line == nil {
t.Fatal("expected line")
}
want := loglayer.Schema{
FieldsKey: "context",
MetadataFieldName: "metadata",
ErrorFieldName: "error",
SourceFieldName: "source",
}
if line.Schema != want {
t.Errorf("Schema: got %+v, want %+v", line.Schema, want)
}
}
func TestSchema_DefaultErrorFieldName(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{DisableFatalExit: true, Transports: tr})
log.Info("hi")
line := libs[0].PopLine()
if line == nil {
t.Fatal("expected line")
}
// ErrorFieldName defaults to "err"; SourceFieldName defaults to "source".
if line.Schema.ErrorFieldName != "err" {
t.Errorf("ErrorFieldName default: got %q, want %q", line.Schema.ErrorFieldName, "err")
}
if line.Schema.SourceFieldName != "source" {
t.Errorf("SourceFieldName default: got %q, want %q", line.Schema.SourceFieldName, "source")
}
if line.Schema.FieldsKey != "" {
t.Errorf("FieldsKey default: got %q, want empty", line.Schema.FieldsKey)
}
if line.Schema.MetadataFieldName != "" {
t.Errorf("MetadataFieldName default: got %q, want empty", line.Schema.MetadataFieldName)
}
}
// When Config.MetadataFieldName is set, transports nest both map and
// non-map metadata under that key uniformly. This is the core symmetric-
// API behavior; per-transport shape verification lives in each transport's
// own tests.
func TestSchema_MetadataFieldNameNestsBothMapAndNonMap(t *testing.T) {
tr, libs := twoTransports("a")
log := loglayer.New(loglayer.Config{
DisableFatalExit: true,
Transports: tr,
MetadataFieldName: "payload",
})
log.WithMetadata(loglayer.Metadata{"k": "v"}).Info("map")
log.WithMetadata(struct{ X int }{X: 1}).Info("struct")
lines := libs[0].Lines()
if len(lines) != 2 {
t.Fatalf("expected 2 lines, got %d", len(lines))
}
for i, line := range lines {
if line.Schema.MetadataFieldName != "payload" {
t.Errorf("line %d MetadataFieldName: got %q", i, line.Schema.MetadataFieldName)
}
}
}