Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 9260bbf

Browse files
authored
Don't trace /healthz (#844)
Kubernetes users end up having tons of /healthz traces which is costly and no-value. Disable known health endpoint tracing until we have a better solution.
1 parent 6892f2d commit 9260bbf

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

plugin/ochttp/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Transport struct {
5757
// RoundTrip implements http.RoundTripper, delegating to Base and recording stats and traces for the request.
5858
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
5959
rt := t.base()
60+
if isHealthEndpoint(req.URL.Path) {
61+
return rt.RoundTrip(req)
62+
}
6063
// TODO: remove excessive nesting of http.RoundTrippers here.
6164
format := t.Propagation
6265
if format == nil {

plugin/ochttp/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8282
}
8383

8484
func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
85+
if isHealthEndpoint(r.URL.Path) {
86+
return r, func() {}
87+
}
8588
var name string
8689
if h.FormatSpanName == nil {
8790
name = spanNameFromURL(r)

plugin/ochttp/server_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,44 @@ func TestHandlerImplementsHTTPCloseNotify(t *testing.T) {
551551
t.Errorf("HTTP2Log got\n\t%q\nwant\n\t%q", g, w)
552552
}
553553
}
554+
555+
func TestIgnoreHealthz(t *testing.T) {
556+
var spans int
557+
558+
ts := httptest.NewServer(&Handler{
559+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
560+
span := trace.FromContext(r.Context())
561+
if span != nil {
562+
spans++
563+
}
564+
fmt.Fprint(w, "ok")
565+
}),
566+
StartOptions: trace.StartOptions{
567+
Sampler: trace.AlwaysSample(),
568+
},
569+
})
570+
defer ts.Close()
571+
572+
client := &http.Client{}
573+
574+
for _, path := range []string{"/healthz", "/_ah/health"} {
575+
resp, err := client.Get(ts.URL + path)
576+
if err != nil {
577+
t.Fatalf("Cannot GET %q: %v", path, err)
578+
}
579+
580+
b, err := ioutil.ReadAll(resp.Body)
581+
if err != nil {
582+
t.Fatalf("Cannot read body for %q: %v", path, err)
583+
}
584+
585+
if got, want := string(b), "ok"; got != want {
586+
t.Fatalf("Body for %q = %q; want %q", path, got, want)
587+
}
588+
resp.Body.Close()
589+
}
590+
591+
if spans > 0 {
592+
t.Errorf("Got %v spans; want no spans", spans)
593+
}
594+
}

plugin/ochttp/trace.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ var codeToStr = map[int32]string{
197197
trace.StatusCodeDataLoss: `"DATA_LOSS"`,
198198
trace.StatusCodeUnauthenticated: `"UNAUTHENTICATED"`,
199199
}
200+
201+
func isHealthEndpoint(path string) bool {
202+
// Health checking is pretty frequent and
203+
// traces collected for health endpoints
204+
// can be extremely noisy and expensive.
205+
// Disable canonical health checking endpoints
206+
// like /healthz and /_ah/health for now.
207+
if path == "/healthz" || path == "/_ah/health" {
208+
return true
209+
}
210+
return false
211+
}

0 commit comments

Comments
 (0)