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

Commit

Permalink
Create Go execution tracer task for each span
Browse files Browse the repository at this point in the history
This allows us to associate OpenCensus spans with execution
tracer tasks and allow users to have fine-grained details
about the runtime events happened in the lifetime of a
distributed tracing span.
  • Loading branch information
rakyll committed Apr 18, 2018
1 parent 730e847 commit 42ab092
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Span struct {
// spanStore is the spanStore this span belongs to, if any, otherwise it is nil.
*spanStore
exportOnce sync.Once

executionTracerSpanEnd func() // ends the execution tracer span
}

// IsRecordingEvents returns true if events are being recorded for this span.
Expand Down Expand Up @@ -162,6 +164,9 @@ func StartSpan(ctx context.Context, name string, o ...StartOption) (context.Cont
op(&opts)
}
span := startSpanInternal(name, parent != SpanContext{}, parent, false, opts)

ctx, end := startExecutionTracerSpan(ctx, name)
span.executionTracerSpanEnd = end
return NewContext(ctx, span), span
}

Expand All @@ -175,6 +180,8 @@ func StartSpanWithRemoteParent(ctx context.Context, name string, parent SpanCont
op(&opts)
}
span := startSpanInternal(name, parent != SpanContext{}, parent, true, opts)
ctx, end := startExecutionTracerSpan(ctx, name)
span.executionTracerSpanEnd = end
return NewContext(ctx, span), span
}

Expand Down Expand Up @@ -259,6 +266,7 @@ func (s *Span) End() {
return
}
s.exportOnce.Do(func() {
s.executionTracerSpanEnd()
// TODO: optimize to avoid this call if sd won't be used.
sd := s.makeSpanData()
sd.EndTime = internal.MonotonicEndTime(sd.StartTime)
Expand Down
26 changes: 26 additions & 0 deletions trace/trace_go11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build go1.11

package trace

import (
"context"
t "runtime/trace"
)

func startExecutionTracerSpan(ctx context.Context, name string) (context.Context, func()) {
return t.NewContext(ctx, name)
}
25 changes: 25 additions & 0 deletions trace/trace_nongo11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !go1.11

package trace

import (
"context"
)

func startExecutionTracerSpan(ctx context.Context, name string) (context.Context, func()) {
return ctx, func() {}
}

0 comments on commit 42ab092

Please sign in to comment.