1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package trace // import "go.opentelemetry.io/otel/sdk/trace"
16
17import (
18	"context"
19
20	"go.opentelemetry.io/otel"
21	export "go.opentelemetry.io/otel/sdk/export/trace"
22)
23
24// SimpleSpanProcessor is a SpanProcessor that synchronously sends all
25// SpanSnapshots to a trace.Exporter when the span finishes.
26type SimpleSpanProcessor struct {
27	e export.SpanExporter
28}
29
30var _ SpanProcessor = (*SimpleSpanProcessor)(nil)
31
32// NewSimpleSpanProcessor returns a new SimpleSpanProcessor that will
33// synchronously send SpanSnapshots to the exporter.
34func NewSimpleSpanProcessor(exporter export.SpanExporter) *SimpleSpanProcessor {
35	ssp := &SimpleSpanProcessor{
36		e: exporter,
37	}
38	return ssp
39}
40
41// OnStart method does nothing.
42func (ssp *SimpleSpanProcessor) OnStart(parent context.Context, s ReadWriteSpan) {
43}
44
45// OnEnd method exports a ReadOnlySpan using the associated exporter.
46func (ssp *SimpleSpanProcessor) OnEnd(s ReadOnlySpan) {
47	if ssp.e != nil && s.SpanContext().IsSampled() {
48		ss := s.Snapshot()
49		if err := ssp.e.ExportSpans(context.Background(), []*export.SpanSnapshot{ss}); err != nil {
50			otel.Handle(err)
51		}
52	}
53}
54
55// Shutdown method does nothing. There is no data to cleanup.
56func (ssp *SimpleSpanProcessor) Shutdown(_ context.Context) error {
57	return nil
58}
59
60// ForceFlush does nothing as there is no data to flush.
61func (ssp *SimpleSpanProcessor) ForceFlush() {
62}
63