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

Export StartSpanFromContextWithTracer #152

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions gocontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ func SpanFromContext(ctx context.Context) Span {
// ...
// }
func StartSpanFromContext(ctx context.Context, operationName string, opts ...StartSpanOption) (Span, context.Context) {
return startSpanFromContextWithTracer(ctx, GlobalTracer(), operationName, opts...)
return StartSpanFromContextWithTracer(ctx, GlobalTracer(), operationName, opts...)
}

// startSpanFromContextWithTracer is factored out for testing purposes.
func startSpanFromContextWithTracer(ctx context.Context, tracer Tracer, operationName string, opts ...StartSpanOption) (Span, context.Context) {
// StartSpanFromContextWithTracer starts and returns a span with `operationName`
// using a span found within the context as a ChildOfRef. If that doesn't exist
// it creates a root span. It also returns a context.Context object built
// around the returned span.
//
// It's behavior is identical to StartSpanFromContext except that it takes an explicit
// tracer as opposed to using the global tracer.
func StartSpanFromContextWithTracer(ctx context.Context, tracer Tracer, operationName string, opts ...StartSpanOption) (Span, context.Context) {
var span Span
Copy link
Member

Choose a reason for hiding this comment

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

build fails because this statement is not needed

Choose a reason for hiding this comment

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

@devoxel would you please fix it?

if parentSpan := SpanFromContext(ctx); parentSpan != nil {
opts = append(opts, ChildOf(parentSpan.Context()))
}
Expand Down
8 changes: 4 additions & 4 deletions gocontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestStartSpanFromContext(t *testing.T) {
{
parentSpan := &testSpan{}
parentCtx := ContextWithSpan(context.Background(), parentSpan)
childSpan, childCtx := startSpanFromContextWithTracer(parentCtx, testTracer, "child")
childSpan, childCtx := StartSpanFromContextWithTracer(parentCtx, testTracer, "child")
if !childSpan.Context().(testSpanContext).HasParent {
t.Errorf("Failed to find parent: %v", childSpan)
}
Expand All @@ -48,7 +48,7 @@ func TestStartSpanFromContext(t *testing.T) {
// Test the case where there *is not* a Span in the Context.
{
emptyCtx := context.Background()
childSpan, childCtx := startSpanFromContextWithTracer(emptyCtx, testTracer, "child")
childSpan, childCtx := StartSpanFromContextWithTracer(emptyCtx, testTracer, "child")
if childSpan.Context().(testSpanContext).HasParent {
t.Errorf("Should not have found parent: %v", childSpan)
}
Expand All @@ -64,7 +64,7 @@ func TestStartSpanFromContextOptions(t *testing.T) {
// Test options are passed to tracer

startTime := time.Now().Add(-10 * time.Second) // ten seconds ago
span, ctx := startSpanFromContextWithTracer(
span, ctx := StartSpanFromContextWithTracer(
context.Background(), testTracer, "parent", StartTime(startTime), Tag{"component", "test"})

assert.Equal(t, "test", span.(testSpan).Tags["component"])
Expand All @@ -73,7 +73,7 @@ func TestStartSpanFromContextOptions(t *testing.T) {
// Test it also works for a child span

childStartTime := startTime.Add(3 * time.Second)
childSpan, _ := startSpanFromContextWithTracer(
childSpan, _ := StartSpanFromContextWithTracer(
ctx, testTracer, "child", StartTime(childStartTime))

assert.Equal(t, childSpan.(testSpan).Tags["component"], nil)
Expand Down