Add Server and Traceparent http headers
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-12-22 17:35:48 -08:00
parent 14edfaf0e9
commit d08c73a528

View File

@@ -15,11 +15,13 @@ import (
"time"
"github.com/oschwald/geoip2-golang"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/tracing"
"go.ntppool.org/common/version"
)
type geoType uint8
@@ -86,12 +88,24 @@ func setupHTTP(ctx context.Context) error {
mux.HandleFunc("/api/json", handleJSON)
mux.HandleFunc("/healthz", handleHealth)
versionHandler := func(next http.Handler) http.Handler {
vinfo := version.VersionInfo()
v := "geoipapi/" + vinfo.Version + "+" + vinfo.GitRevShort
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", v)
span := trace.SpanFromContext(r.Context())
w.Header().Set("Traceparent", span.SpanContext().TraceID().String())
next.ServeHTTP(w, r)
})
}
srv := &http.Server{
Addr: ":8009",
BaseContext: func(_ net.Listener) context.Context { return ctx },
ReadTimeout: time.Second,
WriteTimeout: 10 * time.Second,
Handler: otelhttp.NewHandler(mux, "geoipapi"),
Handler: otelhttp.NewHandler(versionHandler(mux), "geoipapi"),
}
srvErr := make(chan error, 1)
go func() {