exporter.go 2.7 KB

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