-
Notifications
You must be signed in to change notification settings - Fork 327
Deprecate NewSpanXXX in the favor of StartSpan #708
Conversation
@@ -117,11 +117,14 @@ func (e *traceExporter) uploadSpans(spans []*trace.SpanData) { | |||
req.Spans = append(req.Spans, protoFromSpanData(span, e.projectID)) | |||
} | |||
// Create a never-sampled span to prevent traces associated with exporter. | |||
span := trace.NewSpan("go.opencensus.io/exporter/stackdriver.uploadSpans", nil, trace.StartOptions{Sampler: trace.NeverSample()}) | |||
ctx, span := trace.StartSpan( // TODO: add timeouts |
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.
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 comment
The 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 comment
The 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.
trace/trace.go
Outdated
span := NewSpan(name, parentSpan, StartOptions{}) | ||
// If WithRemoteParent option is provided, started span will be a child | ||
// of the given remote parent. | ||
func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Context, *Span) { |
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.
Hand clap, this is a great change! It takes away the previous non-ideal usage of:
- trace.NewSpan
- trace.WithSpan to get the context
trace/trace.go
Outdated
} | ||
|
||
var span *Span | ||
if (opts.remoteParent != SpanContext{}) { |
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.
Umm, please remove the brackets.
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.
Doesn't compile without the brackets.
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.
I guess I shouldn't spend too much time on this but if you'd like
if opts.remoteParent != (SpanContext{}) {
is more conformant to the general style in this repo
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.
Done.
trace/trace.go
Outdated
|
||
var span *Span | ||
if (opts.remoteParent != SpanContext{}) { | ||
span = startSpanInternal(name, opts.remoteParent != SpanContext{}, opts.remoteParent, true, opts) |
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.
We've already evaluated this condition opts.remoteParent != SpanContext{}
so perhaps let's improve it with
if hasRemoteParent := opts.remoteParent != SpanContext{}; hasRemoteParent {
span = startSpanInternal(name, hasRemoteParent, opts.remoteParent, true, opts)
} else {
...
}
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.
hasRemoteParent would always be true. Fixed this.
trace/trace.go
Outdated
parentSpanContext = parent.SpanContext() | ||
} | ||
return startSpanInternal(name, hasParent, parentSpanContext, false, o) | ||
return startSpanInternal(name, true, parentSpanContext, false, 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.
Wait, why has this changed? If parent is nil, why should hasParent
or remoteParent
be true
?
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.
Oops, fixed.
trace/trace.go
Outdated
|
||
var span *Span | ||
if opts.remoteParent != (SpanContext{}) { | ||
span = startSpanInternal(name, true, opts.remoteParent, true, opts) |
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.
Am not sure if this is the right change, not every span has a remote parent.
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.
Rather, not every span has a parent
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.
If opts.remoteParent is non-zero, the span has a parent.
Required to support the Go execution tracer spans. Fixes census-instrumentation#694.
PTAL |
|
||
// 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 comment
The 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 comment
The 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 comment
The 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 comment
The 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.
trace/trace.go
Outdated
} | ||
|
||
// WithRemoteParent makes new spans to be the child of the given part. | ||
func WithRemoteParent(parent SpanContext) StartOption { |
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.
Need explanation of when to use it, what is the difference between this and getting parent from the ctxt vs this (when does it happen to get parent from the ctxt), etc.
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.
Done.
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.
@bogdandrutu PTAL.
|
||
// 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 comment
The 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.
trace/trace.go
Outdated
} | ||
|
||
// WithRemoteParent makes new spans to be the child of the given part. | ||
func WithRemoteParent(parent SpanContext) StartOption { |
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.
Done.
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.
LGTM in general
|
||
// 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 comment
The 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.
trace/trace.go
Outdated
// | ||
// If there is already a parent in the incoming context.Context, | ||
// this option replaces it with the given parent. | ||
func WithRemoteParent(parent SpanContext) StartOption { |
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.
I think this is not actually an "optional" thing, in case of a grpc/http server plugins this is required to be set and in all the other cases this is required to not be set. I will have 2 apis StartSpan and StartSpanWithRemoteParent and make this a required field in the second API.
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.
I see your point. I added StartSpanWithRemoteParent and removed the WithRemoteParent.
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.
LGTM
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.
two minor comments, otherwise LGTM
@@ -125,32 +132,69 @@ 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 comment
The reason will be displayed to describe this comment to others. Learn more.
does StartOptions
need to be public?
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.
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 comment
The 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.
trace/trace.go
Outdated
// 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 | ||
|
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.
line?
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.
Done.
Required to support the Go execution tracer spans.
Fixes #694.