diff --git a/ocis-pkg/service/grpc/client.go b/ocis-pkg/service/grpc/client.go index d3c409008..61690e166 100644 --- a/ocis-pkg/service/grpc/client.go +++ b/ocis-pkg/service/grpc/client.go @@ -48,7 +48,11 @@ func WithTLSCACert(v string) ClientOption { // WithTraceProvider allows to set the trace Provider for grpc clients func WithTraceProvider(tp trace.TracerProvider) ClientOption { return func(o *ClientOptions) { - o.tp = tp + if tp != nil { + o.tp = tp + } else { + o.tp = trace.NewNoopTracerProvider() + } } } @@ -92,8 +96,8 @@ func Configure(opts ...ClientOption) error { tlsConfig.RootCAs = certs } cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig)) - //case "off": - //default: + // case "off": + // default: } defaultClient = mgrpcc.NewClient(cOpts...) @@ -150,8 +154,8 @@ func NewClient(opts ...ClientOption) (client.Client, error) { tlsConfig.RootCAs = certs } cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig)) - //case "off": - //default: + // case "off": + // default: } return mgrpcc.NewClient(cOpts...), nil diff --git a/ocis-pkg/tracing/tracing.go b/ocis-pkg/tracing/tracing.go index 96dd798a0..b4de72947 100644 --- a/ocis-pkg/tracing/tracing.go +++ b/ocis-pkg/tracing/tracing.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + "reflect" "strings" "time" @@ -29,7 +30,13 @@ var Propagator = propagation.NewCompositeTextMapPropagator( // GetServiceTraceProvider returns a configured open-telemetry trace provider. func GetServiceTraceProvider(c ConfigConverter, serviceName string) (trace.TracerProvider, error) { - cfg := c.Convert() + var cfg Config + if c == nil || reflect.ValueOf(c).IsNil() { + cfg = Config{Enabled: false} + } else { + cfg = c.Convert() + } + if cfg.Enabled { return GetTraceProvider(cfg.Endpoint, cfg.Collector, serviceName, cfg.Type) } diff --git a/services/eventhistory/pkg/command/server.go b/services/eventhistory/pkg/command/server.go index 31209b793..655a2cd8e 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 27661368c..68c18a993 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/defaults/defaultconfig.go b/services/eventhistory/pkg/config/defaults/defaultconfig.go index 697ec9316..141817568 100644 --- a/services/eventhistory/pkg/config/defaults/defaultconfig.go +++ b/services/eventhistory/pkg/config/defaults/defaultconfig.go @@ -59,6 +59,17 @@ func EnsureDefaults(cfg *config.Config) { cfg.Log = &config.Log{} } + if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { + cfg.Tracing = &config.Tracing{ + Enabled: cfg.Commons.Tracing.Enabled, + Type: cfg.Commons.Tracing.Type, + Endpoint: cfg.Commons.Tracing.Endpoint, + Collector: cfg.Commons.Tracing.Collector, + } + } else if cfg.Tracing == nil { + cfg.Tracing = &config.Tracing{} + } + if cfg.GRPCClientTLS == nil && cfg.Commons != nil { cfg.GRPCClientTLS = structs.CopyOrZeroValue(cfg.Commons.GRPCClientTLS) } diff --git a/services/eventhistory/pkg/config/tracing.go b/services/eventhistory/pkg/config/tracing.go new file mode 100644 index 000000000..91ba04112 --- /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 20b5506b5..62de8a00f 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,14 @@ 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) { + if traceProvider != nil { + o.TraceProvider = traceProvider + } else { + o.TraceProvider = trace.NewNoopTracerProvider() + } + } +} diff --git a/services/eventhistory/pkg/server/grpc/server.go b/services/eventhistory/pkg/server/grpc/server.go index 41d9583ae..f7b346752 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")