From c22571c26410060ab66e70796596f357ff61d71e Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 16 Jun 2022 10:43:28 +0200 Subject: [PATCH] Tracing improvements (#2962) * Tracing improvements Create separate TracerProviders per service to improve tracing when running multiple services in a single process (like e.g. ocis does). Previously all service were referring to the same package-level TracerProvider (which, among other things, caused the services to overwrite each others services names). We still create a "global" TracerProvider to cover the cases where we don't have access to the per service Provider yet. This will be improved in a follow up change. * Attach TracerProvider to context To generate more useful tracing (especially when running multiple services in a single process) we'd like to generate Tracers from the per service TracerProvider() instead of the global default provider. Therefore we now pass the TracerProvider via context (similar to what is already done for the logger. --- changelog/unreleased/per-service-trace.md | 7 ++ cmd/revad/runtime/runtime.go | 26 ++++---- internal/grpc/interceptors/appctx/appctx.go | 10 +-- internal/grpc/interceptors/auth/auth.go | 9 ++- .../services/gateway/usershareprovider.go | 4 +- .../publicstorageprovider.go | 9 ++- .../storageprovider/storageprovider.go | 2 +- internal/http/interceptors/appctx/appctx.go | 10 +-- internal/http/interceptors/auth/auth.go | 5 +- internal/http/services/owncloud/ocdav/copy.go | 5 +- .../http/services/owncloud/ocdav/delete.go | 5 +- internal/http/services/owncloud/ocdav/get.go | 5 +- internal/http/services/owncloud/ocdav/head.go | 5 +- .../http/services/owncloud/ocdav/locks.go | 7 +- internal/http/services/owncloud/ocdav/meta.go | 5 +- .../http/services/owncloud/ocdav/mkcol.go | 5 +- internal/http/services/owncloud/ocdav/move.go | 5 +- .../http/services/owncloud/ocdav/ocdav.go | 8 ++- .../owncloud/ocdav/propfind/propfind.go | 7 +- .../http/services/owncloud/ocdav/proppatch.go | 5 +- .../services/owncloud/ocdav/publicfile.go | 3 +- internal/http/services/owncloud/ocdav/put.go | 5 +- .../http/services/owncloud/ocdav/trashbin.go | 7 +- internal/http/services/owncloud/ocdav/tus.go | 5 +- .../http/services/owncloud/ocdav/versions.go | 5 +- pkg/appctx/appctx.go | 14 ++++ pkg/micro/ocdav/service.go | 20 +++--- pkg/rgrpc/rgrpc.go | 26 ++++---- pkg/rgrpc/todo/pool/pool.go | 4 +- pkg/rhttp/rhttp.go | 47 +++++++------- .../utils/decomposedfs/decomposedfs.go | 5 +- pkg/trace/trace.go | 65 +++++++++++++++++-- 32 files changed, 205 insertions(+), 145 deletions(-) create mode 100644 changelog/unreleased/per-service-trace.md diff --git a/changelog/unreleased/per-service-trace.md b/changelog/unreleased/per-service-trace.md new file mode 100644 index 0000000000..5484f023f0 --- /dev/null +++ b/changelog/unreleased/per-service-trace.md @@ -0,0 +1,7 @@ +Enhancement: Per service TracerProvider + +To improve tracing we create separate TracerProviders per service now. +This is especially helpful when running multiple reva services in a single +process (like e.g. oCIS does). + +https://github.com/cs3org/reva/pull/2962 diff --git a/cmd/revad/runtime/runtime.go b/cmd/revad/runtime/runtime.go index 18bd08143a..0c4b19091e 100644 --- a/cmd/revad/runtime/runtime.go +++ b/cmd/revad/runtime/runtime.go @@ -39,6 +39,7 @@ import ( "github.com/mitchellh/mapstructure" "github.com/pkg/errors" "github.com/rs/zerolog" + "go.opentelemetry.io/otel/trace" ) // Run runs a reva server with the given config file and pid file. @@ -87,12 +88,10 @@ func run(mainConf map[string]interface{}, coreConf *coreConf, logger *zerolog.Lo host, _ := os.Hostname() logger.Info().Msgf("host info: %s", host) - if coreConf.TracingEnabled { - initTracing(coreConf) - } + tp := initTracing(coreConf) initCPUCount(coreConf, logger) - servers := initServers(mainConf, logger) + servers := initServers(mainConf, logger, tp) watcher, err := initWatcher(logger, filename) if err != nil { log.Panic(err) @@ -121,10 +120,10 @@ func initWatcher(log *zerolog.Logger, filename string) (*grace.Watcher, error) { return watcher, err } -func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[string]grace.Server { +func initServers(mainConf map[string]interface{}, log *zerolog.Logger, tp trace.TracerProvider) map[string]grace.Server { servers := map[string]grace.Server{} if isEnabledHTTP(mainConf) { - s, err := getHTTPServer(mainConf["http"], log) + s, err := getHTTPServer(mainConf["http"], log, tp) if err != nil { log.Error().Err(err).Msg("error creating http server") os.Exit(1) @@ -133,7 +132,7 @@ func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[strin } if isEnabledGRPC(mainConf) { - s, err := getGRPCServer(mainConf["grpc"], log) + s, err := getGRPCServer(mainConf["grpc"], log, tp) if err != nil { log.Error().Err(err).Msg("error creating grpc server") os.Exit(1) @@ -148,8 +147,9 @@ func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[strin return servers } -func initTracing(conf *coreConf) { - rtrace.SetTraceProvider(conf.TracingCollector, conf.TracingEndpoint, conf.TracingServiceName) +func initTracing(conf *coreConf) trace.TracerProvider { + rtrace.InitDefaultTracerProvider(conf.TracingCollector, conf.TracingEndpoint) + return rtrace.GetTracerProvider(conf.TracingEnabled, conf.TracingCollector, conf.TracingEndpoint, conf.TracingServiceName) } func initCPUCount(conf *coreConf, log *zerolog.Logger) { @@ -244,9 +244,9 @@ func getWriter(out string) (io.Writer, error) { return fd, nil } -func getGRPCServer(conf interface{}, l *zerolog.Logger) (*rgrpc.Server, error) { +func getGRPCServer(conf interface{}, l *zerolog.Logger, tp trace.TracerProvider) (*rgrpc.Server, error) { sub := l.With().Str("pkg", "rgrpc").Logger() - s, err := rgrpc.NewServer(conf, sub) + s, err := rgrpc.NewServer(conf, sub, tp) if err != nil { err = errors.Wrap(err, "main: error creating grpc server") return nil, err @@ -254,9 +254,9 @@ func getGRPCServer(conf interface{}, l *zerolog.Logger) (*rgrpc.Server, error) { return s, nil } -func getHTTPServer(conf interface{}, l *zerolog.Logger) (*rhttp.Server, error) { +func getHTTPServer(conf interface{}, l *zerolog.Logger, tp trace.TracerProvider) (*rhttp.Server, error) { sub := l.With().Str("pkg", "rhttp").Logger() - s, err := rhttp.New(conf, sub) + s, err := rhttp.New(conf, sub, tp) if err != nil { err = errors.Wrap(err, "main: error creating http server") return nil, err diff --git a/internal/grpc/interceptors/appctx/appctx.go b/internal/grpc/interceptors/appctx/appctx.go index 6f68951801..705edd7ce5 100644 --- a/internal/grpc/interceptors/appctx/appctx.go +++ b/internal/grpc/interceptors/appctx/appctx.go @@ -23,7 +23,6 @@ import ( "runtime" "github.com/cs3org/reva/v2/pkg/appctx" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/rs/zerolog" semconv "go.opentelemetry.io/otel/semconv/v1.10.0" "go.opentelemetry.io/otel/trace" @@ -34,12 +33,12 @@ import ( const tracerName = "appctx" // NewUnary returns a new unary interceptor that creates the application context. -func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor { +func NewUnary(log zerolog.Logger, tp trace.TracerProvider) grpc.UnaryServerInterceptor { interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - ctx, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "grpc unary") + ctx, span = tp.Tracer(tracerName).Start(ctx, "grpc unary") } _, file, _, ok := runtime.Caller(1) if ok { @@ -48,6 +47,7 @@ func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor { sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger() ctx = appctx.WithLogger(ctx, &sub) + ctx = appctx.WithTracerProvider(ctx, tp) res, err := handler(ctx, req) return res, err } @@ -56,14 +56,14 @@ func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor { // NewStream returns a new server stream interceptor // that creates the application context. -func NewStream(log zerolog.Logger) grpc.StreamServerInterceptor { +func NewStream(log zerolog.Logger, tp trace.TracerProvider) grpc.StreamServerInterceptor { interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { ctx := ss.Context() span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - ctx, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "grpc stream") + ctx, span = tp.Tracer(tracerName).Start(ctx, "grpc stream") } _, file, _, ok := runtime.Caller(1) if ok { diff --git a/internal/grpc/interceptors/auth/auth.go b/internal/grpc/interceptors/auth/auth.go index 69842fca1b..f32cb37e73 100644 --- a/internal/grpc/interceptors/auth/auth.go +++ b/internal/grpc/interceptors/auth/auth.go @@ -34,7 +34,6 @@ import ( "github.com/cs3org/reva/v2/pkg/sharedconf" "github.com/cs3org/reva/v2/pkg/token" tokenmgr "github.com/cs3org/reva/v2/pkg/token/manager/registry" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" @@ -70,7 +69,7 @@ func parseConfig(m map[string]interface{}) (*config, error) { // NewUnary returns a new unary interceptor that adds // trace information for the request. -func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerInterceptor, error) { +func NewUnary(m map[string]interface{}, unprotected []string, tp trace.TracerProvider) (grpc.UnaryServerInterceptor, error) { conf, err := parseConfig(m) if err != nil { err = errors.Wrap(err, "auth: error parsing config") @@ -101,7 +100,7 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - ctx, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "grpc auth unary") + ctx, span = tp.Tracer(tracerName).Start(ctx, "grpc auth unary") } if utils.Skip(info.FullMethod, unprotected) { @@ -150,7 +149,7 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI // NewStream returns a new server stream interceptor // that adds trace information to the request. -func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServerInterceptor, error) { +func NewStream(m map[string]interface{}, unprotected []string, tp trace.TracerProvider) (grpc.StreamServerInterceptor, error) { conf, err := parseConfig(m) if err != nil { return nil, err @@ -180,7 +179,7 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - ctx, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "grpc auth new stream") + ctx, span = tp.Tracer(tracerName).Start(ctx, "grpc auth new stream") } if utils.Skip(info.FullMethod, unprotected) { diff --git a/internal/grpc/services/gateway/usershareprovider.go b/internal/grpc/services/gateway/usershareprovider.go index 4f2dff96d5..fff53cceb7 100644 --- a/internal/grpc/services/gateway/usershareprovider.go +++ b/internal/grpc/services/gateway/usershareprovider.go @@ -33,7 +33,6 @@ import ( "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/storage/utils/grants" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/pkg/errors" ) @@ -190,8 +189,7 @@ func (s *svc) GetReceivedShare(ctx context.Context, req *collaboration.GetReceiv // 1) if received share is mounted: we also do a rename in the storage // 2) if received share is not mounted: we only rename in user share provider. func (s *svc) UpdateReceivedShare(ctx context.Context, req *collaboration.UpdateReceivedShareRequest) (*collaboration.UpdateReceivedShareResponse, error) { - t := rtrace.Provider.Tracer("reva") - ctx, span := t.Start(ctx, "Gateway.UpdateReceivedShare") + ctx, span := appctx.GetTracerProvider(ctx).Tracer("gateway").Start(ctx, "Gateway.UpdateReceivedShare") defer span.End() // sanity checks diff --git a/internal/grpc/services/publicstorageprovider/publicstorageprovider.go b/internal/grpc/services/publicstorageprovider/publicstorageprovider.go index 0fe0399091..6c36bb64ac 100644 --- a/internal/grpc/services/publicstorageprovider/publicstorageprovider.go +++ b/internal/grpc/services/publicstorageprovider/publicstorageprovider.go @@ -38,7 +38,6 @@ import ( "github.com/cs3org/reva/v2/pkg/rgrpc/status" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" @@ -555,7 +554,7 @@ func (s *service) CreateContainer(ctx context.Context, req *provider.CreateConta _, req.Ref.ResourceId.StorageId = storagespace.SplitStorageID(req.Ref.ResourceId.StorageId) } - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "CreateContainer") + ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "CreateContainer") defer span.End() span.SetAttributes(attribute.KeyValue{ @@ -616,7 +615,7 @@ func (s *service) Delete(ctx context.Context, req *provider.DeleteRequest) (*pro _, req.Ref.ResourceId.StorageId = storagespace.SplitStorageID(req.Ref.ResourceId.StorageId) } - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "Delete") + ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Delete") defer span.End() span.SetAttributes(attribute.KeyValue{ @@ -663,7 +662,7 @@ func (s *service) Move(ctx context.Context, req *provider.MoveRequest) (*provide _, req.Destination.ResourceId.StorageId = storagespace.SplitStorageID(req.Destination.ResourceId.StorageId) } - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "Move") + ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Move") defer span.End() span.SetAttributes( @@ -730,7 +729,7 @@ func (s *service) Stat(ctx context.Context, req *provider.StatRequest) (*provide _, req.Ref.ResourceId.StorageId = storagespace.SplitStorageID(req.Ref.ResourceId.StorageId) } - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "Stat") + ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Stat") defer span.End() span.SetAttributes( diff --git a/internal/grpc/services/storageprovider/storageprovider.go b/internal/grpc/services/storageprovider/storageprovider.go index 983ae2178d..c1e9d5e42f 100644 --- a/internal/grpc/services/storageprovider/storageprovider.go +++ b/internal/grpc/services/storageprovider/storageprovider.go @@ -714,7 +714,7 @@ func (s *service) Stat(ctx context.Context, req *provider.StatRequest) (*provide providerID := unwrapProviderID(req.Ref.GetResourceId()) defer rewrapProviderID(req.Ref.GetResourceId(), providerID) - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "stat") + ctx, span := rtrace.DefaultProvider().Tracer(tracerName).Start(ctx, "stat") defer span.End() span.SetAttributes(attribute.KeyValue{ diff --git a/internal/http/interceptors/appctx/appctx.go b/internal/http/interceptors/appctx/appctx.go index 7de1e3bd2d..b86caa8c1d 100644 --- a/internal/http/interceptors/appctx/appctx.go +++ b/internal/http/interceptors/appctx/appctx.go @@ -25,7 +25,6 @@ import ( "net/http" "github.com/cs3org/reva/v2/pkg/appctx" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/rs/zerolog" "go.opentelemetry.io/otel/trace" ) @@ -35,25 +34,26 @@ const tracerName = "appctx" // New returns a new HTTP middleware that stores the log // in the context with request ID information. -func New(log zerolog.Logger) func(http.Handler) http.Handler { +func New(log zerolog.Logger, tp trace.TracerProvider) func(http.Handler) http.Handler { chain := func(h http.Handler) http.Handler { - return handler(log, h) + return handler(log, tp, h) } return chain } -func handler(log zerolog.Logger, h http.Handler) http.Handler { +func handler(log zerolog.Logger, tp trace.TracerProvider, h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - ctx, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "http interceptor") + ctx, span = tp.Tracer(tracerName).Start(ctx, "http interceptor") } sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger() ctx = appctx.WithLogger(ctx, &sub) + ctx = appctx.WithTracerProvider(ctx, tp) r = r.WithContext(ctx) h.ServeHTTP(w, r) }) diff --git a/internal/http/interceptors/auth/auth.go b/internal/http/interceptors/auth/auth.go index 74db583f77..8eea964129 100644 --- a/internal/http/interceptors/auth/auth.go +++ b/internal/http/interceptors/auth/auth.go @@ -43,7 +43,6 @@ import ( "github.com/cs3org/reva/v2/pkg/sharedconf" "github.com/cs3org/reva/v2/pkg/token" tokenmgr "github.com/cs3org/reva/v2/pkg/token/manager/registry" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" @@ -84,7 +83,7 @@ func parseConfig(m map[string]interface{}) (*config, error) { } // New returns a new middleware with defined priority. -func New(m map[string]interface{}, unprotected []string) (global.Middleware, error) { +func New(m map[string]interface{}, unprotected []string, tp trace.TracerProvider) (global.Middleware, error) { conf, err := parseConfig(m) if err != nil { return nil, err @@ -168,7 +167,7 @@ func New(m map[string]interface{}, unprotected []string) (global.Middleware, err span := trace.SpanFromContext(ctx) defer span.End() if !span.SpanContext().HasTraceID() { - _, span = rtrace.Provider.Tracer(tracerName).Start(ctx, "http auth interceptor") + _, span = tp.Tracer(tracerName).Start(ctx, "http auth interceptor") } if r.Method == "OPTIONS" { diff --git a/internal/http/services/owncloud/ocdav/copy.go b/internal/http/services/owncloud/ocdav/copy.go index 8d7bbba374..c2fa25333f 100644 --- a/internal/http/services/owncloud/ocdav/copy.go +++ b/internal/http/services/owncloud/ocdav/copy.go @@ -37,7 +37,6 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp" "github.com/cs3org/reva/v2/pkg/rhttp/router" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" ) @@ -51,7 +50,7 @@ type copy struct { } func (s *svc) handlePathCopy(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "copy") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "copy") defer span.End() if s.c.EnableHTTPTpc { @@ -291,7 +290,7 @@ func (s *svc) executePathCopy(ctx context.Context, client gateway.GatewayAPIClie } func (s *svc) handleSpacesCopy(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_copy") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_copy") defer span.End() dh := r.Header.Get(net.HeaderDestination) diff --git a/internal/http/services/owncloud/ocdav/delete.go b/internal/http/services/owncloud/ocdav/delete.go index d0abf20653..d28a58f899 100644 --- a/internal/http/services/owncloud/ocdav/delete.go +++ b/internal/http/services/owncloud/ocdav/delete.go @@ -31,7 +31,6 @@ import ( "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/spacelookup" "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rgrpc/status" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" ) @@ -61,7 +60,7 @@ func (s *svc) handlePathDelete(w http.ResponseWriter, r *http.Request, ns string } func (s *svc) handleDelete(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, log zerolog.Logger) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "delete") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(ctx, "delete") defer span.End() req := &provider.DeleteRequest{Ref: ref} @@ -130,7 +129,7 @@ func (s *svc) handleDelete(ctx context.Context, w http.ResponseWriter, r *http.R func (s *svc) handleSpacesDelete(w http.ResponseWriter, r *http.Request, spaceID string) { ctx := r.Context() - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "spaces_delete") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(ctx, "spaces_delete") defer span.End() sublog := appctx.GetLogger(ctx).With().Logger() diff --git a/internal/http/services/owncloud/ocdav/get.go b/internal/http/services/owncloud/ocdav/get.go index b6d0dac825..035b5d958d 100644 --- a/internal/http/services/owncloud/ocdav/get.go +++ b/internal/http/services/owncloud/ocdav/get.go @@ -38,13 +38,12 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" ) func (s *svc) handlePathGet(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "get") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "get") defer span.End() fn := path.Join(ns, r.URL.Path) @@ -179,7 +178,7 @@ func (s *svc) handleGet(ctx context.Context, w http.ResponseWriter, r *http.Requ } func (s *svc) handleSpacesGet(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_get") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_get") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("spaceid", spaceID).Str("handler", "get").Logger() diff --git a/internal/http/services/owncloud/ocdav/head.go b/internal/http/services/owncloud/ocdav/head.go index ae04895f8b..798ef7ac4c 100644 --- a/internal/http/services/owncloud/ocdav/head.go +++ b/internal/http/services/owncloud/ocdav/head.go @@ -28,7 +28,6 @@ import ( "time" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -42,7 +41,7 @@ import ( ) func (s *svc) handlePathHead(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "head") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "head") defer span.End() fn := path.Join(ns, r.URL.Path) @@ -109,7 +108,7 @@ func (s *svc) handleHead(ctx context.Context, w http.ResponseWriter, r *http.Req } func (s *svc) handleSpacesHead(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_head") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_head") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("spaceid", spaceID).Str("path", r.URL.Path).Logger() diff --git a/internal/http/services/owncloud/ocdav/locks.go b/internal/http/services/owncloud/ocdav/locks.go index 8383622fd9..10f7fd6f06 100644 --- a/internal/http/services/owncloud/ocdav/locks.go +++ b/internal/http/services/owncloud/ocdav/locks.go @@ -41,7 +41,6 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/errtypes" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/google/uuid" "go.opentelemetry.io/otel/attribute" ) @@ -371,7 +370,7 @@ func parseDepth(s string) int { } */ func (s *svc) handleLock(w http.ResponseWriter, r *http.Request, ns string) (retStatus int, retErr error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) defer span.End() span.SetAttributes(attribute.String("component", "ocdav")) @@ -396,7 +395,7 @@ func (s *svc) handleLock(w http.ResponseWriter, r *http.Request, ns string) (ret } func (s *svc) handleSpacesLock(w http.ResponseWriter, r *http.Request, spaceID string) (retStatus int, retErr error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) defer span.End() span.SetAttributes(attribute.String("component", "ocdav")) @@ -565,7 +564,7 @@ func writeLockInfo(w io.Writer, token string, ld LockDetails) (int, error) { } func (s *svc) handleUnlock(w http.ResponseWriter, r *http.Request, ns string) (status int, err error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) defer span.End() span.SetAttributes(attribute.String("component", "ocdav")) diff --git a/internal/http/services/owncloud/ocdav/meta.go b/internal/http/services/owncloud/ocdav/meta.go index 3db4b1cead..f67f1f5049 100644 --- a/internal/http/services/owncloud/ocdav/meta.go +++ b/internal/http/services/owncloud/ocdav/meta.go @@ -33,7 +33,6 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp/router" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" ) // MetaHandler handles meta requests @@ -91,7 +90,7 @@ func (h *MetaHandler) Handler(s *svc) http.Handler { } func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request, s *svc, rid *provider.ResourceId) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "meta_propfind") + ctx, span := appctx.GetTracerProvider(r.Context()).Tracer(tracerName).Start(r.Context(), "meta_propfind") defer span.End() id := storagespace.FormatResourceID(*rid) @@ -178,7 +177,7 @@ func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request, } func (h *MetaHandler) handleEmptyID(w http.ResponseWriter, r *http.Request) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "meta_propfind") + ctx, span := appctx.GetTracerProvider(r.Context()).Tracer(tracerName).Start(r.Context(), "meta_propfind") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Logger() diff --git a/internal/http/services/owncloud/ocdav/mkcol.go b/internal/http/services/owncloud/ocdav/mkcol.go index ba1be6c75c..047e2b8216 100644 --- a/internal/http/services/owncloud/ocdav/mkcol.go +++ b/internal/http/services/owncloud/ocdav/mkcol.go @@ -30,12 +30,11 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/errtypes" rstatus "github.com/cs3org/reva/v2/pkg/rgrpc/status" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/rs/zerolog" ) func (s *svc) handlePathMkcol(w http.ResponseWriter, r *http.Request, ns string) (status int, err error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "mkcol") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "mkcol") defer span.End() fn := path.Join(ns, r.URL.Path) @@ -88,7 +87,7 @@ func (s *svc) handlePathMkcol(w http.ResponseWriter, r *http.Request, ns string) } func (s *svc) handleSpacesMkCol(w http.ResponseWriter, r *http.Request, spaceID string) (status int, err error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_mkcol") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_mkcol") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("spaceid", spaceID).Str("handler", "mkcol").Logger() diff --git a/internal/http/services/owncloud/ocdav/move.go b/internal/http/services/owncloud/ocdav/move.go index ca513c6af5..24fc61bf6a 100644 --- a/internal/http/services/owncloud/ocdav/move.go +++ b/internal/http/services/owncloud/ocdav/move.go @@ -32,13 +32,12 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp/router" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" ) func (s *svc) handlePathMove(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "move") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "move") defer span.End() if r.Body != http.NoBody { @@ -97,7 +96,7 @@ func (s *svc) handlePathMove(w http.ResponseWriter, r *http.Request, ns string) } func (s *svc) handleSpacesMove(w http.ResponseWriter, r *http.Request, srcSpaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_move") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_move") defer span.End() if r.Body != http.NoBody { diff --git a/internal/http/services/owncloud/ocdav/ocdav.go b/internal/http/services/owncloud/ocdav/ocdav.go index 2cbf2b1ca1..1de9831c88 100644 --- a/internal/http/services/owncloud/ocdav/ocdav.go +++ b/internal/http/services/owncloud/ocdav/ocdav.go @@ -41,8 +41,10 @@ import ( "github.com/cs3org/reva/v2/pkg/storage/favorite" "github.com/cs3org/reva/v2/pkg/storage/favorite/registry" "github.com/cs3org/reva/v2/pkg/storage/utils/templates" + rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/mitchellh/mapstructure" "github.com/rs/zerolog" + "go.opentelemetry.io/otel/trace" ) // name is the Tracer name used to identify this instrumentation library. @@ -149,6 +151,7 @@ type svc struct { // LockSystem is the lock management system. LockSystem LockSystem userIdentifierCache *ttlcache.Cache + tracerProvider trace.TracerProvider } func (s *svc) Config() *Config { @@ -188,11 +191,11 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) return nil, err } - return NewWith(conf, fm, ls, log) + return NewWith(conf, fm, ls, log, rtrace.DefaultProvider()) } // NewWith returns a new ocdav service -func NewWith(conf *Config, fm favorite.Manager, ls LockSystem, _ *zerolog.Logger) (global.Service, error) { +func NewWith(conf *Config, fm favorite.Manager, ls LockSystem, _ *zerolog.Logger, tp trace.TracerProvider) (global.Service, error) { s := &svc{ c: conf, webDavHandler: new(WebDavHandler), @@ -204,6 +207,7 @@ func NewWith(conf *Config, fm favorite.Manager, ls LockSystem, _ *zerolog.Logger favoritesManager: fm, LockSystem: ls, userIdentifierCache: ttlcache.NewCache(), + tracerProvider: tp, } _ = s.userIdentifierCache.SetTTL(60 * time.Second) diff --git a/internal/http/services/owncloud/ocdav/propfind/propfind.go b/internal/http/services/owncloud/ocdav/propfind/propfind.go index 7b7751c25e..78dc3f8c32 100644 --- a/internal/http/services/owncloud/ocdav/propfind/propfind.go +++ b/internal/http/services/owncloud/ocdav/propfind/propfind.go @@ -47,7 +47,6 @@ import ( "github.com/cs3org/reva/v2/pkg/publicshare" "github.com/cs3org/reva/v2/pkg/rhttp/router" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" "go.opentelemetry.io/otel/codes" @@ -179,7 +178,7 @@ func NewHandler(publicURL string, getClientFunc GetGatewayServiceClientFunc) *Ha // HandlePathPropfind handles a path based propfind request // ns is the namespace that is prefixed to the path in the cs3 namespace func (p *Handler) HandlePathPropfind(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) + ctx, span := appctx.GetTracerProvider(r.Context()).Tracer(tracerName).Start(r.Context(), fmt.Sprintf("%s %v", r.Method, r.URL.Path)) defer span.End() fn := path.Join(ns, r.URL.Path) // TODO do we still need to jail if we query the registry about the spaces? @@ -238,7 +237,7 @@ func (p *Handler) HandlePathPropfind(w http.ResponseWriter, r *http.Request, ns // HandleSpacesPropfind handles a spaces based propfind request func (p *Handler) HandleSpacesPropfind(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_propfind") + ctx, span := appctx.GetTracerProvider(r.Context()).Tracer(tracerName).Start(r.Context(), "spaces_propfind") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("spaceid", spaceID).Logger() @@ -285,7 +284,7 @@ func (p *Handler) HandleSpacesPropfind(w http.ResponseWriter, r *http.Request, s } func (p *Handler) propfindResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, namespace, spaceType string, pf XML, sendTusHeaders bool, resourceInfos []*provider.ResourceInfo, log zerolog.Logger) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "propfind_response") + ctx, span := appctx.GetTracerProvider(r.Context()).Tracer(tracerName).Start(ctx, "propfind_response") defer span.End() filters := make([]*link.ListPublicSharesRequest_Filter, 0, len(resourceInfos)) diff --git a/internal/http/services/owncloud/ocdav/proppatch.go b/internal/http/services/owncloud/ocdav/proppatch.go index 20197389fb..58de505f5e 100644 --- a/internal/http/services/owncloud/ocdav/proppatch.go +++ b/internal/http/services/owncloud/ocdav/proppatch.go @@ -39,12 +39,11 @@ import ( ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/errtypes" rstatus "github.com/cs3org/reva/v2/pkg/rgrpc/status" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/rs/zerolog" ) func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns string) (status int, err error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "proppatch") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "proppatch") defer span.End() fn := path.Join(ns, r.URL.Path) @@ -95,7 +94,7 @@ func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns str } func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spaceID string) (status int, err error) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_proppatch") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_proppatch") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("spaceid", spaceID).Logger() diff --git a/internal/http/services/owncloud/ocdav/publicfile.go b/internal/http/services/owncloud/ocdav/publicfile.go index 2c083ef5af..fc19def345 100644 --- a/internal/http/services/owncloud/ocdav/publicfile.go +++ b/internal/http/services/owncloud/ocdav/publicfile.go @@ -29,7 +29,6 @@ import ( "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/propfind" "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp/router" - rtrace "github.com/cs3org/reva/v2/pkg/trace" ) // PublicFileHandler handles requests on a shared file. it needs to be wrapped in a collection @@ -85,7 +84,7 @@ func (h *PublicFileHandler) Handler(s *svc) http.Handler { // ns is the namespace that is prefixed to the path in the cs3 namespace func (s *svc) handlePropfindOnToken(w http.ResponseWriter, r *http.Request, ns string, onContainer bool) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "token_propfind") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "token_propfind") defer span.End() tokenStatInfo := ctx.Value(tokenStatInfoKey{}).(*provider.ResourceInfo) diff --git a/internal/http/services/owncloud/ocdav/put.go b/internal/http/services/owncloud/ocdav/put.go index 068fdb5343..0df6d7b365 100644 --- a/internal/http/services/owncloud/ocdav/put.go +++ b/internal/http/services/owncloud/ocdav/put.go @@ -38,7 +38,6 @@ import ( "github.com/cs3org/reva/v2/pkg/rhttp" "github.com/cs3org/reva/v2/pkg/storage/utils/chunking" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" ) @@ -109,7 +108,7 @@ func isContentRange(r *http.Request) bool { } func (s *svc) handlePathPut(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "put") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "put") defer span.End() fn := path.Join(ns, r.URL.Path) @@ -344,7 +343,7 @@ func (s *svc) handlePut(ctx context.Context, w http.ResponseWriter, r *http.Requ } func (s *svc) handleSpacesPut(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces_put") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces_put") defer span.End() sublog := appctx.GetLogger(ctx).With().Str("spaceid", spaceID).Str("path", r.URL.Path).Logger() diff --git a/internal/http/services/owncloud/ocdav/trashbin.go b/internal/http/services/owncloud/ocdav/trashbin.go index 50dcb3fd2c..e7dc3ee49e 100644 --- a/internal/http/services/owncloud/ocdav/trashbin.go +++ b/internal/http/services/owncloud/ocdav/trashbin.go @@ -35,7 +35,6 @@ import ( "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/propfind" "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/spacelookup" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -177,7 +176,7 @@ func (h *TrashbinHandler) Handler(s *svc) http.Handler { } func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s *svc, ref *provider.Reference, refBase, key, itemPath string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "list_trashbin") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "list_trashbin") defer span.End() sublog := appctx.GetLogger(ctx).With().Logger() @@ -453,7 +452,7 @@ func (h *TrashbinHandler) itemToPropResponse(ctx context.Context, s *svc, spaceI } func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc, ref, dst *provider.Reference, key, itemPath string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "restore") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "restore") defer span.End() sublog := appctx.GetLogger(ctx).With().Logger() @@ -581,7 +580,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc // delete has only a key func (h *TrashbinHandler) delete(w http.ResponseWriter, r *http.Request, s *svc, ref *provider.Reference, key, itemPath string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "erase") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "erase") defer span.End() sublog := appctx.GetLogger(ctx).With().Interface("reference", ref).Str("key", key).Str("item_path", itemPath).Logger() diff --git a/internal/http/services/owncloud/ocdav/tus.go b/internal/http/services/owncloud/ocdav/tus.go index 4e074e0f24..be29b79ca9 100644 --- a/internal/http/services/owncloud/ocdav/tus.go +++ b/internal/http/services/owncloud/ocdav/tus.go @@ -38,14 +38,13 @@ import ( "github.com/cs3org/reva/v2/pkg/appctx" "github.com/cs3org/reva/v2/pkg/rhttp" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/rs/zerolog" tusd "github.com/tus/tusd/pkg/handler" ) func (s *svc) handlePathTusPost(w http.ResponseWriter, r *http.Request, ns string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "tus-post") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "tus-post") defer span.End() // read filename from metadata @@ -71,7 +70,7 @@ func (s *svc) handlePathTusPost(w http.ResponseWriter, r *http.Request, ns strin } func (s *svc) handleSpacesTusPost(w http.ResponseWriter, r *http.Request, spaceID string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "spaces-tus-post") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "spaces-tus-post") defer span.End() // read filename from metadata diff --git a/internal/http/services/owncloud/ocdav/versions.go b/internal/http/services/owncloud/ocdav/versions.go index 0d537ae4fa..37b2d935d4 100644 --- a/internal/http/services/owncloud/ocdav/versions.go +++ b/internal/http/services/owncloud/ocdav/versions.go @@ -27,7 +27,6 @@ import ( "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net" "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/propfind" "github.com/cs3org/reva/v2/pkg/storagespace" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" @@ -109,7 +108,7 @@ func (h *VersionsHandler) Handler(s *svc, rid *provider.ResourceId) http.Handler } func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request, s *svc, rid *provider.ResourceId) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "listVersions") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "listVersions") defer span.End() sublog := appctx.GetLogger(ctx).With().Interface("resourceid", rid).Logger() @@ -208,7 +207,7 @@ func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request, } func (h *VersionsHandler) doRestore(w http.ResponseWriter, r *http.Request, s *svc, rid *provider.ResourceId, key string) { - ctx, span := rtrace.Provider.Tracer(tracerName).Start(r.Context(), "restore") + ctx, span := s.tracerProvider.Tracer(tracerName).Start(r.Context(), "restore") defer span.End() sublog := appctx.GetLogger(ctx).With().Interface("resourceid", rid).Str("key", key).Logger() diff --git a/pkg/appctx/appctx.go b/pkg/appctx/appctx.go index 8c63c3ddae..c8a56537a2 100644 --- a/pkg/appctx/appctx.go +++ b/pkg/appctx/appctx.go @@ -21,7 +21,9 @@ package appctx import ( "context" + rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/rs/zerolog" + "go.opentelemetry.io/otel/trace" ) // DeletingSharedResource flags to a storage a shared resource is being deleted not by the owner. @@ -37,3 +39,15 @@ func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context { func GetLogger(ctx context.Context) *zerolog.Logger { return zerolog.Ctx(ctx) } + +// WithTracerProvider returns a context with an associated TracerProvider +func WithTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context { + return rtrace.ContextSetTracerProvider(ctx, p) +} + +// GetTracerProvider returns the TracerProvider associated with +// the given context. (Or the global default TracerProvider if there +// is no TracerProvider in the context) +func GetTracerProvider(ctx context.Context) trace.TracerProvider { + return rtrace.ContextGetTracerProvider(ctx) +} diff --git a/pkg/micro/ocdav/service.go b/pkg/micro/ocdav/service.go index aed0713162..52f377ca9d 100644 --- a/pkg/micro/ocdav/service.go +++ b/pkg/micro/ocdav/service.go @@ -36,6 +36,7 @@ import ( "go-micro.dev/v4/registry" "go-micro.dev/v4/server" "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" ) const ( @@ -62,7 +63,8 @@ func Service(opts ...Option) (micro.Service, error) { server.Version(sopts.config.VersionString), ) - revaService, err := ocdav.NewWith(&sopts.config, sopts.FavoriteManager, sopts.lockSystem, &sopts.Logger) + tp := rtrace.GetTracerProvider(sopts.TracingEnabled, sopts.TracingCollector, sopts.TracingEndpoint, sopts.Name) + revaService, err := ocdav.NewWith(&sopts.config, sopts.FavoriteManager, sopts.lockSystem, &sopts.Logger, tp) if err != nil { return nil, err } @@ -80,7 +82,7 @@ func Service(opts ...Option) (micro.Service, error) { // chi.RegisterMethod(ocdav.MethodReport) r := chi.NewRouter() - if err := useMiddlewares(r, &sopts, revaService); err != nil { + if err := useMiddlewares(r, &sopts, revaService, tp); err != nil { return nil, err } @@ -132,8 +134,7 @@ func setDefaults(sopts *Options) error { return nil } -func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service) error { - +func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service, tp trace.TracerProvider) error { // auth for _, v := range svc.Unprotected() { sopts.Logger.Info().Str("url", v).Msg("unprotected URL") @@ -145,7 +146,7 @@ func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service) error { "secret": sopts.JWTSecret, }, }, - }, svc.Unprotected()) + }, svc.Unprotected(), tp) if err != nil { return err } @@ -156,23 +157,22 @@ func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service) error { // tracing tm := func(h http.Handler) http.Handler { return h } if sopts.TracingEnabled { - tm = traceHandler("ocdav", sopts.TracingCollector, sopts.TracingEndpoint) + tm = traceHandler(tp, "ocdav") } // ctx - cm := appctx.New(sopts.Logger) + cm := appctx.New(sopts.Logger, tp) // actually register r.Use(tm, lm, authMiddle, cm) return nil } -func traceHandler(name string, collector string, endpoint string) func(http.Handler) http.Handler { - rtrace.SetTraceProvider(collector, endpoint, "ocdav-micro") +func traceHandler(tp trace.TracerProvider, name string) func(http.Handler) http.Handler { return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := rtrace.Propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) - t := rtrace.Provider.Tracer("reva") + t := tp.Tracer("reva") ctx, span := t.Start(ctx, name) defer span.End() diff --git a/pkg/rgrpc/rgrpc.go b/pkg/rgrpc/rgrpc.go index 5cc0520b7f..abb7b0d991 100644 --- a/pkg/rgrpc/rgrpc.go +++ b/pkg/rgrpc/rgrpc.go @@ -37,6 +37,7 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" + "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) @@ -115,15 +116,16 @@ func (c *config) init() { // Server is a gRPC server. type Server struct { - s *grpc.Server - conf *config - listener net.Listener - log zerolog.Logger - services map[string]Service + s *grpc.Server + conf *config + listener net.Listener + log zerolog.Logger + tracerProvider trace.TracerProvider + services map[string]Service } // NewServer returns a new Server. -func NewServer(m interface{}, log zerolog.Logger) (*Server, error) { +func NewServer(m interface{}, log zerolog.Logger, tp trace.TracerProvider) (*Server, error) { conf := &config{} if err := mapstructure.Decode(m, conf); err != nil { return nil, err @@ -131,7 +133,7 @@ func NewServer(m interface{}, log zerolog.Logger) (*Server, error) { conf.init() - server := &Server{conf: conf, log: log, services: map[string]Service{}} + server := &Server{conf: conf, log: log, tracerProvider: tp, services: map[string]Service{}} return server, nil } @@ -271,7 +273,7 @@ func (s *Server) getInterceptors(unprotected []string) ([]grpc.ServerOption, err return unaryTriples[i].Priority < unaryTriples[j].Priority }) - authUnary, err := auth.NewUnary(s.conf.Interceptors["auth"], unprotected) + authUnary, err := auth.NewUnary(s.conf.Interceptors["auth"], unprotected, s.tracerProvider) if err != nil { return nil, errors.Wrap(err, "rgrpc: error creating unary auth interceptor") } @@ -284,12 +286,12 @@ func (s *Server) getInterceptors(unprotected []string) ([]grpc.ServerOption, err unaryInterceptors = append(unaryInterceptors, otelgrpc.UnaryServerInterceptor( - otelgrpc.WithTracerProvider(rtrace.Provider), + otelgrpc.WithTracerProvider(s.tracerProvider), otelgrpc.WithPropagators(rtrace.Propagator)), ) unaryInterceptors = append([]grpc.UnaryServerInterceptor{ - appctx.NewUnary(s.log), + appctx.NewUnary(s.log, s.tracerProvider), token.NewUnary(), useragent.NewUnary(), log.NewUnary(), @@ -318,7 +320,7 @@ func (s *Server) getInterceptors(unprotected []string) ([]grpc.ServerOption, err return streamTriples[i].Priority < streamTriples[j].Priority }) - authStream, err := auth.NewStream(s.conf.Interceptors["auth"], unprotected) + authStream, err := auth.NewStream(s.conf.Interceptors["auth"], unprotected, s.tracerProvider) if err != nil { return nil, errors.Wrap(err, "rgrpc: error creating stream auth interceptor") } @@ -331,7 +333,7 @@ func (s *Server) getInterceptors(unprotected []string) ([]grpc.ServerOption, err streamInterceptors = append([]grpc.StreamServerInterceptor{ authStream, - appctx.NewStream(s.log), + appctx.NewStream(s.log, s.tracerProvider), token.NewStream(), useragent.NewStream(), log.NewStream(), diff --git a/pkg/rgrpc/todo/pool/pool.go b/pkg/rgrpc/todo/pool/pool.go index 2b0eff4a1a..2963518877 100644 --- a/pkg/rgrpc/todo/pool/pool.go +++ b/pkg/rgrpc/todo/pool/pool.go @@ -96,7 +96,7 @@ func NewConn(endpoint string) (*grpc.ClientConn, error) { ), grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor( otelgrpc.WithTracerProvider( - rtrace.Provider, + rtrace.DefaultProvider(), ), otelgrpc.WithPropagators( rtrace.Propagator, @@ -105,7 +105,7 @@ func NewConn(endpoint string) (*grpc.ClientConn, error) { grpc.WithUnaryInterceptor( otelgrpc.UnaryClientInterceptor( otelgrpc.WithTracerProvider( - rtrace.Provider, + rtrace.DefaultProvider(), ), otelgrpc.WithPropagators( rtrace.Propagator, diff --git a/pkg/rhttp/rhttp.go b/pkg/rhttp/rhttp.go index bb527a8046..990d778503 100644 --- a/pkg/rhttp/rhttp.go +++ b/pkg/rhttp/rhttp.go @@ -38,13 +38,14 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog" "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" ) // name is the Tracer name used to identify this instrumentation library. const tracerName = "rhttp" // New returns a new server -func New(m interface{}, l zerolog.Logger) (*Server, error) { +func New(m interface{}, l zerolog.Logger, tp trace.TracerProvider) (*Server, error) { conf := &config{} if err := mapstructure.Decode(m, conf); err != nil { return nil, err @@ -54,26 +55,28 @@ func New(m interface{}, l zerolog.Logger) (*Server, error) { httpServer := &http.Server{} s := &Server{ - httpServer: httpServer, - conf: conf, - svcs: map[string]global.Service{}, - unprotected: []string{}, - handlers: map[string]http.Handler{}, - log: l, + httpServer: httpServer, + conf: conf, + svcs: map[string]global.Service{}, + unprotected: []string{}, + handlers: map[string]http.Handler{}, + log: l, + tracerProvider: tp, } return s, nil } // Server contains the server info. type Server struct { - httpServer *http.Server - conf *config - listener net.Listener - svcs map[string]global.Service // map key is svc Prefix - unprotected []string - handlers map[string]http.Handler - middlewares []*middlewareTriple - log zerolog.Logger + httpServer *http.Server + conf *config + listener net.Listener + svcs map[string]global.Service // map key is svc Prefix + unprotected []string + handlers map[string]http.Handler + middlewares []*middlewareTriple + log zerolog.Logger + tracerProvider trace.TracerProvider } type config struct { @@ -210,7 +213,7 @@ func (s *Server) registerServices() error { } // instrument services with opencensus tracing. - h := traceHandler(svcName, svc.Handler()) + h := traceHandler(svcName, svc.Handler(), s.tracerProvider) s.handlers[svc.Prefix()] = h s.svcs[svc.Prefix()] = svc s.unprotected = append(s.unprotected, getUnprotected(svc.Prefix(), svc.Unprotected())...) @@ -268,13 +271,13 @@ func (s *Server) getHandler() (http.Handler, error) { for _, triple := range s.middlewares { s.log.Info().Msgf("chaining http middleware %s with priority %d", triple.Name, triple.Priority) - handler = triple.Middleware(traceHandler(triple.Name, handler)) + handler = triple.Middleware(traceHandler(triple.Name, handler, s.tracerProvider)) } for _, v := range s.unprotected { s.log.Info().Msgf("unprotected URL: %s", v) } - authMiddle, err := auth.New(s.conf.Middlewares["auth"], s.unprotected) + authMiddle, err := auth.New(s.conf.Middlewares["auth"], s.unprotected, s.tracerProvider) if err != nil { return nil, errors.Wrap(err, "rhttp: error creating auth middleware") } @@ -293,19 +296,19 @@ func (s *Server) getHandler() (http.Handler, error) { coreMiddlewares = append(coreMiddlewares, &middlewareTriple{Middleware: authMiddle, Name: "auth"}) coreMiddlewares = append(coreMiddlewares, &middlewareTriple{Middleware: log.New(), Name: "log"}) - coreMiddlewares = append(coreMiddlewares, &middlewareTriple{Middleware: appctx.New(s.log), Name: "appctx"}) + coreMiddlewares = append(coreMiddlewares, &middlewareTriple{Middleware: appctx.New(s.log, s.tracerProvider), Name: "appctx"}) for _, triple := range coreMiddlewares { - handler = triple.Middleware(traceHandler(triple.Name, handler)) + handler = triple.Middleware(traceHandler(triple.Name, handler, s.tracerProvider)) } return handler, nil } -func traceHandler(name string, h http.Handler) http.Handler { +func traceHandler(name string, h http.Handler, tp trace.TracerProvider) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := rtrace.Propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) - t := rtrace.Provider.Tracer(tracerName) + t := tp.Tracer(tracerName) ctx, span := t.Start(ctx, name) defer span.End() diff --git a/pkg/storage/utils/decomposedfs/decomposedfs.go b/pkg/storage/utils/decomposedfs/decomposedfs.go index 65a033ea9b..bb5d8d5856 100644 --- a/pkg/storage/utils/decomposedfs/decomposedfs.go +++ b/pkg/storage/utils/decomposedfs/decomposedfs.go @@ -50,7 +50,6 @@ import ( "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/tree" "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/xattrs" "github.com/cs3org/reva/v2/pkg/storage/utils/templates" - rtrace "github.com/cs3org/reva/v2/pkg/trace" "github.com/cs3org/reva/v2/pkg/utils" "github.com/pkg/errors" "go.opentelemetry.io/otel/codes" @@ -347,7 +346,7 @@ func (fs *Decomposedfs) TouchFile(ctx context.Context, ref *provider.Reference) // To mimic the eos and owncloud driver we only allow references as children of the "/Shares" folder // FIXME: This comment should explain briefly what a reference is in this context. func (fs *Decomposedfs) CreateReference(ctx context.Context, p string, targetURI *url.URL) (err error) { - ctx, span := rtrace.Provider.Tracer("reva").Start(ctx, "CreateReference") + ctx, span := appctx.GetTracerProvider(ctx).Tracer("reva").Start(ctx, "CreateReference") defer span.End() p = strings.Trim(p, "/") @@ -496,7 +495,7 @@ func (fs *Decomposedfs) ListFolder(ctx context.Context, ref *provider.Reference, return } - ctx, span := rtrace.Provider.Tracer(tracerName).Start(ctx, "ListFolder") + ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "ListFolder") defer span.End() if !n.Exists { diff --git a/pkg/trace/trace.go b/pkg/trace/trace.go index 9c383a28af..9aab17679d 100644 --- a/pkg/trace/trace.go +++ b/pkg/trace/trace.go @@ -19,10 +19,12 @@ package trace import ( + "context" "fmt" "net/url" "os" "strings" + "sync" "go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/propagation" @@ -34,14 +36,63 @@ import ( var ( // Propagator is the default Reva propagator. - Propagator = propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{}) - - // Provider is the default Reva tracer provider. - Provider = trace.NewNoopTracerProvider() + Propagator = propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{}) + defaultProvider = revaDefaultTracerProvider{ + provider: trace.NewNoopTracerProvider(), + } ) -// SetTraceProvider sets the TracerProvider at a package level. -func SetTraceProvider(collectorEndpoint string, agentEndpoint, serviceName string) { +type revaDefaultTracerProvider struct { + mutex sync.RWMutex + initialized bool + provider trace.TracerProvider +} + +type ctxKey struct{} + +// ContextSetTracerProvider returns a copy of ctx with p associated. +func ContextSetTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context { + if tp, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok { + if tp == p { + return ctx + } + } + return context.WithValue(ctx, ctxKey{}, p) +} + +// ContextGetTracerProvider returns the TracerProvider associated with the ctx. +// If no TracerProvider is associated is associated, the global default TracerProvider +// is returned +func ContextGetTracerProvider(ctx context.Context) trace.TracerProvider { + if p, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok { + return p + } + return DefaultProvider() +} + +// InitDefaultTracerProvider initializes a global default TracerProvider at a package level. +func InitDefaultTracerProvider(collectorEndpoint string, agentEndpoint string) { + defaultProvider.mutex.Lock() + defer defaultProvider.mutex.Unlock() + if !defaultProvider.initialized { + defaultProvider.provider = GetTracerProvider(true, collectorEndpoint, agentEndpoint, "reva default provider") + } + defaultProvider.initialized = true +} + +// DefaultProvider returns the "global" default TracerProvider +func DefaultProvider() trace.TracerProvider { + defaultProvider.mutex.RLock() + defer defaultProvider.mutex.RUnlock() + return defaultProvider.provider +} + +// GetTracerProvider returns a new TracerProvider, configure for the specified service +func GetTracerProvider(enabled bool, collectorEndpoint string, agentEndpoint, serviceName string) trace.TracerProvider { + if !enabled { + return trace.NewNoopTracerProvider() + } + // default to 'reva' as service name if not set if serviceName == "" { serviceName = "reva" @@ -82,7 +133,7 @@ func SetTraceProvider(collectorEndpoint string, agentEndpoint, serviceName strin panic(err) } - Provider = sdktrace.NewTracerProvider( + return sdktrace.NewTracerProvider( sdktrace.WithBatcher(exp), sdktrace.WithResource(resource.NewWithAttributes( semconv.SchemaURL,