Files
opencloud/services/proxy/pkg/middleware/tracing.go
Jörn Friedrich Dreyer 632b206675 trace proxie middlewares (#6313)
* trace proxie middlewares

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* Update ocis-pkg/service/grpc/client.go

Co-authored-by: Christian Richter <1058116+dragonchaser@users.noreply.github.com>

* default tls is off

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

---------

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
Co-authored-by: Christian Richter <1058116+dragonchaser@users.noreply.github.com>
2023-05-27 10:18:24 +02:00

51 lines
1.2 KiB
Go

package middleware
import (
"fmt"
"net/http"
chimiddleware "github.com/go-chi/chi/v5/middleware"
pkgtrace "github.com/owncloud/ocis/v2/ocis-pkg/tracing"
proxytracing "github.com/owncloud/ocis/v2/services/proxy/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
// Tracer provides a middleware to start traces
func Tracer() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return &tracer{
next: next,
}
}
}
type tracer struct {
next http.Handler
}
func (m tracer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
span trace.Span
)
tracer := proxytracing.TraceProvider.Tracer("proxy")
spanOpts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindServer),
}
ctx, span = tracer.Start(ctx, fmt.Sprintf("%s %v", r.Method, r.URL.Path), spanOpts...)
defer span.End()
span.SetAttributes(
attribute.KeyValue{
Key: "x-request-id",
Value: attribute.StringValue(chimiddleware.GetReqID(r.Context())),
})
pkgtrace.Propagator.Inject(ctx, propagation.HeaderCarrier(r.Header))
m.next.ServeHTTP(w, r.WithContext(ctx))
}