-
Notifications
You must be signed in to change notification settings - Fork 327
Deprecate NewSpanXXX in the favor of StartSpan #708
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,8 @@ func TestBundling(t *testing.T) { | |
} | ||
trace.RegisterExporter(exporter) | ||
|
||
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) | ||
for i := 0; i < 35; i++ { | ||
_, span := trace.StartSpan(context.Background(), "span") | ||
_, span := trace.StartSpan(context.Background(), "span", trace.WithSampler(trace.AlwaysSample())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not leave it as it was before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any value why we set global settings in the tests unless we are not testing the ability to have global settings. |
||
span.End() | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,14 @@ func FromContext(ctx context.Context) *Span { | |
} | ||
|
||
// WithSpan returns a new context with the given Span attached. | ||
// | ||
// Deprecated: Use NewContext. | ||
func WithSpan(parent context.Context, s *Span) context.Context { | ||
return NewContext(parent, s) | ||
} | ||
|
||
// NewContext returns a new context with the given Span attached. | ||
func NewContext(parent context.Context, s *Span) context.Context { | ||
return context.WithValue(parent, contextKey{}, s) | ||
} | ||
|
||
|
@@ -125,32 +132,68 @@ type StartOptions struct { | |
SpanKind int | ||
} | ||
|
||
// StartOption apply changes to StartOptions. | ||
type StartOption func(*StartOptions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it does for backwards compatibility but maybe we should mark it as deprecated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should keep it as it is. It allows ochttp and ocgrpc to provide it as a configuration field. |
||
|
||
// WithSpanKind makes new spans to be created with the given kind. | ||
func WithSpanKind(spanKind int) StartOption { | ||
return func(o *StartOptions) { | ||
o.SpanKind = spanKind | ||
} | ||
} | ||
|
||
// WithSampler makes new spans to be be created with a custom sampler. | ||
// Otherwise, the global sampler is used. | ||
func WithSampler(sampler Sampler) StartOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will confuse people, WithSpan vs WithSampler (or any other With) are completely different but they look so alike. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest us to rename WithSpan to NewContext to avoid that confusion. Given it is a such a critical API, I didn't want to break it. But did it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will be such a confusion for users. In all the languages we call this WithSpan, I would rather rename these WithSampler or simply use a builder pattern for the StartOptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking more into other libraries like https://godoc.org/google.golang.org/grpc I see that with is a pattern used for optional things. So I am fine with this approach. |
||
return func(o *StartOptions) { | ||
o.Sampler = sampler | ||
} | ||
} | ||
|
||
// StartSpan starts a new child span of the current span in the context. If | ||
// there is no span in the context, creates a new trace and span. | ||
func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) { | ||
var opts StartOptions | ||
var parent SpanContext | ||
if p := FromContext(ctx); p != nil { | ||
parent = p.spanContext | ||
} | ||
for _, op := range o { | ||
op(&opts) | ||
} | ||
span := startSpanInternal(name, parent != SpanContext{}, parent, false, opts) | ||
return NewContext(ctx, span), span | ||
} | ||
|
||
// StartSpanWithRemoteParent starts a new child span of the span from the given parent. | ||
// | ||
// This is provided as a convenience for WithSpan(ctx, NewSpan(...)). Use it | ||
// if you require custom spans in addition to the default spans provided by | ||
// ocgrpc, ochttp or similar framework integration. | ||
func StartSpan(ctx context.Context, name string) (context.Context, *Span) { | ||
parentSpan, _ := ctx.Value(contextKey{}).(*Span) | ||
span := NewSpan(name, parentSpan, StartOptions{}) | ||
return WithSpan(ctx, span), span | ||
// If the incoming context contains a parent, it ignores. StartSpanWithRemoteParent is | ||
// preferred for cases where the parent is propagated via an incoming request. | ||
func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanContext, o ...StartOption) (context.Context, *Span) { | ||
var opts StartOptions | ||
for _, op := range o { | ||
op(&opts) | ||
} | ||
span := startSpanInternal(name, parent != SpanContext{}, parent, true, opts) | ||
return NewContext(ctx, span), span | ||
} | ||
|
||
// NewSpan returns a new span. | ||
// | ||
// If parent is not nil, created span will be a child of the parent. | ||
// | ||
// Deprecated: Use StartSpan. | ||
func NewSpan(name string, parent *Span, o StartOptions) *Span { | ||
hasParent := false | ||
var parentSpanContext SpanContext | ||
if parent != nil { | ||
hasParent = true | ||
parentSpanContext = parent.SpanContext() | ||
} | ||
return startSpanInternal(name, hasParent, parentSpanContext, false, o) | ||
return startSpanInternal(name, parent != nil, parentSpanContext, false, o) | ||
} | ||
|
||
// NewSpanWithRemoteParent returns a new span with the given parent SpanContext. | ||
// | ||
// Deprecated: Use StartSpanWithRemoteParent. | ||
func NewSpanWithRemoteParent(name string, parent SpanContext, o StartOptions) *Span { | ||
return startSpanInternal(name, true, parent, true, o) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rakyll "timeouts" for what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a carry over from the old code. TODOs should have an owner. @Ramonza, please always add an owner for TODOS.