Merge pull request #6920 from owncloud/ainmosni/tracing/thumbnails

Convert thumbnails to service trace provider
This commit is contained in:
Daniel Swärd
2023-08-07 09:47:23 +02:00
committed by GitHub
6 changed files with 40 additions and 40 deletions

View File

@@ -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).

View File

@@ -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,
}
}

View File

@@ -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
}
}

View File

@@ -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(

View File

@@ -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),
}

View File

@@ -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
}