feat: propagate data from http to go-micro, add missing middleware
Middleware in the webdav service was missing. Telemetry for webdav was relying on the otelchi middleware that was added afterwards and removed in the previous commit. New (otelhttp) telemetry has been added to the middlewares for the service. Incoming telemetry data from HTTP (captured with otelhttp) with also be stored so the telemetry for go-micro's requests can be picked up and propagate properly.
This commit is contained in:
1 parent
01a9713b52
commit
0aa20cbf05
2 files changed
+36
-1
No files matched your search
@@ -4,14 +4,19 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
|
||||
"go-micro.dev/v4/metadata"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// GetOtelhttpMiddleware gets a new tracing middleware based on otelhttp
|
||||
// to trace the requests.
|
||||
// This middleware will use the otelhttp middleware and then store the
|
||||
// incoming data into the go-micro's metadata so it can be propagated through
|
||||
// go-micro.
|
||||
func GetOtelhttpMiddleware(service string, tp trace.TracerProvider) func(http.Handler) http.Handler {
|
||||
return otelhttp.NewMiddleware(
|
||||
otelMid := otelhttp.NewMiddleware(
|
||||
service,
|
||||
otelhttp.WithTracerProvider(tp),
|
||||
otelhttp.WithPropagators(tracing.GetPropagator()),
|
||||
@@ -20,6 +25,35 @@ func GetOtelhttpMiddleware(service string, tp trace.TracerProvider) func(http.Ha
|
||||
return r.Method + " " + r.URL.Path
|
||||
}),
|
||||
)
|
||||
|
||||
httpToMicroMid := otelhttpToGoMicroGrpc()
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return otelMid(httpToMicroMid(next))
|
||||
}
|
||||
}
|
||||
|
||||
func otelhttpToGoMicroGrpc() func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
propagator := tracing.GetPropagator()
|
||||
|
||||
// based on go-micro plugin for opentelemetry
|
||||
// inject telemetry data into go-micro's metadata
|
||||
// in order to propagate the info to go-micro's calls
|
||||
md := make(metadata.Metadata)
|
||||
carrier := make(propagation.MapCarrier)
|
||||
propagator.Inject(ctx, carrier)
|
||||
for k, v := range carrier {
|
||||
md.Set(k, v)
|
||||
}
|
||||
mdCtx := metadata.NewContext(ctx, md)
|
||||
r = r.WithContext(mdCtx)
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// GetOtelhttpClient will get a new HTTP client that will use telemetry and
|
||||
|
||||
@@ -86,6 +86,7 @@ func NewService(opts ...Option) (Service, error) {
|
||||
// register method with chi before any routing is set up
|
||||
chi.RegisterMethod("REPORT")
|
||||
|
||||
m.Use(options.Middleware...)
|
||||
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
||||
|
||||
if !svc.config.DisablePreviews {
|
||||
|
||||
Reference in new issue
Block a user