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

Commit f58a717

Browse files
tjcelayarghetia
authored andcommitted
Allow overriding health endpoint check in handler (#1177)
* Allow overriding health endpoint check in handler * Clarify fallback * Clarify godoc for IsHealthEndpoint * Combine TestIgnoreHealthz and TestHandlerIsHealthEndpoint into a single test TestIgnoreHealthEndpoints, and add cases * Even more explicit godoc * IsHealthEndpointFunc now accepts the http.Request
1 parent df0549d commit f58a717

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

plugin/ochttp/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ type Handler struct {
7070
// from the information found in the incoming HTTP Request. By default the
7171
// name equals the URL Path.
7272
FormatSpanName func(*http.Request) string
73+
74+
// IsHealthEndpoint holds the function to use for determining if the
75+
// incoming HTTP request should be considered a health check. This is in
76+
// addition to the private isHealthEndpoint func which may also indicate
77+
// tracing should be skipped.
78+
IsHealthEndpoint func(*http.Request) bool
7379
}
7480

7581
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -87,7 +93,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8793
}
8894

8995
func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
90-
if isHealthEndpoint(r.URL.Path) {
96+
if h.IsHealthEndpoint != nil && h.IsHealthEndpoint(r) || isHealthEndpoint(r.URL.Path) {
9197
return r, func() {}
9298
}
9399
var name string

plugin/ochttp/server_test.go

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -555,40 +555,61 @@ func TestHandlerImplementsHTTPCloseNotify(t *testing.T) {
555555
}
556556
}
557557

558-
func TestIgnoreHealthz(t *testing.T) {
559-
var spans int
558+
func testHealthEndpointSkipArray(r *http.Request) bool {
559+
for _, toSkip := range []string{"/health", "/metrics"} {
560+
if r.URL.Path == toSkip {
561+
return true
562+
}
563+
}
564+
return false
565+
}
560566

561-
ts := httptest.NewServer(&Handler{
562-
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
563-
span := trace.FromContext(r.Context())
564-
if span != nil {
565-
spans++
566-
}
567-
fmt.Fprint(w, "ok")
568-
}),
569-
StartOptions: trace.StartOptions{
570-
Sampler: trace.AlwaysSample(),
571-
},
572-
})
573-
defer ts.Close()
567+
func TestIgnoreHealthEndpoints(t *testing.T) {
568+
var spans int
574569

575570
client := &http.Client{}
571+
tests := []struct {
572+
path string
573+
healthEndpointFunc func(*http.Request) bool
574+
}{
575+
{"/healthz", nil},
576+
{"/_ah/health", nil},
577+
{"/healthz", testHealthEndpointSkipArray},
578+
{"/_ah/health", testHealthEndpointSkipArray},
579+
{"/health", testHealthEndpointSkipArray},
580+
{"/metrics", testHealthEndpointSkipArray},
581+
}
582+
for _, tt := range tests {
583+
t.Run(tt.path, func(t *testing.T) {
584+
ts := httptest.NewServer(&Handler{
585+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
586+
span := trace.FromContext(r.Context())
587+
if span != nil {
588+
spans++
589+
}
590+
fmt.Fprint(w, "ok")
591+
}),
592+
StartOptions: trace.StartOptions{
593+
Sampler: trace.AlwaysSample(),
594+
},
595+
IsHealthEndpoint: tt.healthEndpointFunc,
596+
})
597+
defer ts.Close()
576598

577-
for _, path := range []string{"/healthz", "/_ah/health"} {
578-
resp, err := client.Get(ts.URL + path)
579-
if err != nil {
580-
t.Fatalf("Cannot GET %q: %v", path, err)
581-
}
582-
583-
b, err := ioutil.ReadAll(resp.Body)
584-
if err != nil {
585-
t.Fatalf("Cannot read body for %q: %v", path, err)
586-
}
599+
resp, err := client.Get(ts.URL + tt.path)
600+
if err != nil {
601+
t.Fatalf("Cannot GET %q: %v", tt.path, err)
602+
}
603+
b, err := ioutil.ReadAll(resp.Body)
604+
if err != nil {
605+
t.Fatalf("Cannot read body for %q: %v", tt.path, err)
606+
}
587607

588-
if got, want := string(b), "ok"; got != want {
589-
t.Fatalf("Body for %q = %q; want %q", path, got, want)
590-
}
591-
resp.Body.Close()
608+
if got, want := string(b), "ok"; got != want {
609+
t.Fatalf("Body for %q = %q; want %q", tt.path, got, want)
610+
}
611+
resp.Body.Close()
612+
})
592613
}
593614

594615
if spans > 0 {

0 commit comments

Comments
 (0)