Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 17:15 UTC
caddyfile: add `observe_catchall_hosts` option (#7434)
* caddyfile: add `observe_catchall_hosts` option Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> * correct JSON field name and doc comment Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com> --------- Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
Mohammed Al Sahaf committed
Jan 14, 2026 at 03:06 UTC
e40bd019ff4599b34769d249ee37934217ed9fa4
3 files changed
+21
-19
caddyconfig/httpcaddyfile/options.go
+2
@@ -472,6 +472,8 @@ func unmarshalCaddyfileMetricsOptions(d *caddyfile.Dispenser) (any, error) {
472
switch d.Val() {
473
case "per_host":
474
metrics.PerHost = true
475
+ case "observe_catchall_hosts":
476
+ metrics.ObserveCatchallHosts = true
477
default:
478
return nil, d.Errf("unrecognized servers option '%s'", d.Val())
479
}
modules/caddyhttp/metrics.go
+3
-3
@@ -25,7 +25,7 @@ import (
25
// "http": {
26
// "metrics": {
27
// "per_host": true,
28
-// "allow_catch_all_hosts": false
28
+// "observe_catchall_hosts": false
29
// },
30
// "servers": {
31
// "srv0": {
@@ -65,7 +65,7 @@ type Metrics struct {
65
//
66
// Set to true to allow all hosts to get individual metrics (NOT RECOMMENDED
67
// for production environments exposed to the internet).
68
- AllowCatchAllHosts bool `json:"allow_catch_all_hosts,omitempty"`
68
+ ObserveCatchallHosts bool `json:"observe_catchall_hosts,omitempty"`
69
70
init sync.Once
71
httpMetrics *httpMetrics
@@ -200,7 +200,7 @@ func (m *Metrics) shouldAllowHostMetrics(host string, isHTTPS bool) bool {
200
}
201
202
// For catch-all requests (not in allowed hosts)
203
- allowCatchAll := m.AllowCatchAllHosts || (isHTTPS && m.hasHTTPSServer)
203
+ allowCatchAll := m.ObserveCatchallHosts || (isHTTPS && m.hasHTTPSServer)
204
return allowCatchAll
205
}
206
modules/caddyhttp/metrics_test.go
+16
-16
@@ -207,11 +207,11 @@ func TestMetricsInstrumentedHandler(t *testing.T) {
207
func TestMetricsInstrumentedHandlerPerHost(t *testing.T) {
208
ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
209
metrics := &Metrics{
210
- PerHost: true,
211
- AllowCatchAllHosts: true, // Allow all hosts for testing
212
- init: sync.Once{},
213
- httpMetrics: &httpMetrics{},
214
- allowedHosts: make(map[string]struct{}),
210
+ PerHost: true,
211
+ ObserveCatchallHosts: true, // Allow all hosts for testing
212
+ init: sync.Once{},
213
+ httpMetrics: &httpMetrics{},
214
+ allowedHosts: make(map[string]struct{}),
215
}
216
handlerErr := errors.New("oh noes")
217
response := []byte("hello world!")
@@ -387,11 +387,11 @@ func TestMetricsCardinalityProtection(t *testing.T) {
387
388
// Test 1: Without AllowCatchAllHosts, arbitrary hosts should be mapped to "_other"
389
metrics := &Metrics{
390
- PerHost: true,
391
- AllowCatchAllHosts: false, // Default - should map unknown hosts to "_other"
392
- init: sync.Once{},
393
- httpMetrics: &httpMetrics{},
394
- allowedHosts: make(map[string]struct{}),
390
+ PerHost: true,
391
+ ObserveCatchallHosts: false, // Default - should map unknown hosts to "_other"
392
+ init: sync.Once{},
393
+ httpMetrics: &httpMetrics{},
394
+ allowedHosts: make(map[string]struct{}),
395
}
396
397
// Add one allowed host
@@ -444,12 +444,12 @@ func TestMetricsHTTPSCatchAll(t *testing.T) {
444
445
// Test that HTTPS requests allow catch-all even when AllowCatchAllHosts is false
446
metrics := &Metrics{
447
- PerHost: true,
448
- AllowCatchAllHosts: false,
449
- hasHTTPSServer: true, // Simulate having HTTPS servers
450
- init: sync.Once{},
451
- httpMetrics: &httpMetrics{},
452
- allowedHosts: make(map[string]struct{}), // Empty - no explicitly allowed hosts
447
+ PerHost: true,
448
+ ObserveCatchallHosts: false,
449
+ hasHTTPSServer: true, // Simulate having HTTPS servers
450
+ init: sync.Once{},
451
+ httpMetrics: &httpMetrics{},
452
+ allowedHosts: make(map[string]struct{}), // Empty - no explicitly allowed hosts
453
}
454
455
mh := middlewareHandlerFunc(func(w http.ResponseWriter, r *http.Request, h Handler) error {