Skip to content

Commit

Permalink
Add GetLogger method to Tracer interface (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone authored Dec 2, 2021
1 parent a707cbc commit 6c694d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
27 changes: 27 additions & 0 deletions interceptor/tracing_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/log"
"go.temporal.io/sdk/workflow"
)

Expand All @@ -41,6 +42,9 @@ const (
// Tracer is an interface for tracing implementations as used by
// NewTracingInterceptor. Most callers do not use this directly, but rather use
// the opentracing or opentelemetry packages.
//
// All implementations must embed BaseTracer to safely
// handle future changes.
type Tracer interface {
// Options returns the options for the tracer. This is only called once on
// initialization.
Expand All @@ -61,8 +65,24 @@ type Tracer interface {

// StartSpan starts and returns a span with the given options.
StartSpan(*TracerStartSpanOptions) (TracerSpan, error)

// GetLogger returns a log.Logger which may include additional fields in its
// output in order to support correlation of tracing and log data.
GetLogger(log.Logger, TracerSpanRef) log.Logger

mustEmbedBaseTracer()
}

// BaseTracer is a default implementation of Tracer meant for embedding.
type BaseTracer struct{}

func (BaseTracer) GetLogger(logger log.Logger, ref TracerSpanRef) log.Logger {
return logger
}

//lint:ignore U1000 Ignore unused method; it is only required to implement the Tracer interface but will never be called.
func (BaseTracer) mustEmbedBaseTracer() {}

// TracerOptions are options returned from Tracer.Options.
type TracerOptions struct {
// SpanContextKey provides a key to put a span on a context unrelated to how a
Expand Down Expand Up @@ -319,6 +339,13 @@ func (t *tracingWorkflowOutboundInterceptor) ExecuteLocalActivity(
return t.Next.ExecuteLocalActivity(ctx, activityType, args...)
}

func (t *tracingWorkflowOutboundInterceptor) GetLogger(ctx workflow.Context) log.Logger {
if span, _ := ctx.Value(t.root.options.SpanContextKey).(TracerSpan); span != nil {
t.root.tracer.GetLogger(t.Next.GetLogger(ctx), span)
}
return t.Next.GetLogger(ctx)
}

func (t *tracingWorkflowOutboundInterceptor) ExecuteChildWorkflow(
ctx workflow.Context,
childWorkflowType string,
Expand Down
7 changes: 5 additions & 2 deletions opentelemetry/tracing_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ type spanContextKey struct{}

const defaultHeaderKey = "_tracer-data"

type tracer struct{ options *TracerOptions }
type tracer struct {
interceptor.BaseTracer
options *TracerOptions
}

// NewTracer creates a tracer with the given options. Most callers should use
// NewTracingInterceptor instead.
Expand Down Expand Up @@ -100,7 +103,7 @@ func NewTracer(options TracerOptions) (interceptor.Tracer, error) {
return span
}
}
return &tracer{&options}, nil
return &tracer{options: &options}, nil
}

// NewTracingInterceptor creates an interceptor for setting on client options
Expand Down
7 changes: 5 additions & 2 deletions opentracing/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ type spanContextKey struct{}

const defaultHeaderKey = "_tracer-data"

type tracer struct{ options *TracerOptions }
type tracer struct {
interceptor.BaseTracer
options *TracerOptions
}

// NewTracer creates a tracer with the given options. Most callers should use
// NewInterceptor instead.
Expand All @@ -77,7 +80,7 @@ func NewTracer(options TracerOptions) (interceptor.Tracer, error) {
return t.StartSpan(operationName, opts...)
}
}
return &tracer{&options}, nil
return &tracer{options: &options}, nil
}

// NewTracingInterceptor creates an interceptor for setting on client options
Expand Down

0 comments on commit 6c694d5

Please sign in to comment.