From 838071e188bb07be6090d553a82c0c81f801f3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Franke?= Date: Thu, 6 Jul 2023 08:24:22 +0200 Subject: [PATCH] Micro: Add TraceProvider injector. (#4040) * Micro: Add TraceProvider injector. This allows to inject a TraceProvider into the service constructor, instead of letting the service constructor create one. * Add changelog. --- .../external-traceprovider-micro.md | 5 ++++ pkg/micro/ocdav/option.go | 10 +++++++ pkg/micro/ocdav/service.go | 30 ++++++++++--------- 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 changelog/unreleased/external-traceprovider-micro.md diff --git a/changelog/unreleased/external-traceprovider-micro.md b/changelog/unreleased/external-traceprovider-micro.md new file mode 100644 index 0000000000..51c172e134 --- /dev/null +++ b/changelog/unreleased/external-traceprovider-micro.md @@ -0,0 +1,5 @@ +Enhancement: Allow to use external trace provider in micro service + +Allow injecting of external trace provider in the micro service instead of forcing the initialisation of an internal one. + +https://github.com/cs3org/reva/pull/4040 diff --git a/pkg/micro/ocdav/option.go b/pkg/micro/ocdav/option.go index 5ff8644ac9..a6ebba9429 100644 --- a/pkg/micro/ocdav/option.go +++ b/pkg/micro/ocdav/option.go @@ -28,6 +28,7 @@ import ( "github.com/cs3org/reva/v2/pkg/storage/favorite" "github.com/rs/zerolog" "go-micro.dev/v4/broker" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -54,6 +55,8 @@ type Options struct { TracingCollector string TracingEndpoint string + TraceProvider trace.TracerProvider + MetricsEnabled bool MetricsNamespace string MetricsSubsystem string @@ -234,6 +237,13 @@ func WithTracingExporter(exporter string) Option { } } +// WithTraceProvider option +func WithTraceProvider(provider trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = provider + } +} + // Version provides a function to set the Version config option. func Version(val string) Option { return func(o *Options) { diff --git a/pkg/micro/ocdav/service.go b/pkg/micro/ocdav/service.go index a54f5280a6..3173e0c90b 100644 --- a/pkg/micro/ocdav/service.go +++ b/pkg/micro/ocdav/service.go @@ -51,7 +51,6 @@ const ( // Service initializes the ocdav service and underlying http server. func Service(opts ...Option) (micro.Service, error) { - sopts := newOptions(opts...) // set defaults @@ -86,19 +85,23 @@ func Service(opts ...Option) (micro.Service, error) { // chi.RegisterMethod(ocdav.MethodMkcol) // chi.RegisterMethod(ocdav.MethodReport) r := chi.NewRouter() - topts := []rtrace.Option{ - rtrace.WithExporter(sopts.TracingExporter), - rtrace.WithEndpoint(sopts.TracingEndpoint), - rtrace.WithCollector(sopts.TracingCollector), - rtrace.WithServiceName(sopts.Name), - } - if sopts.TracingEnabled { - topts = append(topts, rtrace.WithEnabled()) - } - if sopts.TracingInsecure { - topts = append(topts, rtrace.WithInsecure()) + tp := sopts.TraceProvider + + if tp == nil { + topts := []rtrace.Option{ + rtrace.WithExporter(sopts.TracingExporter), + rtrace.WithEndpoint(sopts.TracingEndpoint), + rtrace.WithCollector(sopts.TracingCollector), + rtrace.WithServiceName(sopts.Name), + } + if sopts.TracingEnabled { + topts = append(topts, rtrace.WithEnabled()) + } + if sopts.TracingInsecure { + topts = append(topts, rtrace.WithInsecure()) + } + tp = rtrace.NewTracerProvider(topts...) } - tp := rtrace.NewTracerProvider(topts...) if err := useMiddlewares(r, &sopts, revaService, tp); err != nil { return nil, err } @@ -132,7 +135,6 @@ func Service(opts ...Option) (micro.Service, error) { } func setDefaults(sopts *Options) error { - // set defaults if sopts.Name == "" { sopts.Name = ServerName