exporter.go 2.9 KB

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