trace.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
  15. import (
  16. "context"
  17. "encoding/json"
  18. "sync"
  19. "time"
  20. "go.opentelemetry.io/otel/sdk/trace"
  21. "go.opentelemetry.io/otel/sdk/trace/tracetest"
  22. )
  23. var zeroTime time.Time
  24. var _ trace.SpanExporter = &Exporter{}
  25. // New creates an Exporter with the passed options.
  26. func New(options ...Option) (*Exporter, error) {
  27. cfg, err := newConfig(options...)
  28. if err != nil {
  29. return nil, err
  30. }
  31. enc := json.NewEncoder(cfg.Writer)
  32. if cfg.PrettyPrint {
  33. enc.SetIndent("", "\t")
  34. }
  35. return &Exporter{
  36. encoder: enc,
  37. timestamps: cfg.Timestamps,
  38. }, nil
  39. }
  40. // Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
  41. type Exporter struct {
  42. encoder *json.Encoder
  43. encoderMu sync.Mutex
  44. timestamps bool
  45. stoppedMu sync.RWMutex
  46. stopped bool
  47. }
  48. // ExportSpans writes spans in json format to stdout.
  49. func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
  50. e.stoppedMu.RLock()
  51. stopped := e.stopped
  52. e.stoppedMu.RUnlock()
  53. if stopped {
  54. return nil
  55. }
  56. if len(spans) == 0 {
  57. return nil
  58. }
  59. stubs := tracetest.SpanStubsFromReadOnlySpans(spans)
  60. e.encoderMu.Lock()
  61. defer e.encoderMu.Unlock()
  62. for i := range stubs {
  63. stub := &stubs[i]
  64. // Remove timestamps
  65. if !e.timestamps {
  66. stub.StartTime = zeroTime
  67. stub.EndTime = zeroTime
  68. for j := range stub.Events {
  69. ev := &stub.Events[j]
  70. ev.Time = zeroTime
  71. }
  72. }
  73. // Encode span stubs, one by one
  74. if err := e.encoder.Encode(stub); err != nil {
  75. return err
  76. }
  77. }
  78. return nil
  79. }
  80. // Shutdown is called to stop the exporter, it performs no action.
  81. func (e *Exporter) Shutdown(ctx context.Context) error {
  82. e.stoppedMu.Lock()
  83. e.stopped = true
  84. e.stoppedMu.Unlock()
  85. select {
  86. case <-ctx.Done():
  87. return ctx.Err()
  88. default:
  89. }
  90. return nil
  91. }
  92. // MarshalLog is the marshaling function used by the logging system to represent this exporter.
  93. func (e *Exporter) MarshalLog() interface{} {
  94. return struct {
  95. Type string
  96. WithTimestamps bool
  97. }{
  98. Type: "stdout",
  99. WithTimestamps: e.timestamps,
  100. }
  101. }