2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-16 13:58:15 -07:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-11-04 18:10:58 +01:00
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
2019-09-16 13:58:15 -07:00
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2021-03-08 17:50:15 +00:00
|
|
|
"sync"
|
2019-10-08 20:56:58 +02:00
|
|
|
|
2020-11-16 18:30:54 +01:00
|
|
|
"go.opentelemetry.io/otel"
|
2019-11-05 13:08:55 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2019-10-08 20:56:58 +02:00
|
|
|
)
|
|
|
|
|
2021-03-05 16:08:29 +00:00
|
|
|
// simpleSpanProcessor is a SpanProcessor that synchronously sends all
|
2021-03-08 17:50:15 +00:00
|
|
|
// completed Spans to a trace.Exporter immediately.
|
2021-03-05 16:08:29 +00:00
|
|
|
type simpleSpanProcessor struct {
|
2021-03-08 17:50:15 +00:00
|
|
|
exporterMu sync.RWMutex
|
|
|
|
exporter export.SpanExporter
|
|
|
|
stopOnce sync.Once
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:08:29 +00:00
|
|
|
var _ SpanProcessor = (*simpleSpanProcessor)(nil)
|
2019-09-16 13:58:15 -07:00
|
|
|
|
2021-03-05 16:08:29 +00:00
|
|
|
// NewSimpleSpanProcessor returns a new SpanProcessor that will synchronously
|
|
|
|
// send completed spans to the exporter immediately.
|
|
|
|
func NewSimpleSpanProcessor(exporter export.SpanExporter) SpanProcessor {
|
|
|
|
ssp := &simpleSpanProcessor{
|
2021-03-08 17:50:15 +00:00
|
|
|
exporter: exporter,
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
return ssp
|
|
|
|
}
|
|
|
|
|
2021-03-08 17:50:15 +00:00
|
|
|
// OnStart does nothing.
|
|
|
|
func (ssp *simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
|
2019-09-16 13:58:15 -07:00
|
|
|
|
2021-03-08 17:50:15 +00:00
|
|
|
// OnEnd immediately exports a ReadOnlySpan.
|
2021-03-05 16:08:29 +00:00
|
|
|
func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) {
|
2021-03-08 17:50:15 +00:00
|
|
|
ssp.exporterMu.RLock()
|
|
|
|
defer ssp.exporterMu.RUnlock()
|
|
|
|
|
|
|
|
if ssp.exporter != nil && s.SpanContext().IsSampled() {
|
2020-12-11 06:15:44 +01:00
|
|
|
ss := s.Snapshot()
|
2021-03-08 17:50:15 +00:00
|
|
|
if err := ssp.exporter.ExportSpans(context.Background(), []*export.SpanSnapshot{ss}); err != nil {
|
2020-11-16 18:30:54 +01:00
|
|
|
otel.Handle(err)
|
2020-09-09 10:19:03 -07:00
|
|
|
}
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 17:50:15 +00:00
|
|
|
// Shutdown shuts down the exporter this SimpleSpanProcessor exports to.
|
|
|
|
func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {
|
|
|
|
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
|
2019-09-16 13:58:15 -07:00
|
|
|
}
|
2020-09-20 13:35:44 -04:00
|
|
|
|
|
|
|
// ForceFlush does nothing as there is no data to flush.
|
2021-03-05 16:08:29 +00:00
|
|
|
func (ssp *simpleSpanProcessor) ForceFlush() {
|
2020-09-20 13:35:44 -04:00
|
|
|
}
|