mux.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tracing
  14. import (
  15. "context"
  16. "errors"
  17. "sync"
  18. sdktrace "go.opentelemetry.io/otel/sdk/trace"
  19. )
  20. type MuxExporter struct {
  21. exporters []sdktrace.SpanExporter
  22. }
  23. func (m MuxExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
  24. var (
  25. wg sync.WaitGroup
  26. errMu sync.Mutex
  27. errs = make([]error, 0, len(m.exporters))
  28. )
  29. for _, exporter := range m.exporters {
  30. wg.Add(1)
  31. go func() {
  32. defer wg.Done()
  33. if err := exporter.ExportSpans(ctx, spans); err != nil {
  34. errMu.Lock()
  35. errs = append(errs, err)
  36. errMu.Unlock()
  37. }
  38. }()
  39. }
  40. wg.Wait()
  41. return errors.Join(errs...)
  42. }
  43. func (m MuxExporter) Shutdown(ctx context.Context) error {
  44. var (
  45. wg sync.WaitGroup
  46. errMu sync.Mutex
  47. errs = make([]error, 0, len(m.exporters))
  48. )
  49. for _, exporter := range m.exporters {
  50. wg.Add(1)
  51. go func() {
  52. defer wg.Done()
  53. if err := exporter.Shutdown(ctx); err != nil {
  54. errMu.Lock()
  55. errs = append(errs, err)
  56. errMu.Unlock()
  57. }
  58. }()
  59. }
  60. wg.Wait()
  61. return errors.Join(errs...)
  62. }