From fc3dce05f429d7b7b675a5689026d8a40e8cefb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Tue, 8 Aug 2023 13:14:45 +0200 Subject: [PATCH] Add missing tracing initialization for graph service (#6988) * Convert webdav to service trace provider * graph: Add missing tracing initialization to graph service --- services/graph/pkg/command/server.go | 6 ++++++ services/graph/pkg/server/http/server.go | 1 + services/webdav/pkg/command/server.go | 10 ++++++--- services/webdav/pkg/config/tracing.go | 12 +++++++++++ services/webdav/pkg/server/http/option.go | 21 ++++++++++++------ services/webdav/pkg/server/http/server.go | 2 +- services/webdav/pkg/service/v0/option.go | 15 ++++++++++--- services/webdav/pkg/service/v0/service.go | 11 +++++++++- services/webdav/pkg/service/v0/tracing.go | 26 ----------------------- services/webdav/pkg/tracing/tracing.go | 23 -------------------- 10 files changed, 64 insertions(+), 63 deletions(-) delete mode 100644 services/webdav/pkg/service/v0/tracing.go delete mode 100644 services/webdav/pkg/tracing/tracing.go diff --git a/services/graph/pkg/command/server.go b/services/graph/pkg/command/server.go index 8fa8899935..386916072a 100644 --- a/services/graph/pkg/command/server.go +++ b/services/graph/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" "github.com/owncloud/ocis/v2/services/graph/pkg/config" "github.com/owncloud/ocis/v2/services/graph/pkg/config/parser" @@ -27,6 +28,10 @@ func Server(cfg *config.Config) *cli.Command { }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) + traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name) + if err != nil { + return err + } gr := run.Group{} ctx, cancel := func() (context.Context, context.CancelFunc) { @@ -47,6 +52,7 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(mtrcs), + http.TraceProvider(traceProvider), ) if err != nil { logger.Error().Err(err).Str("transport", "http").Msg("Failed to initialize server") diff --git a/services/graph/pkg/server/http/server.go b/services/graph/pkg/server/http/server.go index 76bd11966c..00e29f091f 100644 --- a/services/graph/pkg/server/http/server.go +++ b/services/graph/pkg/server/http/server.go @@ -44,6 +44,7 @@ func Server(opts ...Option) (http.Service, error) { http.Address(options.Config.HTTP.Addr), http.Context(options.Context), http.Flags(options.Flags...), + http.TraceProvider(options.TraceProvider), ) if err != nil { options.Logger.Error(). diff --git a/services/webdav/pkg/command/server.go b/services/webdav/pkg/command/server.go index 9a2dcdb78b..f065fd515a 100644 --- a/services/webdav/pkg/command/server.go +++ b/services/webdav/pkg/command/server.go @@ -7,6 +7,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" "github.com/owncloud/ocis/v2/services/webdav/pkg/config" "github.com/owncloud/ocis/v2/services/webdav/pkg/config/parser" @@ -14,7 +15,6 @@ import ( "github.com/owncloud/ocis/v2/services/webdav/pkg/metrics" "github.com/owncloud/ocis/v2/services/webdav/pkg/server/debug" "github.com/owncloud/ocis/v2/services/webdav/pkg/server/http" - "github.com/owncloud/ocis/v2/services/webdav/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -29,11 +29,14 @@ func Server(cfg *config.Config) *cli.Command { }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) - err := tracing.Configure(cfg) + traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name) if err != nil { return err } - cfg.GrpcClient, err = ogrpc.NewClient(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...) + cfg.GrpcClient, err = ogrpc.NewClient( + append(ogrpc.GetClientOptions(cfg.GRPCClientTLS), + ogrpc.WithTraceProvider(traceProvider), + )...) if err != nil { return err } @@ -59,6 +62,7 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(metrics), + http.TraceProvider(traceProvider), ) if err != nil { diff --git a/services/webdav/pkg/config/tracing.go b/services/webdav/pkg/config/tracing.go index 6b3212f5ff..948c22a93e 100644 --- a/services/webdav/pkg/config/tracing.go +++ b/services/webdav/pkg/config/tracing.go @@ -1,5 +1,7 @@ package config +import "github.com/owncloud/ocis/v2/ocis-pkg/tracing" + // Tracing defines the available tracing configuration. type Tracing struct { Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;WEBDAV_TRACING_ENABLED" desc:"Activates tracing."` @@ -7,3 +9,13 @@ type Tracing struct { Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;WEBDAV_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;WEBDAV_TRACING_COLLECTOR" desc:"The HTTP endpoint for sending spans directly to a collector, i.e. http://jaeger-collector:14268/api/traces. Only used if the tracing endpoint is unset."` } + +// Convert Tracing to the tracing package's Config struct. +func (t Tracing) Convert() tracing.Config { + return tracing.Config{ + Enabled: t.Enabled, + Type: t.Type, + Endpoint: t.Endpoint, + Collector: t.Collector, + } +} diff --git a/services/webdav/pkg/server/http/option.go b/services/webdav/pkg/server/http/option.go index e05f6aaf29..7e5d0fdc0f 100644 --- a/services/webdav/pkg/server/http/option.go +++ b/services/webdav/pkg/server/http/option.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/v2/services/webdav/pkg/config" "github.com/owncloud/ocis/v2/services/webdav/pkg/metrics" "github.com/urfave/cli/v2" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Namespace string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag + Namespace string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + Flags []cli.Flag + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Namespace(val string) Option { o.Namespace = val } } + +// TraceProvider provides a function to set the TraceProvider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/webdav/pkg/server/http/server.go b/services/webdav/pkg/server/http/server.go index 02e8748667..77c2743531 100644 --- a/services/webdav/pkg/server/http/server.go +++ b/services/webdav/pkg/server/http/server.go @@ -25,6 +25,7 @@ func Server(opts ...Option) (http.Service, error) { http.Address(options.Config.HTTP.Addr), http.Context(options.Context), http.Flags(options.Flags...), + http.TraceProvider(options.TraceProvider), ) if err != nil { options.Logger.Error(). @@ -64,7 +65,6 @@ func Server(opts ...Option) (http.Service, error) { { handle = svc.NewInstrument(handle, options.Metrics) handle = svc.NewLogging(handle, options.Logger) - handle = svc.NewTracing(handle) } if err := micro.RegisterHandler(service.Server(), handle); err != nil { diff --git a/services/webdav/pkg/service/v0/option.go b/services/webdav/pkg/service/v0/option.go index a2c5dc641d..7a053abbbc 100644 --- a/services/webdav/pkg/service/v0/option.go +++ b/services/webdav/pkg/service/v0/option.go @@ -5,6 +5,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/webdav/pkg/config" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -12,9 +13,10 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Logger log.Logger - Config *config.Config - Middleware []func(http.Handler) http.Handler + Logger log.Logger + Config *config.Config + Middleware []func(http.Handler) http.Handler + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -48,3 +50,10 @@ func Middleware(val ...func(http.Handler) http.Handler) Option { o.Middleware = val } } + +// TraceProvider provides a function to set the traceProvider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/webdav/pkg/service/v0/service.go b/services/webdav/pkg/service/v0/service.go index e7b0716295..73e91dee38 100644 --- a/services/webdav/pkg/service/v0/service.go +++ b/services/webdav/pkg/service/v0/service.go @@ -19,12 +19,14 @@ import ( "github.com/go-chi/render" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/registry" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" thumbnailsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/thumbnails/v0" searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" thumbnailssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/thumbnails/v0" "github.com/owncloud/ocis/v2/services/webdav/pkg/config" "github.com/owncloud/ocis/v2/services/webdav/pkg/constants" "github.com/owncloud/ocis/v2/services/webdav/pkg/dav/requests" + "github.com/riandyrn/otelchi" merrors "go-micro.dev/v4/errors" "google.golang.org/grpc/metadata" ) @@ -59,7 +61,14 @@ func NewService(opts ...Option) (Service, error) { conf := options.Config m := chi.NewMux() - m.Use(options.Middleware...) + m.Use( + otelchi.Middleware( + conf.Service.Name, + otelchi.WithChiRoutes(m), + otelchi.WithTracerProvider(options.TraceProvider), + otelchi.WithPropagators(tracing.GetPropagator()), + ), + ) tm, err := pool.StringToTLSMode(conf.GRPCClientTLS.Mode) if err != nil { diff --git a/services/webdav/pkg/service/v0/tracing.go b/services/webdav/pkg/service/v0/tracing.go deleted file mode 100644 index 6deb60a37d..0000000000 --- a/services/webdav/pkg/service/v0/tracing.go +++ /dev/null @@ -1,26 +0,0 @@ -package svc - -import ( - "net/http" -) - -// NewTracing returns a service that instruments traces. -func NewTracing(next Service) Service { - return tracing{ - next: next, - } -} - -type tracing struct { - next Service -} - -// ServeHTTP implements the Service interface. -func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) { - t.next.ServeHTTP(w, r) -} - -// Thumbnail implements the Service interface. -func (t tracing) Thumbnail(w http.ResponseWriter, r *http.Request) { - t.next.Thumbnail(w, r) -} diff --git a/services/webdav/pkg/tracing/tracing.go b/services/webdav/pkg/tracing/tracing.go deleted file mode 100644 index cbcadf44ed..0000000000 --- a/services/webdav/pkg/tracing/tracing.go +++ /dev/null @@ -1,23 +0,0 @@ -package tracing - -import ( - pkgtrace "github.com/owncloud/ocis/v2/ocis-pkg/tracing" - "github.com/owncloud/ocis/v2/services/webdav/pkg/config" - "go.opentelemetry.io/otel/trace" -) - -var ( - // TraceProvider is the global trace provider for the proxy service. - TraceProvider = trace.NewNoopTracerProvider() -) - -func Configure(cfg *config.Config) error { - var err error - if cfg.Tracing.Enabled { - if TraceProvider, err = pkgtrace.GetTraceProvider(cfg.Tracing.Endpoint, cfg.Tracing.Collector, cfg.Service.Name, cfg.Tracing.Type); err != nil { - return err - } - } - - return nil -}