diff --git a/http/handler.go b/http/handler.go index d20a49b5acd..494f6d63e73 100644 --- a/http/handler.go +++ b/http/handler.go @@ -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" @@ -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 { @@ -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( diff --git a/tsdb/index/tsi1/index_test.go b/tsdb/index/tsi1/index_test.go index c7c02843c31..dc51d71bac2 100644 --- a/tsdb/index/tsi1/index_test.go +++ b/tsdb/index/tsi1/index_test.go @@ -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.