Skip to content

Commit

Permalink
Attach TracerProvider to context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rhafer committed Jun 15, 2022
1 parent 07b0d40 commit 4cd127e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
1 change: 1 addition & 0 deletions internal/grpc/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewUnary(log zerolog.Logger, tp trace.TracerProvider) grpc.UnaryServerInter

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
}
Expand Down
4 changes: 1 addition & 3 deletions internal/grpc/services/gateway/usershareprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.DefaultProvider().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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.DefaultProvider().Tracer(tracerName).Start(ctx, "CreateContainer")
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "CreateContainer")
defer span.End()

span.SetAttributes(attribute.KeyValue{
Expand Down Expand Up @@ -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.DefaultProvider().Tracer(tracerName).Start(ctx, "Delete")
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Delete")
defer span.End()

span.SetAttributes(attribute.KeyValue{
Expand Down Expand Up @@ -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.DefaultProvider().Tracer(tracerName).Start(ctx, "Move")
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Move")
defer span.End()

span.SetAttributes(
Expand Down Expand Up @@ -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.DefaultProvider().Tracer(tracerName).Start(ctx, "Stat")
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "Stat")
defer span.End()

span.SetAttributes(
Expand Down
1 change: 1 addition & 0 deletions internal/http/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func handler(log zerolog.Logger, tp trace.TracerProvider, h http.Handler) http.H

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)
})
Expand Down
5 changes: 2 additions & 3 deletions internal/http/services/owncloud/ocdav/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.DefaultProvider().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)
Expand Down Expand Up @@ -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.DefaultProvider().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()
Expand Down
7 changes: 3 additions & 4 deletions internal/http/services/owncloud/ocdav/propfind/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.DefaultProvider().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?
Expand Down Expand Up @@ -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.DefaultProvider().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()
Expand Down Expand Up @@ -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.DefaultProvider().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))
Expand Down
10 changes: 10 additions & 0 deletions pkg/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -37,3 +39,11 @@ func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context {
func GetLogger(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
}

func WithTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context {
return rtrace.ContextSetTracerProvider(ctx, p)
}

func GetTracerProvider(ctx context.Context) trace.TracerProvider {
return rtrace.ContextGetTracerProvider(ctx)
}
5 changes: 2 additions & 3 deletions pkg/storage/utils/decomposedfs/decomposedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.DefaultProvider().Tracer("reva").Start(ctx, "CreateReference")
ctx, span := appctx.GetTracerProvider(ctx).Tracer("reva").Start(ctx, "CreateReference")
defer span.End()

p = strings.Trim(p, "/")
Expand Down Expand Up @@ -496,7 +495,7 @@ func (fs *Decomposedfs) ListFolder(ctx context.Context, ref *provider.Reference,
return
}

ctx, span := rtrace.DefaultProvider().Tracer(tracerName).Start(ctx, "ListFolder")
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "ListFolder")
defer span.End()

if !n.Exists {
Expand Down
19 changes: 19 additions & 0 deletions pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package trace

import (
"context"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -47,6 +48,24 @@ type revaDefaultTracerProvider struct {
provider trace.TracerProvider
}

type ctxKey struct{}

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)
}

func ContextGetTracerProvider(ctx context.Context) trace.TracerProvider {
if p, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok {
return p
}
return trace.NewNoopTracerProvider()
}

// InitDefaultTracerProvider initializes a global default TracerProvider at a package level.
func InitDefaultTracerProvider(collectorEndpoint string, agentEndpoint string) {
defaultProvider.mutex.Lock()
Expand Down

0 comments on commit 4cd127e

Please sign in to comment.