diff --git a/idp/pkg/service/v0/tracing.go b/idp/pkg/service/v0/tracing.go index 1857cf0d8..2b4b3cf8c 100644 --- a/idp/pkg/service/v0/tracing.go +++ b/idp/pkg/service/v0/tracing.go @@ -19,5 +19,5 @@ type tracing struct { // ServeHTTP implements the Service interface. func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) { - middleware.Trace(t.next).ServeHTTP(w, r) + middleware.TraceContext(t.next).ServeHTTP(w, r) } diff --git a/ocis-pkg/middleware/tracing.go b/ocis-pkg/middleware/tracing.go index 30d12c959..60ba4572d 100644 --- a/ocis-pkg/middleware/tracing.go +++ b/ocis-pkg/middleware/tracing.go @@ -11,8 +11,8 @@ var propagator = propagation.NewCompositeTextMapPropagator( propagation.TraceContext{}, ) -// Trace unpacks the request context looking for an existing trace id. -func Trace(next http.Handler) http.Handler { +// TraceContext unpacks the request context looking for an existing trace id. +func TraceContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) propagator.Inject(ctx, propagation.HeaderCarrier(r.Header)) diff --git a/ocs/pkg/middleware/logtrace.go b/ocs/pkg/middleware/logtrace.go new file mode 100644 index 000000000..bea2d339d --- /dev/null +++ b/ocs/pkg/middleware/logtrace.go @@ -0,0 +1,24 @@ +package middleware + +import ( + "net/http" + + ocstracing "github.com/owncloud/ocis/ocs/pkg/tracing" + "go.opentelemetry.io/otel/propagation" +) + +var propagator = propagation.NewCompositeTextMapPropagator( + propagation.Baggage{}, + propagation.TraceContext{}, +) + +// LogTrace unpacks the request context looking for an existing trace id. +func LogTrace(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx, span := ocstracing.TraceProvider.Tracer("ocs").Start(r.Context(), r.URL.Path) + defer span.End() + + propagator.Inject(ctx, propagation.HeaderCarrier(r.Header)) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/ocs/pkg/server/http/server.go b/ocs/pkg/server/http/server.go index d5714e072..2dd365501 100644 --- a/ocs/pkg/server/http/server.go +++ b/ocs/pkg/server/http/server.go @@ -4,6 +4,7 @@ import ( "github.com/asim/go-micro/v3" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" + ocsmw "github.com/owncloud/ocis/ocs/pkg/middleware" svc "github.com/owncloud/ocis/ocs/pkg/service/v0" ) @@ -25,19 +26,15 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.Trace, + middleware.TraceContext, middleware.RealIP, + ocsmw.LogTrace, middleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, - middleware.Version( - options.Config.Service.Name, - options.Config.Service.Version, - ), - middleware.Logger( - options.Logger, - ), + middleware.Version(options.Config.Service.Name, options.Config.Service.Version), + middleware.Logger(options.Logger), ), ) diff --git a/ocs/pkg/service/v0/groups.go b/ocs/pkg/service/v0/groups.go index 57c3f6c7c..a8c461d00 100644 --- a/ocs/pkg/service/v0/groups.go +++ b/ocs/pkg/service/v0/groups.go @@ -9,15 +9,15 @@ import ( "regexp" "strconv" - "github.com/cs3org/reva/pkg/user" - merrors "github.com/asim/go-micro/v3/errors" + "github.com/cs3org/reva/pkg/user" "github.com/go-chi/chi" "github.com/go-chi/render" - accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/owncloud/ocis/ocs/pkg/service/v0/data" "github.com/owncloud/ocis/ocs/pkg/service/v0/response" + ocstracing "github.com/owncloud/ocis/ocs/pkg/tracing" + "go.opentelemetry.io/otel/attribute" ) // ListUserGroups lists a users groups @@ -32,6 +32,16 @@ func (o Ocs) ListUserGroups(w http.ResponseWriter, r *http.Request) { if u.Username == userid { // the OCS API is a REST API and it uses the username to look for groups. If the id from the user in the context // differs from that of the url we can assume we are an admin because we are past the selfOrAdmin middleware. + + if o.config.Tracing.Enabled { + _, span := ocstracing.TraceProvider. + Tracer("ocs"). + Start(r.Context(), "ListUserGroups") + defer span.End() + + span.SetAttributes(attribute.Any("groups", u.Groups)) + } + if len(u.Groups) > 0 { mustNotFail(render.Render(w, r, response.DataRender(&data.Groups{Groups: u.Groups}))) return @@ -82,6 +92,16 @@ func (o Ocs) ListUserGroups(w http.ResponseWriter, r *http.Request) { } o.logger.Error().Err(err).Int("count", len(groups)).Str("userid", account.Id).Msg("listing groups for user") + + if o.config.Tracing.Enabled { + _, span := ocstracing.TraceProvider. + Tracer("ocs"). + Start(r.Context(), "ListUserGroups") + defer span.End() + + span.SetAttributes(attribute.Any("groups", groups)) + } + mustNotFail(render.Render(w, r, response.DataRender(&data.Groups{Groups: groups}))) } @@ -245,6 +265,15 @@ func (o Ocs) ListGroups(w http.ResponseWriter, r *http.Request) { groups = append(groups, res.Groups[i].OnPremisesSamAccountName) } + if o.config.Tracing.Enabled { + _, span := ocstracing.TraceProvider. + Tracer("ocs"). + Start(r.Context(), "ListGroups") + defer span.End() + + span.SetAttributes(attribute.Any("groups", groups)) + } + mustNotFail(render.Render(w, r, response.DataRender(&data.Groups{Groups: groups}))) } diff --git a/ocs/pkg/service/v0/tracing.go b/ocs/pkg/service/v0/tracing.go index 341aa923b..5403996ce 100644 --- a/ocs/pkg/service/v0/tracing.go +++ b/ocs/pkg/service/v0/tracing.go @@ -19,7 +19,7 @@ type tracing struct { // ServeHTTP implements the Service interface. func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) { - middleware.Trace(t.next).ServeHTTP(w, r) + middleware.TraceContext(t.next).ServeHTTP(w, r) } // GetConfig implements the Service interface. diff --git a/ocs/pkg/service/v0/users.go b/ocs/pkg/service/v0/users.go index 4f86f7dd5..75a052365 100644 --- a/ocs/pkg/service/v0/users.go +++ b/ocs/pkg/service/v0/users.go @@ -9,6 +9,10 @@ import ( "strconv" "strings" + "go.opentelemetry.io/otel/attribute" + + ocstracing "github.com/owncloud/ocis/ocs/pkg/tracing" + "github.com/asim/go-micro/plugins/client/grpc/v3" merrors "github.com/asim/go-micro/v3/errors" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" @@ -25,7 +29,6 @@ import ( accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/owncloud/ocis/ocs/pkg/service/v0/data" "github.com/owncloud/ocis/ocs/pkg/service/v0/response" - ocstracing "github.com/owncloud/ocis/ocs/pkg/tracing" storepb "github.com/owncloud/ocis/store/pkg/proto/v0" "github.com/pkg/errors" "google.golang.org/genproto/protobuf/field_mask" @@ -91,16 +94,13 @@ func (o Ocs) GetUser(w http.ResponseWriter, r *http.Request) { var account *accounts.Account var err error - ctx, span := ocstracing.TraceProvider.Tracer("ocs").Start(r.Context(), "GetUser") - defer span.End() - switch { case userid == "": mustNotFail(render.Render(w, r, response.ErrRender(data.MetaBadRequest.StatusCode, "missing user in context"))) case o.config.AccountBackend == "accounts": - account, err = o.fetchAccountByUsername(ctx, userid) + account, err = o.fetchAccountByUsername(r.Context(), userid) case o.config.AccountBackend == "cs3": - account, err = o.fetchAccountFromCS3Backend(ctx, userid) + account, err = o.fetchAccountFromCS3Backend(r.Context(), userid) default: o.logger.Fatal().Msgf("Invalid accounts backend type '%s'", o.config.AccountBackend) } @@ -147,6 +147,16 @@ func (o Ocs) GetUser(w http.ResponseWriter, r *http.Request) { Definition: "default", }, } + + if o.config.Tracing.Enabled { + _, span := ocstracing.TraceProvider. + Tracer("ocs"). + Start(r.Context(), "GetUser") + defer span.End() + + span.SetAttributes(attribute.Any("user", d)) + } + mustNotFail(render.Render(w, r, response.DataRender(d))) } diff --git a/proxy/pkg/command/server.go b/proxy/pkg/command/server.go index 0686f984e..48420db85 100644 --- a/proxy/pkg/command/server.go +++ b/proxy/pkg/command/server.go @@ -178,7 +178,7 @@ func loadMiddlewares(ctx context.Context, l log.Logger, cfg *config.Config) alic return alice.New( // first make sure we log all requests and redirect to https if necessary - pkgmiddleware.Trace, + pkgmiddleware.TraceContext, pkgmiddleware.RealIP, pkgmiddleware.RequestID, middleware.AccessLog(l), diff --git a/proxy/pkg/proxy/proxy.go b/proxy/pkg/proxy/proxy.go index 97c6ade90..a7d27a7c5 100644 --- a/proxy/pkg/proxy/proxy.go +++ b/proxy/pkg/proxy/proxy.go @@ -226,6 +226,7 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request defer span.End() proxytracing.Propagator.Inject(ctx, propagation.HeaderCarrier(r.Header)) } + p.ReverseProxy.ServeHTTP(w, r.WithContext(ctx)) }