From d00f5a9da52c1ab256cb1b02e84d12c0311b3e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Franke?= Date: Fri, 7 Jul 2023 12:46:59 +0200 Subject: [PATCH] Add tracing to eventhistory service. This adds tracing to the event history service. --- services/eventhistory/pkg/command/server.go | 11 +++++-- services/eventhistory/pkg/config/config.go | 8 ----- services/eventhistory/pkg/config/tracing.go | 21 ++++++++++++++ .../eventhistory/pkg/server/grpc/option.go | 29 ++++++++++++------- .../eventhistory/pkg/server/grpc/server.go | 1 + 5 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 services/eventhistory/pkg/config/tracing.go diff --git a/services/eventhistory/pkg/command/server.go b/services/eventhistory/pkg/command/server.go index 31209b793d..655a2cd8e8 100644 --- a/services/eventhistory/pkg/command/server.go +++ b/services/eventhistory/pkg/command/server.go @@ -11,6 +11,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/handlers" "github.com/owncloud/ocis/v2/ocis-pkg/service/debug" 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/eventhistory/pkg/config" "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config/parser" @@ -32,7 +33,13 @@ func Server(cfg *config.Config) *cli.Command { }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) - err := ogrpc.Configure(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...) + traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name) + if err != nil { + return err + } + err = ogrpc.Configure( + append(ogrpc.GetClientOptions(cfg.GRPCClientTLS), ogrpc.WithTraceProvider(traceProvider))..., + ) if err != nil { return err } @@ -76,6 +83,7 @@ func Server(cfg *config.Config) *cli.Command { grpc.Metrics(metrics), grpc.Consumer(consumer), grpc.Persistence(st), + grpc.TraceProvider(traceProvider), ) gr.Add(service.Run, func(err error) { @@ -107,7 +115,6 @@ func Server(cfg *config.Config) *cli.Command { } return gr.Run() - }, } } diff --git a/services/eventhistory/pkg/config/config.go b/services/eventhistory/pkg/config/config.go index 27661368c8..68c18a9937 100644 --- a/services/eventhistory/pkg/config/config.go +++ b/services/eventhistory/pkg/config/config.go @@ -51,11 +51,3 @@ type Events struct { TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"OCIS_EVENTS_TLS_ROOT_CA_CERTIFICATE;EVENTHISTORY_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided NOTIFICATIONS_EVENTS_TLS_INSECURE will be seen as false."` EnableTLS bool `yaml:"enable_tls" env:"OCIS_EVENTS_ENABLE_TLS;EVENTHISTORY_EVENTS_ENABLE_TLS" desc:"Enable TLS for the connection to the events broker. The events broker is the ocis service which receives and delivers events between the services.."` } - -// Tracing defines the available tracing configuration. -type Tracing struct { - Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;EVENTHISTORY_TRACING_ENABLED" desc:"Activates tracing."` - Type string `yaml:"type" env:"OCIS_TRACING_TYPE;EVENTHISTORY_TRACING_TYPE" desc:"The type of tracing. Defaults to '', which is the same as 'jaeger'. Allowed tracing types are 'jaeger' and '' as of now."` - Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;EVENTHISTORY_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` - Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;EVENTHISTORY_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."` -} diff --git a/services/eventhistory/pkg/config/tracing.go b/services/eventhistory/pkg/config/tracing.go new file mode 100644 index 0000000000..91ba041120 --- /dev/null +++ b/services/eventhistory/pkg/config/tracing.go @@ -0,0 +1,21 @@ +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;EVENTHISTORY_TRACING_ENABLED" desc:"Activates tracing."` + Type string `yaml:"type" env:"OCIS_TRACING_TYPE;EVENTHISTORY_TRACING_TYPE" desc:"The type of tracing. Defaults to '', which is the same as 'jaeger'. Allowed tracing types are 'jaeger' and '' as of now."` + Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;EVENTHISTORY_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` + Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;EVENTHISTORY_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/eventhistory/pkg/server/grpc/option.go b/services/eventhistory/pkg/server/grpc/option.go index 20b5506b5b..cfe1f09175 100644 --- a/services/eventhistory/pkg/server/grpc/option.go +++ b/services/eventhistory/pkg/server/grpc/option.go @@ -9,6 +9,7 @@ import ( "github.com/owncloud/ocis/v2/services/eventhistory/pkg/metrics" "github.com/urfave/cli/v2" "go-micro.dev/v4/store" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -16,16 +17,17 @@ 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 - Persistence store.Store - Consumer events.Consumer + Name string + Address string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + Namespace string + Flags []cli.Flag + Persistence store.Store + Consumer events.Consumer + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -108,3 +110,10 @@ func Consumer(consumer events.Consumer) Option { o.Consumer = consumer } } + +// TraceProvider provides a function to configure the trace provider +func TraceProvider(traceProvider trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = traceProvider + } +} diff --git a/services/eventhistory/pkg/server/grpc/server.go b/services/eventhistory/pkg/server/grpc/server.go index 41d9583ae2..f7b3467520 100644 --- a/services/eventhistory/pkg/server/grpc/server.go +++ b/services/eventhistory/pkg/server/grpc/server.go @@ -25,6 +25,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 event history service")