Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Deprecate NewSpanXXX in the favor of StartSpan #708

Merged
merged 3 commits into from
Apr 18, 2018

Conversation

rakyll
Copy link
Contributor

@rakyll rakyll commented Apr 16, 2018

Required to support the Go execution tracer spans.

Fixes #694.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rakyll "timeouts" for what?

Copy link
Contributor Author

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()))
Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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:

  1. trace.NewSpan
  2. trace.WithSpan to get the context

trace/trace.go Outdated
}

var span *Span
if (opts.remoteParent != SpanContext{}) {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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)
Copy link
Member

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 {
    ...
}

Copy link
Contributor Author

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)
Copy link
Member

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?

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Member

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

Copy link
Contributor Author

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.
@rakyll
Copy link
Contributor Author

rakyll commented Apr 17, 2018

PTAL


// WithSampler makes new spans to be be created with a custom sampler.
// Otherwise, the global sampler is used.
func WithSampler(sampler Sampler) StartOption {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

@rakyll rakyll left a 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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@bogdandrutu bogdandrutu left a 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 {
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rakyll
Copy link
Contributor Author

rakyll commented Apr 18, 2018

@odeke-em @Ramonza can you take a final look for Go readability before I merge?

Copy link
Contributor

@semistrict semistrict left a 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)
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@rakyll rakyll merged commit 730e847 into census-instrumentation:master Apr 18, 2018
@rakyll rakyll deleted the startspan branch April 18, 2018 17:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants