Skip to content

Commit

Permalink
feat: set X-Influxdb-Version and X-Influxdb-Build headers (influxdata…
Browse files Browse the repository at this point in the history
…#22535)

Closes influxdata#20224
Also a forward port of influxdata#22038 since I saw the same test failing on 2.x
  • Loading branch information
lesam authored Sep 22, 2021
1 parent 233f277 commit 7dfd7de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/go-chi/chi"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
"github.com/influxdata/influxdb/v2/kit/prom"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
Expand Down Expand Up @@ -90,6 +91,19 @@ func WithMetrics(reg *prom.Registry, exposed bool) HandlerOptFn {
}
}

type AddHeader struct {
WriteHeader func(header http.Header)
}

// Middleware is a middleware that mutates the header of all responses
func (h *AddHeader) Middleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
h.WriteHeader(w.Header())
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}

// NewRootHandler creates a new handler with the given name and registers any root-level
// (non-API) routes enabled by the caller.
func NewRootHandler(name string, opts ...HandlerOptFn) *Handler {
Expand All @@ -112,6 +126,13 @@ func NewRootHandler(name string, opts ...HandlerOptFn) *Handler {
h.initMetrics()

r := chi.NewRouter()
buildHeader := &AddHeader{
WriteHeader: func(header http.Header) {
header.Add("X-Influxdb-Build", "OSS")
header.Add("X-Influxdb-Version", influxdb.GetBuildInfo().Version)
},
}
r.Use(buildHeader.Middleware)
// only gather metrics for system handlers
r.Group(func(r chi.Router) {
r.Use(
Expand Down
6 changes: 5 additions & 1 deletion tsdb/index/tsi1/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,14 @@ func (idx Index) Open() error {
// Close closes and removes the index directory.
func (idx *Index) Close() error {
defer os.RemoveAll(idx.Path())
// Series file is opened first and must be closed last
if err := idx.Index.Close(); err != nil {
return err
}
if err := idx.SeriesFile.Close(); err != nil {
return err
}
return idx.Index.Close()
return nil
}

// Reopen closes and opens the index.
Expand Down

0 comments on commit 7dfd7de

Please sign in to comment.