Skip to content

Commit

Permalink
ignore requests with header in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Staněk committed Dec 16, 2024
1 parent 8b69a35 commit 01d9a22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/http/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const (
// load balancers to not load balance the respective request but to
// send it to the request's target directly.
PassthroughLoadbalancingKey = "K-Passthrough-Lb"

// IgnoreMetricsKey is used to mark requests that should not be counted in metrics.
IgnoreMetricsKey = "K-Ignore-Metrics"
)

// User Agent Key & Values
Expand Down Expand Up @@ -135,6 +138,11 @@ func IsKubeletProbe(r *http.Request) bool {
r.Header.Get(KubeletProbeKey) != ""
}

// IsMetricsIgnored returns true if the request is marked to be ignored by metrics.
func IsMetricsIgnored(r *http.Request) bool {
return r.Header.Get(IgnoreMetricsKey) != ""
}

// RewriteHostIn removes the `Host` header from the inbound (server) request
// and replaces it with our custom header.
// This is done to avoid Istio Host based routing, see #3870.
Expand Down
15 changes: 15 additions & 0 deletions pkg/http/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ func TestIsProbe(t *testing.T) {
}
}

func TestIsMetricsIgnored(t *testing.T) {
// Not a metrics ignored
req, err := http.NewRequest(http.MethodGet, "http://example.com/", nil)
if err != nil {
t.Fatal("Error building request:", err)
}
if IsMetricsIgnored(req) {
t.Error("Not a metrics ignored but counted as such")
}
req.Header.Set(IgnoreMetricsKey, "true")
if !IsMetricsIgnored(req) {
t.Error("Metrics ignored but not counted as such")
}
}

func TestRewriteHost(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "http://love.is/not-hate", nil)
r.Header.Set("Host", "love.is")
Expand Down

0 comments on commit 01d9a22

Please sign in to comment.