Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Tracing #22

Open
wants to merge 47 commits into
base: cernbox
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ba92ab0
Remove tracing
vascoguita May 20, 2022
ccdc334
tracing: add tracing package which exports function to set the global…
vascoguita May 25, 2022
72bbefb
tracing: change configuration
vascoguita Jun 1, 2022
f420d4a
tracing: instrument the storageprovider part of the gateway grpc service
vascoguita Jun 7, 2022
399a4cc
tracing: instrument grpc storageprovider service
vascoguita Jun 8, 2022
1665955
tracing: instrument rgrpc
vascoguita Jun 10, 2022
fed2684
tracing: instrument the grpc client connections with opentelemetry
vascoguita Jun 20, 2022
a5feb64
tracing: instrument authprovider and authregistry with opentelemetry
vascoguita Jun 20, 2022
102b744
tracing: instrument authctx interceptor and token interceptor
vascoguita Jun 21, 2022
b2eed74
tracing: instrument log interceptor and useragent interceptor
vascoguita Jun 21, 2022
ca7feee
tracing: instrument recovery interceptor
vascoguita Jun 21, 2022
1e54ab9
tracing: instrument auth interceptor
vascoguita Jun 21, 2022
7cc43cf
tracing: decorate spans with hostname
vascoguita Jun 21, 2022
92536d8
tracing: add jaeger propagator
vascoguita Jun 21, 2022
81ffb86
tracing: add trace provider registry and otel interceptor selectors
vascoguita Jun 27, 2022
d08c5e3
tracing: instrument applicationauth, appprovider and appregistry with…
vascoguita Jun 27, 2022
f835925
tracing: refactor trace provider and interceptor registry
vascoguita Jun 30, 2022
008db92
tracing: instrument datatx, groupprovider and storageregistry with op…
vascoguita Jul 5, 2022
3fa7a9b
tracing: set tracerprovider in context
vascoguita Jul 6, 2022
1774873
tracing: instrument grpc eventsmiddleware and readonly interceptors w…
vascoguita Jul 6, 2022
0b5e757
tracing: add grpc client conn interceptors
vascoguita Jul 7, 2022
a630ed7
tracing: add logging and refactor
vascoguita Jul 7, 2022
08b4801
tracing: refactor
vascoguita Jul 11, 2022
4773935
tracing: add thread protection
vascoguita Jul 11, 2022
1f3c72d
tracing: change registries to sync maps
vascoguita Jul 21, 2022
7f00bb6
tracing: instrument ocmcore, ocminvitemanager, ocmproviderauthorizer,…
vascoguita Jul 21, 2022
7d753f1
tracing: instrument owncloud HTTP service (ocdav and ocs) with OpenTe…
vascoguita Jul 27, 2022
c613ee1
tracing: instrument HTTP services with otelhttp handler
vascoguita Jul 27, 2022
4809461
tracing: instrument main ocdav handler
vascoguita Jul 28, 2022
c4051bd
tracing: instrument owncloud HTTP service functions with context from…
vascoguita Jul 28, 2022
d27eb8b
tracing: add span start function that stores tracing service in the H…
vascoguita Jul 29, 2022
d9656d8
tracing: refactor span start functions
vascoguita Jul 29, 2022
b9dfb19
tracing: instrument appprovider, archiver, datagateway, dataprovider,…
vascoguita Aug 3, 2022
3630779
tracing: remove faulty tracing shutdown
vascoguita Aug 8, 2022
c5be208
tracing: compose grpc service structure with tracing middleware
vascoguita Aug 10, 2022
087e5da
tracing: fix issue with GRPC services with the same API and refactor
vascoguita Aug 11, 2022
70a04cd
tracing: refactor
vascoguita Aug 11, 2022
b284e1b
tracing: fix GRPC interceptors
vascoguita Aug 24, 2022
ce7f512
tracing: Compose HTTP service interface to instrument middleware
vascoguita Aug 25, 2022
1d9a5f3
tracing: instrument HTTP interceptors
vascoguita Aug 29, 2022
5513120
tracing: Add username atribute to auth HTTP interceptor
vascoguita Aug 29, 2022
607e748
tracing: instrument GRPC clients and the share, storage, utils, cbox …
vascoguita Aug 29, 2022
114dcf7
tracing: refactor HTTP middleware
vascoguita Aug 30, 2022
99ebe3d
tracing: fix type casting problem in rhttp
vascoguita Aug 31, 2022
aca9656
tracing: fix after rebase
vascoguita Aug 31, 2022
35e6f41
tracing: add TraceID to logs
vascoguita Sep 21, 2022
3ae4274
tracing: rebase and instrument mailer with OTel
vascoguita Oct 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions cmd/revad/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rhttp"
"github.com/cs3org/reva/pkg/sharedconf"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/cs3org/reva/pkg/tracing"
"github.com/cs3org/reva/pkg/utils"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -73,25 +73,17 @@ func RunWithOptions(mainConf map[string]interface{}, pidFile string, opts ...Opt
}

