Skip to content

Commit

Permalink
Micro: Add TraceProvider injector. (#4040)
Browse files Browse the repository at this point in the history
* Micro: Add TraceProvider injector.

This allows to inject a TraceProvider into the service constructor,
instead of letting the service constructor create one.

* Add changelog.
  • Loading branch information
ainmosni authored Jul 6, 2023
1 parent 47ff327 commit 838071e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/external-traceprovider-micro.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions pkg/micro/ocdav/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -54,6 +55,8 @@ type Options struct {
TracingCollector string
TracingEndpoint string

TraceProvider trace.TracerProvider

MetricsEnabled bool
MetricsNamespace string
MetricsSubsystem string
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 16 additions & 14 deletions pkg/micro/ocdav/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -132,7 +135,6 @@ func Service(opts ...Option) (micro.Service, error) {
}

func setDefaults(sopts *Options) error {

// set defaults
if sopts.Name == "" {
sopts.Name = ServerName
Expand Down

0 comments on commit 838071e

Please sign in to comment.