Skip to content

Commit

Permalink
Merge pull request #46 from cultureamp/healthcheck-telemetry
Browse files Browse the repository at this point in the history
fix: exclude healthcheck from telemetry
  • Loading branch information
jamestelfer authored May 16, 2024
2 parents 8612d73 + df52504 commit 1332f34
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
10 changes: 10 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func handlePostGitCredentials(tokenVendor vendor.PipelineTokenVendor) http.Handl
})
}

func handleHealthCheck() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer drainRequestBody(r)

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
}

func maxRequestSize(limit int64) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.MaxBytesHandler(next, limit)
Expand Down
22 changes: 22 additions & 0 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func TestHandlePostGitCredentials_ReturnsEmptySuccessWhenNoToken(t *testing.T) {
respBody := rr.Body.String()
assert.Equal(t, "", respBody)
}

func TestHandlePostGitCredentials_ReturnsFailureOnInvalidRequest(t *testing.T) {
tokenVendor := tv("expected-token-value")

Expand Down Expand Up @@ -248,6 +249,27 @@ func TestHandlePostGitCredentials_ReturnsFailureOnVendorFailure(t *testing.T) {
assert.Equal(t, "Internal Server Error\n", rr.Body.String())
}

func TestHandleHealthCheck_Success(t *testing.T) {
ctx := context.Background()

req, err := http.NewRequest("GET", "/healthcheck", nil)
require.NoError(t, err)

req = req.WithContext(ctx)
rr := httptest.NewRecorder()

// act
handler := handleHealthCheck()
handler.ServeHTTP(rr, req)

// assert
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "text/plain", rr.Header().Get("Content-Type"))

respBody := rr.Body.String()
assert.Equal(t, "OK", respBody)
}

func tv(token string) vendor.PipelineTokenVendor {
return vendor.PipelineTokenVendor(func(_ context.Context, claims jwt.BuildkiteClaims, repoUrl string) (*vendor.PipelineRepositoryToken, error) {
return &vendor.PipelineRepositoryToken{
Expand Down
12 changes: 4 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (

func configureServerRoutes(cfg config.Config) (http.Handler, error) {
// wrap a mux such that HTTP telemetry is configured by default
mux := observe.NewMux(http.NewServeMux())
muxWithoutTelemetry := http.NewServeMux()
mux := observe.NewMux(muxWithoutTelemetry)

// configure middleware
authorizer, err := jwt.Middleware(cfg.Authorization, jwtmiddleware.WithErrorHandler(jwt.LogErrorHandler()))
Expand Down Expand Up @@ -58,13 +59,9 @@ func configureServerRoutes(cfg config.Config) (http.Handler, error) {

mux.Handle("POST /token", authorized.Then(handlePostToken(tokenVendor)))
mux.Handle("POST /git-credentials", authorized.Then(handlePostGitCredentials(tokenVendor)))
mux.Handle("GET /healthcheck", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer drainRequestBody(r)

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))
// healthchecks are not included in telemetry
muxWithoutTelemetry.Handle("GET /healthcheck", handleHealthCheck())

return mux, nil
}
Expand Down Expand Up @@ -144,7 +141,6 @@ func logBuildInfo() {
}
ev := log.Info()
for _, v := range buildInfo.Settings {

if strings.HasPrefix(v.Key, "vcs.") ||
strings.HasPrefix(v.Key, "GO") ||
v.Key == "CGO_ENABLED" {
Expand Down

0 comments on commit 1332f34

Please sign in to comment.