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