shouldAllowHostMetrics determines if metrics should be collected for the given host. This implements the cardinality protection by only allowing metrics for: 1. Explicitly configured hosts 2. Catch-all requests on HTTPS servers (if AllowCatchAllHosts is true or auto-enabled) 3. Catch-all requests on
(host string, isHTTPS bool)
| 268 | // 2. Catch-all requests on HTTPS servers (if AllowCatchAllHosts is true or auto-enabled) |
| 269 | // 3. Catch-all requests on HTTP servers only if explicitly allowed |
| 270 | func (m *Metrics) shouldAllowHostMetrics(host string, isHTTPS bool) bool { |
| 271 | if !m.PerHost { |
| 272 | return true // host won't be used in labels anyway |
| 273 | } |
| 274 | |
| 275 | normalizedHost := strings.ToLower(host) |
| 276 | |
| 277 | // Always allow explicitly configured hosts |
| 278 | if _, exists := m.allowedHosts[normalizedHost]; exists { |
| 279 | return true |
| 280 | } |
| 281 | |
| 282 | // For catch-all requests (not in allowed hosts) |
| 283 | allowCatchAll := m.ObserveCatchallHosts || (isHTTPS && m.hasHTTPSServer) |
| 284 | return allowCatchAll |
| 285 | } |
| 286 | |
| 287 | // serverNameFromContext extracts the current server name from the context. |
| 288 | // Returns "UNKNOWN" if none is available (should probably never happen). |