simple_span_processor.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 trace // import "go.opentelemetry.io/otel/sdk/trace"
  15. import (
  16. "context"
  17. "sync"
  18. "go.opentelemetry.io/otel"
  19. )
  20. // simpleSpanProcessor is a SpanProcessor that synchronously sends all
  21. // completed Spans to a trace.Exporter immediately.
  22. type simpleSpanProcessor struct {
  23. exporterMu sync.RWMutex
  24. exporter SpanExporter
  25. stopOnce sync.Once
  26. }
  27. var _ SpanProcessor = (*simpleSpanProcessor)(nil)
  28. // NewSimpleSpanProcessor returns a new SpanProcessor that will synchronously
  29. // send completed spans to the exporter immediately.
  30. //
  31. // This SpanProcessor is not recommended for production use. The synchronous
  32. // nature of this SpanProcessor make it good for testing, debugging, or
  33. // showing examples of other feature, but it will be slow and have a high
  34. // computation resource usage overhead. The BatchSpanProcessor is recommended
  35. // for production use instead.
  36. func NewSimpleSpanProcessor(exporter SpanExporter) SpanProcessor {
  37. ssp := &simpleSpanProcessor{
  38. exporter: exporter,
  39. }
  40. return ssp
  41. }
  42. // OnStart does nothing.
  43. func (ssp *simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
  44. // OnEnd immediately exports a ReadOnlySpan.
  45. func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) {
  46. ssp.exporterMu.RLock()
  47. defer ssp.exporterMu.RUnlock()
  48. if ssp.exporter != nil && s.SpanContext().TraceFlags().IsSampled() {
  49. if err := ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s}); err != nil {
  50. otel.Handle(err)
  51. }
  52. }
  53. }
  54. // Shutdown shuts down the exporter this SimpleSpanProcessor exports to.
  55. func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {
  56. var err error
  57. ssp.stopOnce.Do(func() {
  58. stopFunc := func(exp SpanExporter) (<-chan error, func()) {
  59. done := make(chan error)
  60. return done, func() { done <- exp.Shutdown(ctx) }
  61. }
  62. // The exporter field of the simpleSpanProcessor needs to be zeroed to
  63. // signal it is shut down, meaning all subsequent calls to OnEnd will
  64. // be gracefully ignored. This needs to be done synchronously to avoid
  65. // any race condition.
  66. //
  67. // A closure is used to keep reference to the exporter and then the
  68. // field is zeroed. This ensures the simpleSpanProcessor is shut down
  69. // before the exporter. This order is important as it avoids a
  70. // potential deadlock. If the exporter shut down operation generates a
  71. // span, that span would need to be exported. Meaning, OnEnd would be
  72. // called and try acquiring the lock that is held here.
  73. ssp.exporterMu.Lock()
  74. done, shutdown := stopFunc(ssp.exporter)
  75. ssp.exporter = nil
  76. ssp.exporterMu.Unlock()
  77. go shutdown()
  78. // Wait for the exporter to shut down or the deadline to expire.
  79. select {
  80. case err = <-done:
  81. case <-ctx.Done():
  82. // It is possible for the exporter to have immediately shut down
  83. // and the context to be done simultaneously. In that case this
  84. // outer select statement will randomly choose a case. This will
  85. // result in a different returned error for similar scenarios.
  86. // Instead, double check if the exporter shut down at the same
  87. // time and return that error if so. This will ensure consistency
  88. // as well as ensure the caller knows the exporter shut down
  89. // successfully (they can already determine if the deadline is
  90. // expired given they passed the context).
  91. select {
  92. case err = <-done:
  93. default:
  94. err = ctx.Err()
  95. }
  96. }
  97. })
  98. return err
  99. }
  100. // ForceFlush does nothing as there is no data to flush.
  101. func (ssp *simpleSpanProcessor) ForceFlush(context.Context) error {
  102. return nil
  103. }
  104. // MarshalLog is the marshaling function used by the logging system to represent this Span Processor.
  105. func (ssp *simpleSpanProcessor) MarshalLog() interface{} {
  106. return struct {
  107. Type string
  108. Exporter SpanExporter
  109. }{
  110. Type: "SimpleSpanProcessor",
  111. Exporter: ssp.exporter,
  112. }
  113. }