Skip to content

Commit

Permalink
feat: add health check with /heatlhz endpoint (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
arno4000 authored Jan 9, 2025
1 parent 65320b8 commit d95d3bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/influx/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (a *API) Register(router *mux.Router) {
// Registering two write endpoints; the second is necessary to allow for compatibility with clients that hard-code the endpoint
registerer.RegisterRoute("/api/v1/push/influx/write", http.HandlerFunc(a.handleSeriesPush), http.MethodPost)
registerer.RegisterRoute("/api/v2/write", http.HandlerFunc(a.handleSeriesPush), http.MethodPost)
registerer.RegisterRoute("/healthz", http.HandlerFunc(a.handleHealth), http.MethodGet)
}

func NewAPI(conf ProxyConfig, client remotewrite.Client, recorder Recorder) (*API, error) {
Expand All @@ -40,6 +41,22 @@ func NewAPI(conf ProxyConfig, client remotewrite.Client, recorder Recorder) (*AP
}, nil
}

func (a *API) handleHealth(w http.ResponseWriter, r *http.Request) {
span, _ := opentracing.StartSpanFromContext(r.Context(), "handleHealth")
defer span.Finish()

logger := withRequestInfo(a.logger, r)
_, err := w.Write([]byte("OK"))

if err != nil {
ext.LogError(span, err)
a.handleError(w, r, err, logger)
return
}

w.WriteHeader(http.StatusOK)
}

// HandlerForInfluxLine is a http.Handler which accepts Influx Line protocol and converts it to WriteRequests.
func (a *API) handleSeriesPush(w http.ResponseWriter, r *http.Request) {
span, ctx := opentracing.StartSpanFromContext(r.Context(), "handleSeriesPush")
Expand Down
14 changes: 14 additions & 0 deletions pkg/influx/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,17 @@ func TestHandleSeriesPush(t *testing.T) {
})
}
}
func TestHandleHealth(t *testing.T) {
req := httptest.NewRequest("GET", "/healthz", nil)
req.Header.Set("X-Scope-OrgID", "fake")
rec := httptest.NewRecorder()
logger := log.NewNopLogger()
api := &API{
logger: logger,
}

api.handleHealth(rec, req)

assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "OK", rec.Body.String())
}

0 comments on commit d95d3bc

Please sign in to comment.