type coreConf struct {
MaxCPUs string `mapstructure:"max_cpus"`
TracingEnabled bool `mapstructure:"tracing_enabled"`
TracingEndpoint string `mapstructure:"tracing_endpoint"`
TracingCollector string `mapstructure:"tracing_collector"`
TracingServiceName string `mapstructure:"tracing_service_name"`

// TracingService specifies the service. i.e OpenCensus, OpenTelemetry, OpenTracing...
TracingService string `mapstructure:"tracing_service"`
MaxCPUs string `mapstructure:"max_cpus"`
}

func run(mainConf map[string]interface{}, coreConf *coreConf, logger *zerolog.Logger, filename string) {
host, _ := os.Hostname()
logger.Info().Msgf("host info: %s", host)

if coreConf.TracingEnabled {
initTracing(coreConf)
}
initCPUCount(coreConf, logger)

tracing.Init(mainConf["tracing"], tracing.WithLogger(logger.With().Str("pkg", "tracing").Logger()))

servers := initServers(mainConf, logger)
watcher, err := initWatcher(logger, filename)
if err != nil {
Expand Down Expand Up @@ -148,10 +140,6 @@ 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 initCPUCount(conf *coreConf, log *zerolog.Logger) {
ncpus, err := adjustCPU(conf.MaxCPUs)
if err != nil {
Expand Down Expand Up @@ -264,7 +252,8 @@ func getHTTPServer(conf interface{}, l *zerolog.Logger) (*rhttp.Server, error) {
return s, nil
}

// adjustCPU parses string cpu and sets GOMAXPROCS
// adjustCPU parses string cpu and sets GOMAXPROCS
//
// according to its value. It accepts either
// a number (e.g. 3) or a percent (e.g. 50%).
// Default is to use all available cores.
Expand Down Expand Up @@ -311,15 +300,6 @@ func parseCoreConfOrDie(v interface{}) *coreConf {
os.Exit(1)
}

// tracing defaults to enabled if not explicitly configured
if v == nil {
c.TracingEnabled = true
c.TracingEndpoint = "localhost:6831"
} else if _, ok := v.(map[string]interface{})["tracing_enabled"]; !ok {
c.TracingEnabled = true
c.TracingEndpoint = "localhost:6831"
}

return c
}

Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/rs/zerolog v1.27.0
github.com/sciencemesh/meshdirectory-web v1.0.4
github.com/sethvargo/go-password v0.2.0
github.com/stretchr/testify v1.7.2
github.com/stretchr/testify v1.8.0
github.com/studio-b12/gowebdav v0.0.0-20210917133250-a3a86976a1df
github.com/thanhpk/randstr v1.0.4
github.com/tidwall/pretty v1.2.0 // indirect
Expand All @@ -68,17 +68,19 @@ require (
go-micro.dev/v4 v4.3.1-0.20211108085239-0c2041e43908
go.opencensus.io v0.23.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.32.0
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0
go.opentelemetry.io/contrib/propagators/jaeger v1.7.0
go.opentelemetry.io/otel v1.8.0
go.opentelemetry.io/otel/exporters/jaeger v1.7.0
go.opentelemetry.io/otel/sdk v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
go.opentelemetry.io/otel/trace v1.8.0
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29
golang.org/x/image v0.0.0-20220617043117-41969df76e82 // indirect
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
Expand Down
23 changes: 18 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
Expand Down Expand Up @@ -946,17 +948,19 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/studio-b12/gowebdav v0.0.0-20210917133250-a3a86976a1df h1:C+J/LwTqP8gRPt1MdSzBNZP0OYuDm5wsmDKgwpLjYzo=
github.com/studio-b12/gowebdav v0.0.0-20210917133250-a3a86976a1df/go.mod h1:gCcfDlA1Y7GqOaeEKw5l9dOGx1VLdc/HuQSlQAaZ30s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -1025,14 +1029,22 @@ go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.32.0 h1:WenoaOMNP71oq3KkMZ/jnxI9xU/JSCLw8yZILSI2lfU=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.32.0/go.mod h1:J0dBVrt7dPS/lKJyQoW0xzQiUr4r2Ik1VwPjAUWnofI=
go.opentelemetry.io/otel v1.7.0 h1:Z2lA3Tdch0iDcrhJXDIlC94XE+bxok1F9B+4Lz/lGsM=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0 h1:Z0lVKLXU+jxGf3ANoh+UWx9Ai5bjpQVnZXI1zEzvqS0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0/go.mod h1:U5rUt7Rw6zuORsWNfpMRy8XMNKLrmIlv/4HgLVW/d5M=
go.opentelemetry.io/contrib/propagators/jaeger v1.7.0 h1:x2mXKtONfOJFfNFSx4QXFx1fms6bKIPVvWvgdiaPdRI=
go.opentelemetry.io/contrib/propagators/jaeger v1.7.0/go.mod h1:kt2lNImfxV6dETRsDCENd6jU6G0mPRS+P0qlNuvtkTE=
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
go.opentelemetry.io/otel v1.8.0 h1:zcvBFizPbpa1q7FehvFiHbQwGzmPILebO0tyqIR5Djg=
go.opentelemetry.io/otel v1.8.0/go.mod h1:2pkj+iMj0o03Y+cW6/m8Y4WkRdYN3AvCXCnzRMp9yvM=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0 h1:wXgjiRldljksZkZrldGVe6XrG9u3kYDyQmkZwmm5dI0=
go.opentelemetry.io/otel/exporters/jaeger v1.7.0/go.mod h1:PwQAOqBgqbLQRKlj466DuD2qyMjbtcPpfPfj+AqbSBs=
go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9spDTqUs=
go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A=
go.opentelemetry.io/otel/sdk v1.7.0 h1:4OmStpcKVOfvDOgCt7UriAPtKolwIhxpnSNI/yK+1B0=
go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe374q6cZwUU=
go.opentelemetry.io/otel/trace v1.7.0 h1:O37Iogk1lEkMRXewVtZ1BBTVn5JEp8GrJvP92bJqC6o=
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
go.opentelemetry.io/otel/trace v1.8.0 h1:cSy0DF9eGI5WIfNwZ1q2iUyGj00tGzP24dE1lOlHrfY=
go.opentelemetry.io/otel/trace v1.8.0/go.mod h1:0Bt3PXY8w+3pheS3hQUt+wow8b1ojPaTBoTCh2zIFI4=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -1316,8 +1328,9 @@ golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201113234701-d7a72108b828/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
25 changes: 10 additions & 15 deletions internal/grpc/interceptors/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@ import (
"context"

"github.com/cs3org/reva/pkg/appctx"
rtrace "github.com/cs3org/reva/pkg/trace"
"github.com/cs3org/reva/pkg/tracing"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
)

const tracerName = "appctx"

// NewUnary returns a new unary interceptor that creates the application context.
func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor {
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
span := trace.SpanFromContext(ctx)
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "appctx UnaryServerInterceptor")
defer span.End()
if !span.SpanContext().HasTraceID() {
ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc unary")
}

sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger()
sub := log.With().Str("TraceID", span.SpanContext().TraceID().String()).Logger()
ctx = appctx.WithLogger(ctx, &sub)
res, err := handler(ctx, req)
return res, err
Expand All @@ -50,24 +48,21 @@ func NewUnary(log zerolog.Logger) grpc.UnaryServerInterceptor {
func NewStream(log zerolog.Logger) grpc.StreamServerInterceptor {
interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
span := trace.SpanFromContext(ctx)
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "appctx StreamServerInterceptor")
defer span.End()

if !span.SpanContext().HasTraceID() {
ctx, span = rtrace.Provider.Tracer("grpc").Start(ctx, "grpc stream")
}

sub := log.With().Str("traceid", span.SpanContext().TraceID().String()).Logger()
sub := log.With().Str("TraceID", span.SpanContext().TraceID().String()).Logger()
ctx = appctx.WithLogger(ctx, &sub)

wrapped := newWrappedServerStream(ctx, ss)
err := handler(srv, wrapped)
return err
return handler(srv, wrapped)
}
return interceptor
}

func newWrappedServerStream(ctx context.Context, ss grpc.ServerStream) *wrappedServerStream {
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "appctx newWrappedServerStream")
defer span.End()
return &wrappedServerStream{ServerStream: ss, newCtx: ctx}
}

Expand Down
28 changes: 27 additions & 1 deletion internal/grpc/interceptors/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ import (
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/token"
tokenmgr "github.com/cs3org/reva/pkg/token/manager/registry"
"github.com/cs3org/reva/pkg/tracing"
"github.com/cs3org/reva/pkg/utils"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -44,6 +46,8 @@ import (
var userGroupsCache gcache.Cache
var scopeExpansionCache gcache.Cache

const tracerName = "auth"

type config struct {
// TODO(labkode): access a map is more performant as uri as fixed in length
// for SkipMethods.
Expand Down Expand Up @@ -89,6 +93,9 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI
}

interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "auth UnaryServerInterceptor")
defer span.End()

log := appctx.GetLogger(ctx)

if utils.Skip(info.FullMethod, unprotected) {
Expand All @@ -99,6 +106,7 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI
if ok {
u, err := dismantleToken(ctx, tkn, req, tokenManager, conf.GatewayAddr, true)
if err == nil {
span.SetAttributes(semconv.EnduserIDKey.String(u.Username))
ctx = ctxpkg.ContextSetUser(ctx, u)
}
}
Expand All @@ -119,6 +127,7 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI
return nil, status.Errorf(codes.PermissionDenied, "auth: core access token is invalid")
}

span.SetAttributes(semconv.EnduserIDKey.String(u.Username))
ctx = ctxpkg.ContextSetUser(ctx, u)
return handler(ctx, req)
}
Expand Down Expand Up @@ -152,6 +161,9 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe

interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "auth StreamServerInterceptor")
defer span.End()

log := appctx.GetLogger(ctx)

if utils.Skip(info.FullMethod, unprotected) {
Expand All @@ -163,6 +175,7 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe
if ok {
u, err := dismantleToken(ctx, tkn, ss, tokenManager, conf.GatewayAddr, true)
if err == nil {
span.SetAttributes(semconv.EnduserIDKey.String(u.Username))
ctx = ctxpkg.ContextSetUser(ctx, u)
ss = newWrappedServerStream(ctx, ss)
}
Expand All @@ -185,6 +198,7 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe
return status.Errorf(codes.PermissionDenied, "auth: core access token is invalid")
}

span.SetAttributes(semconv.EnduserIDKey.String(u.Username))
// store user and core access token in context.
ctx = ctxpkg.ContextSetUser(ctx, u)
wrapped := newWrappedServerStream(ctx, ss)
Expand All @@ -194,6 +208,8 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe
}

func newWrappedServerStream(ctx context.Context, ss grpc.ServerStream) *wrappedServerStream {
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "auth newWrappedServerStream")
defer span.End()
return &wrappedServerStream{ServerStream: ss, newCtx: ctx}
}

