Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 07:00 UTC
httpcaddyfile: Implement log `sampling` config (#6682)
* Allow log sampling configuration from Caddyfile * Add log sampling adapt tests
Nikolai K committed
Nov 12, 2024 at 00:42 UTC
b183aec83c2d467706583ad9ea0e4e9c56077c55
4 files changed
+126
caddyconfig/httpcaddyfile/builtins.go
+44
@@ -981,6 +981,50 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
981
}
982
cl.WriterRaw = caddyconfig.JSONModuleObject(wo, "output", moduleName, h.warnings)
983
984
+ case "sampling":
985
+ d := h.Dispenser.NewFromNextSegment()
986
+ for d.NextArg() {
987
+ // consume any tokens on the same line, if any.
988
+ }
989
+
990
+ sampling := &caddy.LogSampling{}
991
+ for nesting := d.Nesting(); d.NextBlock(nesting); {
992
+ subdir := d.Val()
993
+ switch subdir {
994
+ case "interval":
995
+ if !d.NextArg() {
996
+ return nil, d.ArgErr()
997
+ }
998
+ interval, err := time.ParseDuration(d.Val() + "ns")
999
+ if err != nil {
1000
+ return nil, d.Errf("failed to parse interval: %v", err)
1001
+ }
1002
+ sampling.Interval = interval
1003
+ case "first":
1004
+ if !d.NextArg() {
1005
+ return nil, d.ArgErr()
1006
+ }
1007
+ first, err := strconv.Atoi(d.Val())
1008
+ if err != nil {
1009
+ return nil, d.Errf("failed to parse first: %v", err)
1010
+ }
1011
+ sampling.First = first
1012
+ case "thereafter":
1013
+ if !d.NextArg() {
1014
+ return nil, d.ArgErr()
1015
+ }
1016
+ thereafter, err := strconv.Atoi(d.Val())
1017
+ if err != nil {
1018
+ return nil, d.Errf("failed to parse thereafter: %v", err)
1019
+ }
1020
+ sampling.Thereafter = thereafter
1021
+ default:
1022
+ return nil, d.Errf("unrecognized subdirective: %s", subdir)
1023
+ }
1024
+ }
1025
+
1026
+ cl.Sampling = sampling
1027
+
1028
case "core":
1029
if !h.NextArg() {
1030
return nil, h.ArgErr()
caddyconfig/httpcaddyfile/builtins_test.go
+14
@@ -62,6 +62,20 @@ func TestLogDirectiveSyntax(t *testing.T) {
62
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.name-override"]},"name-override":{"writer":{"filename":"foo.log","output":"file"},"core":{"module":"mock"},"include":["http.log.access.name-override"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"name-override"}}}}}}`,
63
expectError: false,
64
},
65
+ {
66
+ input: `:8080 {
67
+ log {
68
+ sampling {
69
+ interval 2
70
+ first 3
71
+ thereafter 4
72
+ }
73
+ }
74
+ }
75
+ `,
76
+ output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"sampling":{"interval":2,"first":3,"thereafter":4},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
77
+ expectError: false,
78
+ },
79
} {
80
81
adapter := caddyfile.Adapter{
caddytest/integration/caddyfile_adapt/global_options_log_sampling.caddyfiletest
new
+23
@@ -0,0 +1,23 @@
1
+{
2
+ log {
3
+ sampling {
4
+ interval 300
5
+ first 50
6
+ thereafter 40
7
+ }
8
+ }
9
+}
10
+----------
11
+{
12
+ "logging": {
13
+ "logs": {
14
+ "default": {
15
+ "sampling": {
16
+ "interval": 300,
17
+ "first": 50,
18
+ "thereafter": 40
19
+ }
20
+ }
21
+ }
22
+ }
23
+}
\ No newline at end of file
caddytest/integration/caddyfile_adapt/log_sampling.caddyfiletest
new
+45
@@ -0,0 +1,45 @@
1
+:80 {
2
+ log {
3
+ sampling {
4
+ interval 300
5
+ first 50
6
+ thereafter 40
7
+ }
8
+ }
9
+}
10
+----------
11
+{
12
+ "logging": {
13
+ "logs": {
14
+ "default": {
15
+ "exclude": [
16
+ "http.log.access.log0"
17
+ ]
18
+ },
19
+ "log0": {
20
+ "sampling": {
21
+ "interval": 300,
22
+ "first": 50,
23
+ "thereafter": 40
24
+ },
25
+ "include": [
26
+ "http.log.access.log0"
27
+ ]
28
+ }
29
+ }
30
+ },
31
+ "apps": {
32
+ "http": {
33
+ "servers": {
34
+ "srv0": {
35
+ "listen": [
36
+ ":80"
37
+ ],
38
+ "logs": {
39
+ "default_logger_name": "log0"
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+}
\ No newline at end of file