mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-16 17:20:34 +00:00
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>
This commit is contained in:
committed by
GitHub
parent
cbebc1292b
commit
e40bd019ff
@@ -472,6 +472,8 @@ func unmarshalCaddyfileMetricsOptions(d *caddyfile.Dispenser) (any, error) {
|
||||
switch d.Val() {
|
||||
case "per_host":
|
||||
metrics.PerHost = true
|
||||
case "observe_catchall_hosts":
|
||||
metrics.ObserveCatchallHosts = true
|
||||
default:
|
||||
return nil, d.Errf("unrecognized servers option '%s'", d.Val())
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
// "http": {
|
||||
// "metrics": {
|
||||
// "per_host": true,
|
||||
// "allow_catch_all_hosts": false
|
||||
// "observe_catchall_hosts": false
|
||||
// },
|
||||
// "servers": {
|
||||
// "srv0": {
|
||||
@@ -65,7 +65,7 @@ type Metrics struct {
|
||||
//
|
||||
// Set to true to allow all hosts to get individual metrics (NOT RECOMMENDED
|
||||
// for production environments exposed to the internet).
|
||||
AllowCatchAllHosts bool `json:"allow_catch_all_hosts,omitempty"`
|
||||
ObserveCatchallHosts bool `json:"observe_catchall_hosts,omitempty"`
|
||||
|
||||
init sync.Once
|
||||
httpMetrics *httpMetrics
|
||||
@@ -200,7 +200,7 @@ func (m *Metrics) shouldAllowHostMetrics(host string, isHTTPS bool) bool {
|
||||
}
|
||||
|
||||
// For catch-all requests (not in allowed hosts)
|
||||
allowCatchAll := m.AllowCatchAllHosts || (isHTTPS && m.hasHTTPSServer)
|
||||
allowCatchAll := m.ObserveCatchallHosts || (isHTTPS && m.hasHTTPSServer)
|
||||
return allowCatchAll
|
||||
}
|
||||
|
||||
|
||||
@@ -208,7 +208,7 @@ func TestMetricsInstrumentedHandlerPerHost(t *testing.T) {
|
||||
ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
|
||||
metrics := &Metrics{
|
||||
PerHost: true,
|
||||
AllowCatchAllHosts: true, // Allow all hosts for testing
|
||||
ObserveCatchallHosts: true, // Allow all hosts for testing
|
||||
init: sync.Once{},
|
||||
httpMetrics: &httpMetrics{},
|
||||
allowedHosts: make(map[string]struct{}),
|
||||
@@ -388,7 +388,7 @@ func TestMetricsCardinalityProtection(t *testing.T) {
|
||||
// Test 1: Without AllowCatchAllHosts, arbitrary hosts should be mapped to "_other"
|
||||
metrics := &Metrics{
|
||||
PerHost: true,
|
||||
AllowCatchAllHosts: false, // Default - should map unknown hosts to "_other"
|
||||
ObserveCatchallHosts: false, // Default - should map unknown hosts to "_other"
|
||||
init: sync.Once{},
|
||||
httpMetrics: &httpMetrics{},
|
||||
allowedHosts: make(map[string]struct{}),
|
||||
@@ -445,7 +445,7 @@ func TestMetricsHTTPSCatchAll(t *testing.T) {
|
||||
// Test that HTTPS requests allow catch-all even when AllowCatchAllHosts is false
|
||||
metrics := &Metrics{
|
||||
PerHost: true,
|
||||
AllowCatchAllHosts: false,
|
||||
ObserveCatchallHosts: false,
|
||||
hasHTTPSServer: true, // Simulate having HTTPS servers
|
||||
init: sync.Once{},
|
||||
httpMetrics: &httpMetrics{},
|
||||
|
||||
Reference in New Issue
Block a user