Expand All @@ -207,17 +223,22 @@ func (ss *wrappedServerStream) Context() context.Context {
}

func dismantleToken(ctx context.Context, tkn string, req interface{}, mgr token.Manager, gatewayAddr string, unprotected bool) (*userpb.User, error) {
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "auth dismantleToken")
defer span.End()

u, tokenScope, err := mgr.DismantleToken(ctx, tkn)
if err != nil {
return nil, err
}

span.SetAttributes(semconv.EnduserIDKey.String(u.Username))

if unprotected {
return u, nil
}

if sharedconf.SkipUserGroupsInToken() {
client, err := pool.GetGatewayServiceClient(pool.Endpoint(gatewayAddr))
client, err := pool.GetGatewayServiceClient(ctx, pool.Endpoint(gatewayAddr))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -245,6 +266,11 @@ func dismantleToken(ctx context.Context, tkn string, req interface{}, mgr token.
}

func getUserGroups(ctx context.Context, u *userpb.User, client gatewayv1beta1.GatewayAPIClient) ([]string, error) {
ctx, span := tracing.SpanStartFromContext(ctx, tracerName, "auth getUserGroups")
defer span.End()

span.SetAttributes(semconv.EnduserIDKey.String(u.Username))

if groupsIf, err := userGroupsCache.Get(u.Id.OpaqueId); err == nil {
log := appctx.GetLogger(ctx)
log.Info().Msgf("user groups found in cache %s", u.Id.OpaqueId)
Expand Down
Loading