Skip to content

Commit

Permalink
cleanup tracing and deprecate Collector and Exporter
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Feb 8, 2024
1 parent 47fc3f4 commit 5e714c3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
34 changes: 27 additions & 7 deletions pkg/micro/ocdav/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/rs/zerolog"
"go-micro.dev/v4/broker"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/credentials"
)

// Option defines a single option function.
Expand All @@ -50,11 +51,10 @@ type Options struct {
FavoriteManager favorite.Manager
GatewaySelector pool.Selectable[gateway.GatewayAPIClient]

TracingEnabled bool
TracingInsecure bool
TracingExporter string
TracingCollector string
TracingEndpoint string
TracingEnabled bool
TracingInsecure bool
TracingEndpoint string
TracingTransportCredentials credentials.TransportCredentials

TraceProvider trace.TracerProvider

Expand Down Expand Up @@ -230,11 +230,25 @@ func LockSystem(val ocdav.LockSystem) Option {
}

// Tracing enables tracing
// Deprecated: use WithTracingEndpoint and WithTracingEnabled, Collector is unused
func Tracing(endpoint, collector string) Option {
return func(o *Options) {
o.TracingEnabled = true
o.TracingEndpoint = endpoint
o.TracingCollector = collector
}
}

// WithTracingEnabled option
func WithTracingEnabled(enabled bool) Option {
return func(o *Options) {
o.TracingEnabled = enabled
}
}

// WithTracingEndpoint option
func WithTracingEndpoint(endpoint string) Option {
return func(o *Options) {
o.TracingEndpoint = endpoint
}
}

Expand All @@ -246,9 +260,15 @@ func WithTracingInsecure() Option {
}

// WithTracingExporter option
// Deprecated: unused
func WithTracingExporter(exporter string) Option {
return func(o *Options) {}
}

// WithTracingTransportCredentials option
func WithTracingTransportCredentials(v credentials.TransportCredentials) Option {
return func(o *Options) {
o.TracingExporter = exporter
o.TracingTransportCredentials = v
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/micro/ocdav/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ func Service(opts ...Option) (micro.Service, error) {

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 {
Expand All @@ -101,6 +99,9 @@ func Service(opts ...Option) (micro.Service, error) {
if sopts.TracingInsecure {
topts = append(topts, rtrace.WithInsecure())
}
if sopts.TracingTransportCredentials != nil {
topts = append(topts, rtrace.WithTransportCredentials(sopts.TracingTransportCredentials))
}
tp = rtrace.NewTracerProvider(topts...)
}
if err := useMiddlewares(r, &sopts, revaService, tp); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/trace/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func WithEnabled() Option {
}

// WithExporter option
// Deprecated: unused
func WithExporter(v string) Option {
return func(o *Options) {
o.Exporter = v
Expand All @@ -38,6 +39,7 @@ func WithInsecure() Option {
}

// WithCollector option
// Deprecated: unused
func WithCollector(v string) Option {
return func(o *Options) {
o.Collector = v
Expand All @@ -57,3 +59,10 @@ func WithServiceName(v string) Option {
o.ServiceName = v
}
}

// WithTransportCredentials option
func WithTransportCredentials(v credentials.TransportCredentials) Option {
return func(o *Options) {
o.TransportCredentials = v
}
}
14 changes: 7 additions & 7 deletions pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ func InitDefaultTracerProvider(collector, endpoint string) {
defer defaultProvider.mutex.Unlock()
if !defaultProvider.initialized {
SetDefaultTracerProvider(getOtlpTracerProvider(Options{
Enabled: true,
Collector: collector,
Endpoint: endpoint,
ServiceName: "reva default otlp provider",
Insecure: true,
}))
}
}
Expand All @@ -107,16 +106,17 @@ func DefaultProvider() trace.TracerProvider {

// getOtelTracerProvider returns a new TracerProvider, configure for the specified service
func getOtlpTracerProvider(options Options) trace.TracerProvider {

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
transportCredentials := options.TransportCredentials
if options.Insecure {
transportCredentials = insecure.NewCredentials()
}
conn, err := grpc.DialContext(ctx, options.Endpoint,
// Note the use of insecure transport here. TLS is recommended in production.
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithTransportCredentials(transportCredentials),
)
if err != nil {
panic(fmt.Errorf("failed to create gRPC connection to collector: %w", err))
panic(fmt.Errorf("failed to create gRPC connection to endpoint: %w", err))
}
exporter, err := otlptracegrpc.New(
context.Background(),
Expand Down

0 comments on commit 5e714c3

Please sign in to comment.