Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to ignore request in metrics #1025

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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