Add Server and Traceparent http headers
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
20
geoipapi.go
20
geoipapi.go
@@ -15,11 +15,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/oschwald/geoip2-golang"
|
"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/contrib/instrumentation/net/http/otelhttp"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
|
|
||||||
|
"go.ntppool.org/common/logger"
|
||||||
|
"go.ntppool.org/common/tracing"
|
||||||
|
"go.ntppool.org/common/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
type geoType uint8
|
type geoType uint8
|
||||||
@@ -86,12 +88,24 @@ func setupHTTP(ctx context.Context) error {
|
|||||||
mux.HandleFunc("/api/json", handleJSON)
|
mux.HandleFunc("/api/json", handleJSON)
|
||||||
mux.HandleFunc("/healthz", handleHealth)
|
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{
|
srv := &http.Server{
|
||||||
Addr: ":8009",
|
Addr: ":8009",
|
||||||
BaseContext: func(_ net.Listener) context.Context { return ctx },
|
BaseContext: func(_ net.Listener) context.Context { return ctx },
|
||||||
ReadTimeout: time.Second,
|
ReadTimeout: time.Second,
|
||||||
WriteTimeout: 10 * time.Second,
|
WriteTimeout: 10 * time.Second,
|
||||||
Handler: otelhttp.NewHandler(mux, "geoipapi"),
|
Handler: otelhttp.NewHandler(versionHandler(mux), "geoipapi"),
|
||||||
}
|
}
|
||||||
srvErr := make(chan error, 1)
|
srvErr := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
Reference in New Issue
Block a user