2020-07-22 21:34:44 +02:00
|
|
|
// Copyright The OpenTelemetry 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.
|
|
|
|
|
2021-06-16 17:32:42 +02:00
|
|
|
package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
2020-07-22 21:34:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-09-09 19:19:03 +02:00
|
|
|
"sync"
|
2021-08-23 23:29:51 +02:00
|
|
|
"time"
|
2020-07-22 21:34:44 +02:00
|
|
|
|
2021-04-07 17:03:43 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/trace"
|
2021-05-05 01:45:13 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
2020-07-22 21:34:44 +02:00
|
|
|
)
|
|
|
|
|
2021-08-23 23:29:51 +02:00
|
|
|
var zeroTime time.Time
|
|
|
|
|
2021-08-25 17:48:48 +02:00
|
|
|
var _ trace.SpanExporter = &Exporter{}
|
|
|
|
|
|
|
|
// New creates an Exporter with the passed options.
|
|
|
|
func New(options ...Option) (*Exporter, error) {
|
|
|
|
cfg, err := newConfig(options...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(cfg.Writer)
|
|
|
|
if cfg.PrettyPrint {
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Exporter{
|
|
|
|
encoder: enc,
|
|
|
|
timestamps: cfg.Timestamps,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
// Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
|
2021-08-25 17:48:48 +02:00
|
|
|
type Exporter struct {
|
|
|
|
encoder *json.Encoder
|
2021-09-30 21:58:26 +02:00
|
|
|
encoderMu sync.Mutex
|
2021-08-25 17:48:48 +02:00
|
|
|
timestamps bool
|
2020-07-22 21:34:44 +02:00
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
stoppedMu sync.RWMutex
|
|
|
|
stopped bool
|
2020-07-22 21:34:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 01:45:13 +02:00
|
|
|
// ExportSpans writes spans in json format to stdout.
|
2021-08-25 17:48:48 +02:00
|
|
|
func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
|
2020-09-09 19:19:03 +02:00
|
|
|
e.stoppedMu.RLock()
|
|
|
|
stopped := e.stopped
|
|
|
|
e.stoppedMu.RUnlock()
|
|
|
|
if stopped {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:32:42 +02:00
|
|
|
if len(spans) == 0 {
|
2020-09-09 19:19:03 +02:00
|
|
|
return nil
|
2020-07-22 21:34:44 +02:00
|
|
|
}
|
2021-08-25 17:48:48 +02:00
|
|
|
|
2021-08-23 23:29:51 +02:00
|
|
|
stubs := tracetest.SpanStubsFromReadOnlySpans(spans)
|
2021-08-25 17:48:48 +02:00
|
|
|
|
2021-09-30 21:58:26 +02:00
|
|
|
e.encoderMu.Lock()
|
|
|
|
defer e.encoderMu.Unlock()
|
2021-08-25 17:48:48 +02:00
|
|
|
for i := range stubs {
|
|
|
|
stub := &stubs[i]
|
|
|
|
// Remove timestamps
|
|
|
|
if !e.timestamps {
|
2021-08-23 23:29:51 +02:00
|
|
|
stub.StartTime = zeroTime
|
|
|
|
stub.EndTime = zeroTime
|
|
|
|
for j := range stub.Events {
|
|
|
|
ev := &stub.Events[j]
|
|
|
|
ev.Time = zeroTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:48:48 +02:00
|
|
|
// Encode span stubs, one by one
|
|
|
|
if err := e.encoder.Encode(stub); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-09 19:19:03 +02:00
|
|
|
}
|
2021-08-25 17:48:48 +02:00
|
|
|
return nil
|
2020-09-09 19:19:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 02:28:13 +02:00
|
|
|
// Shutdown is called to stop the exporter, it performs no action.
|
2021-08-25 17:48:48 +02:00
|
|
|
func (e *Exporter) Shutdown(ctx context.Context) error {
|
2020-09-09 19:19:03 +02:00
|
|
|
e.stoppedMu.Lock()
|
|
|
|
e.stopped = true
|
|
|
|
e.stoppedMu.Unlock()
|
2020-07-22 21:34:44 +02:00
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2020-07-22 21:34:44 +02:00
|
|
|
}
|
2020-09-09 19:19:03 +02:00
|
|
|
return nil
|
2020-07-22 21:34:44 +02:00
|
|
|
}
|
2022-01-11 02:58:01 +02:00
|
|
|
|
|
|
|
// MarshalLog is the marshaling function used by the logging system to represent this exporter.
|
|
|
|
func (e *Exporter) MarshalLog() interface{} {
|
|
|
|
return struct {
|
|
|
|
Type string
|
|
|
|
WithTimestamps bool
|
|
|
|
}{
|
|
|
|
Type: "stdout",
|
|
|
|
WithTimestamps: e.timestamps,
|
|
|
|
}
|
|
|
|
}
|