diff --git a/services/thumbnails/pkg/command/server.go b/services/thumbnails/pkg/command/server.go index d0ec65fd7..6e32f7fbe 100644 --- a/services/thumbnails/pkg/command/server.go +++ b/services/thumbnails/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/thumbnails/pkg/config" "github.com/owncloud/ocis/v2/services/thumbnails/pkg/config/parser" @@ -15,7 +16,6 @@ import ( "github.com/owncloud/ocis/v2/services/thumbnails/pkg/server/debug" "github.com/owncloud/ocis/v2/services/thumbnails/pkg/server/grpc" "github.com/owncloud/ocis/v2/services/thumbnails/pkg/server/http" - "github.com/owncloud/ocis/v2/services/thumbnails/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -30,7 +30,8 @@ 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 } @@ -62,6 +63,7 @@ func Server(cfg *config.Config) *cli.Command { grpc.Namespace(cfg.GRPC.Namespace), grpc.Address(cfg.GRPC.Addr), grpc.Metrics(metrics), + grpc.TraceProvider(traceProvider), ) gr.Add(service.Run, func(_ error) { @@ -77,7 +79,6 @@ func Server(cfg *config.Config) *cli.Command { debug.Logger(logger), debug.Config(cfg), ) - if err != nil { logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server") return err @@ -95,7 +96,6 @@ func Server(cfg *config.Config) *cli.Command { http.Metrics(metrics), http.Namespace(cfg.HTTP.Namespace), ) - if err != nil { logger.Info(). Err(err). diff --git a/services/thumbnails/pkg/config/tracing.go b/services/thumbnails/pkg/config/tracing.go index 82ca1c6dc..304ab143e 100644 --- a/services/thumbnails/pkg/config/tracing.go +++ b/services/thumbnails/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;THUMBNAILS_TRACING_ENABLED" desc:"Activates tracing."` @@ -7,3 +9,13 @@ type Tracing struct { Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;THUMBNAILS_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;THUMBNAILS_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/thumbnails/pkg/server/grpc/option.go b/services/thumbnails/pkg/server/grpc/option.go index ede5c1207..1b858c835 100644 --- a/services/thumbnails/pkg/server/grpc/option.go +++ b/services/thumbnails/pkg/server/grpc/option.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/v2/services/thumbnails/pkg/config" "github.com/owncloud/ocis/v2/services/thumbnails/pkg/metrics" "github.com/urfave/cli/v2" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -14,14 +15,15 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Name string - Address string - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Namespace string - Flags []cli.Flag + Name string + Address string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + Namespace string + Flags []cli.Flag + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -90,3 +92,10 @@ func Flags(flags []cli.Flag) Option { o.Flags = append(o.Flags, flags...) } } + +// 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/thumbnails/pkg/server/grpc/server.go b/services/thumbnails/pkg/server/grpc/server.go index 9201aace5..270cd994c 100644 --- a/services/thumbnails/pkg/server/grpc/server.go +++ b/services/thumbnails/pkg/server/grpc/server.go @@ -31,6 +31,7 @@ func NewService(opts ...Option) grpc.Service { grpc.Context(options.Context), grpc.Flags(options.Flags...), grpc.Version(version.GetString()), + grpc.TraceProvider(options.TraceProvider), ) if err != nil { options.Logger.Fatal().Err(err).Msg("Error creating thumbnail service") @@ -70,7 +71,7 @@ func NewService(opts ...Option) grpc.Service { ) thumbnail = decorators.NewInstrument(thumbnail, options.Metrics) thumbnail = decorators.NewLogging(thumbnail, options.Logger) - thumbnail = decorators.NewTracing(thumbnail) + thumbnail = decorators.NewTracing(thumbnail, options.TraceProvider) } _ = thumbnailssvc.RegisterThumbnailServiceHandler( diff --git a/services/thumbnails/pkg/service/grpc/v0/decorators/tracing.go b/services/thumbnails/pkg/service/grpc/v0/decorators/tracing.go index 636143d96..f34166e3e 100644 --- a/services/thumbnails/pkg/service/grpc/v0/decorators/tracing.go +++ b/services/thumbnails/pkg/service/grpc/v0/decorators/tracing.go @@ -6,27 +6,28 @@ import ( "go.opentelemetry.io/otel/trace" thumbnailssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/thumbnails/v0" - thumbnailsTracing "github.com/owncloud/ocis/v2/services/thumbnails/pkg/tracing" "go.opentelemetry.io/otel/attribute" ) // NewTracing returns a service that instruments traces. -func NewTracing(next DecoratedService) DecoratedService { +func NewTracing(next DecoratedService, tp trace.TracerProvider) DecoratedService { return tracing{ Decorator: Decorator{next: next}, + tp: tp, } } type tracing struct { Decorator + tp trace.TracerProvider } // GetThumbnail implements the ThumbnailServiceHandler interface. func (t tracing) GetThumbnail(ctx context.Context, req *thumbnailssvc.GetThumbnailRequest, rsp *thumbnailssvc.GetThumbnailResponse) error { var span trace.Span - if thumbnailsTracing.TraceProvider != nil { - tracer := thumbnailsTracing.TraceProvider.Tracer("thumbnails") + if t.tp != nil { + tracer := t.tp.Tracer("thumbnails") spanOpts := []trace.SpanStartOption{ trace.WithSpanKind(trace.SpanKindServer), } diff --git a/services/thumbnails/pkg/tracing/tracing.go b/services/thumbnails/pkg/tracing/tracing.go deleted file mode 100644 index 984cdc95b..000000000 --- a/services/thumbnails/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/thumbnails/pkg/config" - "go.opentelemetry.io/otel/trace" -) - -var ( - // TraceProvider is the global trace provider for the thumbnails 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 -}