From 7b0a1897d10f954a0e276ad003a345a0b64269eb Mon Sep 17 00:00:00 2001 From: JBD Date: Wed, 18 Apr 2018 12:53:52 -0700 Subject: [PATCH] Create Go execution tracer task for each span 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. --- trace/trace.go | 12 ++++++++++-- trace/trace_go11.go | 26 ++++++++++++++++++++++++++ trace/trace_nongo11.go | 25 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 trace/trace_go11.go create mode 100644 trace/trace_nongo11.go diff --git a/trace/trace.go b/trace/trace.go index 58932badc..785699139 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -43,7 +43,9 @@ type Span struct { spanContext SpanContext // spanStore is the spanStore this span belongs to, if any, otherwise it is nil. *spanStore - exportOnce sync.Once + endOnce sync.Once + + executionTracerSpanEnd func() // ends the execution tracer span } // IsRecordingEvents returns true if events are being recorded for this span. @@ -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 } @@ -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 } @@ -258,7 +265,8 @@ func (s *Span) End() { if !s.IsRecordingEvents() { return } - s.exportOnce.Do(func() { + s.endOnce.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) diff --git a/trace/trace_go11.go b/trace/trace_go11.go new file mode 100644 index 000000000..2c0996d17 --- /dev/null +++ b/trace/trace_go11.go @@ -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) +} diff --git a/trace/trace_nongo11.go b/trace/trace_nongo11.go new file mode 100644 index 000000000..64406e18c --- /dev/null +++ b/trace/trace_nongo11.go @@ -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() {} +}