mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2025-12-31 01:10:20 -06:00
add test spans
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
24
ocs/pkg/middleware/logtrace.go
Normal file
24
ocs/pkg/middleware/logtrace.go
Normal file
@@ -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))
|
||||
})
|
||||
}
|
||||
@@ -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),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -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})))
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user