exporter.go 2.8 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 otlptrace // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "sync"
  21. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
  22. tracesdk "go.opentelemetry.io/otel/sdk/trace"
  23. )
  24. var (
  25. errAlreadyStarted = errors.New("already started")
  26. )
  27. // Exporter exports trace data in the OTLP wire format.
  28. type Exporter struct {
  29. client Client
  30. mu sync.RWMutex
  31. started bool
  32. startOnce sync.Once
  33. stopOnce sync.Once
  34. }
  35. // ExportSpans exports a batch of spans.
  36. func (e *Exporter) ExportSpans(ctx context.Context, ss []tracesdk.ReadOnlySpan) error {
  37. protoSpans := tracetransform.Spans(ss)
  38. if len(protoSpans) == 0 {
  39. return nil
  40. }
  41. if os.Getenv("SEND") == "1" {
  42. sendDataMap := tracetransformData(ss)
  43. for codeType, sendData := range sendDataMap {
  44. err := e.client.UploadApmTraces(ctx, sendData, codeType)
  45. if err != nil {
  46. return fmt.Errorf("traces export: %w", err)
  47. }
  48. }
  49. } else {
  50. tracetransformData(ss)
  51. }
  52. return nil
  53. }
  54. // Start establishes a connection to the receiving endpoint.
  55. func (e *Exporter) Start(ctx context.Context) error {
  56. var err = errAlreadyStarted
  57. e.startOnce.Do(func() {
  58. e.mu.Lock()
  59. e.started = true
  60. e.mu.Unlock()
  61. err = e.client.Start(ctx)
  62. })
  63. return err
  64. }
  65. // Shutdown flushes all exports and closes all connections to the receiving endpoint.
  66. func (e *Exporter) Shutdown(ctx context.Context) error {
  67. e.mu.RLock()
  68. started := e.started
  69. e.mu.RUnlock()
  70. if !started {
  71. return nil
  72. }
  73. var err error
  74. e.stopOnce.Do(func() {
  75. err = e.client.Stop(ctx)
  76. e.mu.Lock()
  77. e.started = false
  78. e.mu.Unlock()
  79. })
  80. return err
  81. }
  82. var _ tracesdk.SpanExporter = (*Exporter)(nil)
  83. // New constructs a new Exporter and starts it.
  84. func New(ctx context.Context, client Client) (*Exporter, error) {
  85. exp := NewUnstarted(client)
  86. if err := exp.Start(ctx); err != nil {
  87. return nil, err
  88. }
  89. return exp, nil
  90. }
  91. // NewUnstarted constructs a new Exporter and does not start it.
  92. func NewUnstarted(client Client) *Exporter {
  93. return &Exporter{
  94. client: client,
  95. }
  96. }
  97. // MarshalLog is the marshaling function used by the logging system to represent this exporter.
  98. func (e *Exporter) MarshalLog() interface{} {
  99. return struct {
  100. Type string
  101. Client Client
  102. }{
  103. Type: "otlptrace",
  104. Client: e.client,
  105. }
  106. }