From 5e28c52259d9f435bf523d14d3cb0d0b44c11283 Mon Sep 17 00:00:00 2001 From: Sam Arnold Date: Tue, 21 Sep 2021 15:20:19 -0400 Subject: [PATCH] feat: set X-Influxdb-Version and X-Influxdb-Build headers Closes #20224 Also a forward port of #22038 since I saw the same test failing on 2.x --- http/handler.go | 24 ++++++++++++++++++++++-- tsdb/index/tsi1/index_test.go | 6 +++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/http/handler.go b/http/handler.go index d20a49b5acd..3c8a1490aed 100644 --- a/http/handler.go +++ b/http/handler.go @@ -3,15 +3,15 @@ package http import ( "context" "encoding/json" - "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" "github.com/influxdata/influxdb/v2/pprof" "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" + "net/http" ) const ( @@ -90,6 +90,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 +125,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.