1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-02-09 13:37:12 +02:00

Update the SimpleSpanProcessor (#1612)

* Update the SimpleSpanProcessor

Subsequent calls to OnStart, OnEnd, and ForceFlush should be ignored
gracefully once Shutdown has been called. This implements that behavior
by shutting down the exporter and removing it from the
SimpleSpanProcessor so subsequent calls to OnEnd will be no-ops.

* Add changes to changelog

* Lock in sync.Once of shutdown instead of Shutdown scope

* Update CHANGELOG.md

Co-authored-by: Steven E. Harris <seh@panix.com>

* Release exporterMu before shutdown exporter

* Move changes to unreleased section of changelog

* Update simple_span_processor.go

revert comment change from merge

Co-authored-by: Steven E. Harris <seh@panix.com>
This commit is contained in:
Tyler Yahn 2021-03-08 17:50:15 +00:00 committed by GitHub
parent a7f7abac65
commit 569048591c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 110 additions and 39 deletions

View File

@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed ### Changed
- The SimpleSpanProcessor will now shut down the enclosed `SpanExporter` and gracefully ignore subsequent calls to `OnEnd` after `Shutdown` is called. (#1612)
- Added non-empty string check for trace `Attribute` keys. (#1659) - Added non-empty string check for trace `Attribute` keys. (#1659)
- Add `description` to SpanStatus only when `StatusCode` is set to error. (#1662) - Add `description` to SpanStatus only when `StatusCode` is set to error. (#1662)

View File

@ -16,15 +16,18 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace"
import ( import (
"context" "context"
"sync"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
export "go.opentelemetry.io/otel/sdk/export/trace" export "go.opentelemetry.io/otel/sdk/export/trace"
) )
// simpleSpanProcessor is a SpanProcessor that synchronously sends all // simpleSpanProcessor is a SpanProcessor that synchronously sends all
// SpanSnapshots to a trace.Exporter when the span finishes. // completed Spans to a trace.Exporter immediately.
type simpleSpanProcessor struct { type simpleSpanProcessor struct {
e export.SpanExporter exporterMu sync.RWMutex
exporter export.SpanExporter
stopOnce sync.Once
} }
var _ SpanProcessor = (*simpleSpanProcessor)(nil) var _ SpanProcessor = (*simpleSpanProcessor)(nil)
@ -33,28 +36,43 @@ var _ SpanProcessor = (*simpleSpanProcessor)(nil)
// send completed spans to the exporter immediately. // send completed spans to the exporter immediately.
func NewSimpleSpanProcessor(exporter export.SpanExporter) SpanProcessor { func NewSimpleSpanProcessor(exporter export.SpanExporter) SpanProcessor {
ssp := &simpleSpanProcessor{ ssp := &simpleSpanProcessor{
e: exporter, exporter: exporter,
} }
return ssp return ssp
} }
// OnStart method does nothing. // OnStart does nothing.
func (ssp *simpleSpanProcessor) OnStart(parent context.Context, s ReadWriteSpan) { func (ssp *simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
}
// OnEnd method exports a ReadOnlySpan using the associated exporter. // OnEnd immediately exports a ReadOnlySpan.
func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) { func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) {
if ssp.e != nil && s.SpanContext().IsSampled() { ssp.exporterMu.RLock()
defer ssp.exporterMu.RUnlock()
if ssp.exporter != nil && s.SpanContext().IsSampled() {
ss := s.Snapshot() ss := s.Snapshot()
if err := ssp.e.ExportSpans(context.Background(), []*export.SpanSnapshot{ss}); err != nil { if err := ssp.exporter.ExportSpans(context.Background(), []*export.SpanSnapshot{ss}); err != nil {
otel.Handle(err) otel.Handle(err)
} }
} }
} }
// Shutdown method does nothing. There is no data to cleanup. // Shutdown shuts down the exporter this SimpleSpanProcessor exports to.
func (ssp *simpleSpanProcessor) Shutdown(_ context.Context) error { func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {
return nil var err error
ssp.stopOnce.Do(func() {
ssp.exporterMu.Lock()
exporter := ssp.exporter
// Set exporter to nil so subsequent calls to OnEnd are ignored
// gracefully.
ssp.exporter = nil
ssp.exporterMu.Unlock()
// Clear the ssp.exporter prior to shutting it down so if that creates
// a span that needs to be exported there is no deadlock.
err = exporter.Shutdown(ctx)
})
return err
} }
// ForceFlush does nothing as there is no data to flush. // ForceFlush does nothing as there is no data to flush.

View File

@ -24,8 +24,14 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
) )
var (
tid, _ = trace.TraceIDFromHex("01020304050607080102040810203040")
sid, _ = trace.SpanIDFromHex("0102040810203040")
)
type testExporter struct { type testExporter struct {
spans []*export.SpanSnapshot spans []*export.SpanSnapshot
shutdown bool
} }
func (t *testExporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapshot) error { func (t *testExporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapshot) error {
@ -33,36 +39,27 @@ func (t *testExporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapsho
return nil return nil
} }
func (t *testExporter) Shutdown(context.Context) error { return nil } func (t *testExporter) Shutdown(context.Context) error {
t.shutdown = true
return nil
}
var _ export.SpanExporter = (*testExporter)(nil) var _ export.SpanExporter = (*testExporter)(nil)
func TestNewSimpleSpanProcessor(t *testing.T) { func TestNewSimpleSpanProcessor(t *testing.T) {
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{}) if ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{}); ssp == nil {
if ssp == nil { t.Error("failed to create new SimpleSpanProcessor")
t.Errorf("Error creating new instance of SimpleSpanProcessor\n")
} }
} }
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) { func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) {
ssp := sdktrace.NewSimpleSpanProcessor(nil) if ssp := sdktrace.NewSimpleSpanProcessor(nil); ssp == nil {
if ssp == nil { t.Error("failed to create new SimpleSpanProcessor with nil exporter")
t.Errorf("Error creating new instance of SimpleSpanProcessor with nil Exporter\n")
} }
} }
func TestSimpleSpanProcessorOnEnd(t *testing.T) { func startSpan(tp trace.TracerProvider) trace.Span {
tp := basicTracerProvider(t)
te := testExporter{}
ssp := sdktrace.NewSimpleSpanProcessor(&te)
if ssp == nil {
t.Errorf("Error creating new instance of SimpleSpanProcessor with nil Exporter\n")
}
tp.RegisterSpanProcessor(ssp)
tr := tp.Tracer("SimpleSpanProcessor") tr := tp.Tracer("SimpleSpanProcessor")
tid, _ := trace.TraceIDFromHex("01020304050607080102040810203040")
sid, _ := trace.SpanIDFromHex("0102040810203040")
sc := trace.SpanContext{ sc := trace.SpanContext{
TraceID: tid, TraceID: tid,
SpanID: sid, SpanID: sid,
@ -70,7 +67,16 @@ func TestSimpleSpanProcessorOnEnd(t *testing.T) {
} }
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc) ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
_, span := tr.Start(ctx, "OnEnd") _, span := tr.Start(ctx, "OnEnd")
span.End() return span
}
func TestSimpleSpanProcessorOnEnd(t *testing.T) {
tp := basicTracerProvider(t)
te := testExporter{}
ssp := sdktrace.NewSimpleSpanProcessor(&te)
tp.RegisterSpanProcessor(ssp)
startSpan(tp).End()
wantTraceID := tid wantTraceID := tid
gotTraceID := te.spans[0].SpanContext.TraceID gotTraceID := te.spans[0].SpanContext.TraceID
@ -80,14 +86,60 @@ func TestSimpleSpanProcessorOnEnd(t *testing.T) {
} }
func TestSimpleSpanProcessorShutdown(t *testing.T) { func TestSimpleSpanProcessorShutdown(t *testing.T) {
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{}) exporter := &testExporter{}
if ssp == nil { ssp := sdktrace.NewSimpleSpanProcessor(exporter)
t.Errorf("Error creating new instance of SimpleSpanProcessor\n")
return // Ensure we can export a span before we test we cannot after shutdown.
tp := basicTracerProvider(t)
tp.RegisterSpanProcessor(ssp)
startSpan(tp).End()
nExported := len(exporter.spans)
if nExported != 1 {
t.Error("failed to verify span export")
} }
err := ssp.Shutdown(context.Background()) if err := ssp.Shutdown(context.Background()); err != nil {
if err != nil { t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
t.Error("Error shutting the SimpleSpanProcessor down\n") }
if !exporter.shutdown {
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
}
startSpan(tp).End()
if len(exporter.spans) > nExported {
t.Error("exported span to shutdown exporter")
} }
} }
func TestSimpleSpanProcessorShutdownOnEndConcurrency(t *testing.T) {
exporter := &testExporter{}
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
tp := basicTracerProvider(t)
tp.RegisterSpanProcessor(ssp)
stop := make(chan struct{})
done := make(chan struct{})
go func() {
defer func() {
done <- struct{}{}
}()
for {
select {
case <-stop:
return
default:
startSpan(tp).End()
}
}
}()
if err := ssp.Shutdown(context.Background()); err != nil {
t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
}
if !exporter.shutdown {
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
}
stop <- struct{}{}
<-done
}