From 42c02a73c37b2a87f1e571466b73e7ca3181012c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Franke?= Date: Wed, 5 Jul 2023 13:11:40 +0200 Subject: [PATCH 1/2] Micro: Add TraceProvider injector. This allows to inject a TraceProvider into the service constructor, instead of letting the service constructor create one. --- pkg/micro/ocdav/option.go | 10 ++++++++++ pkg/micro/ocdav/service.go | 30 ++++++++++++++++-------------- 2 files changed, 26 insertions(+), 14 deletions(-) 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 From 01ede829c0fbaa206a0097e5863bb970176fd9a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Franke?= Date: Wed, 5 Jul 2023 13:14:55 +0200 Subject: [PATCH 2/2] Add changelog. --- changelog/unreleased/external-traceprovider-micro.md | 5 +++++ 1 file changed, 5 insertions(+) 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