tracing.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright 2020 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. "fmt"
  18. "os"
  19. "strings"
  20. "github.com/docker/compose/v2/internal"
  21. "go.opentelemetry.io/otel/attribute"
  22. "github.com/docker/cli/cli/command"
  23. "github.com/moby/buildkit/util/tracing/detect"
  24. _ "github.com/moby/buildkit/util/tracing/env" //nolint:blank-imports
  25. "go.opentelemetry.io/otel"
  26. "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
  27. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
  28. "go.opentelemetry.io/otel/propagation"
  29. "go.opentelemetry.io/otel/sdk/resource"
  30. sdktrace "go.opentelemetry.io/otel/sdk/trace"
  31. semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
  32. )
  33. func init() {
  34. detect.ServiceName = "compose"
  35. // do not log tracing errors to stdio
  36. otel.SetErrorHandler(skipErrors{})
  37. }
  38. // OTLPConfig contains the necessary values to initialize an OTLP client
  39. // manually.
  40. //
  41. // This supports a minimal set of options based on what is necessary for
  42. // automatic OTEL configuration from Docker context metadata.
  43. type OTLPConfig struct {
  44. Endpoint string
  45. }
  46. // ShutdownFunc flushes and stops an OTEL exporter.
  47. type ShutdownFunc func(ctx context.Context) error
  48. // envMap is a convenience type for OS environment variables.
  49. type envMap map[string]string
  50. func InitTracing(dockerCli command.Cli) (ShutdownFunc, error) {
  51. // set global propagator to tracecontext (the default is no-op).
  52. otel.SetTextMapPropagator(propagation.TraceContext{})
  53. return InitProvider(dockerCli)
  54. }
  55. func InitProvider(dockerCli command.Cli) (ShutdownFunc, error) {
  56. ctx := context.Background()
  57. var errs []error
  58. var exporters []sdktrace.SpanExporter
  59. envClient, otelEnv := traceClientFromEnv()
  60. if envClient != nil {
  61. if envExporter, err := otlptrace.New(ctx, envClient); err != nil {
  62. errs = append(errs, err)
  63. } else if envExporter != nil {
  64. exporters = append(exporters, envExporter)
  65. }
  66. }
  67. if dcClient, err := traceClientFromDockerContext(dockerCli, otelEnv); err != nil {
  68. errs = append(errs, err)
  69. } else if dcClient != nil {
  70. if dcExporter, err := otlptrace.New(ctx, dcClient); err != nil {
  71. errs = append(errs, err)
  72. } else if dcExporter != nil {
  73. exporters = append(exporters, dcExporter)
  74. }
  75. }
  76. if len(errs) != 0 {
  77. return nil, errors.Join(errs...)
  78. }
  79. res, err := resource.New(
  80. ctx,
  81. resource.WithAttributes(
  82. semconv.ServiceName("compose"),
  83. semconv.ServiceVersion(internal.Version),
  84. attribute.String("docker.context", dockerCli.CurrentContext()),
  85. ),
  86. )
  87. if err != nil {
  88. return nil, fmt.Errorf("failed to create resource: %w", err)
  89. }
  90. muxExporter := MuxExporter{exporters: exporters}
  91. tracerProvider := sdktrace.NewTracerProvider(
  92. sdktrace.WithResource(res),
  93. sdktrace.WithBatcher(muxExporter),
  94. )
  95. otel.SetTracerProvider(tracerProvider)
  96. // Shutdown will flush any remaining spans and shut down the exporter.
  97. return tracerProvider.Shutdown, nil
  98. }
  99. // traceClientFromEnv creates a GRPC OTLP client based on OS environment
  100. // variables.
  101. //
  102. // https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/
  103. func traceClientFromEnv() (otlptrace.Client, envMap) {
  104. hasOtelEndpointInEnv := false
  105. otelEnv := make(map[string]string)
  106. for _, kv := range os.Environ() {
  107. k, v, ok := strings.Cut(kv, "=")
  108. if !ok {
  109. continue
  110. }
  111. if strings.HasPrefix(k, "OTEL_") {
  112. otelEnv[k] = v
  113. if strings.HasSuffix(k, "ENDPOINT") {
  114. hasOtelEndpointInEnv = true
  115. }
  116. }
  117. }
  118. if !hasOtelEndpointInEnv {
  119. return nil, nil
  120. }
  121. client := otlptracegrpc.NewClient()
  122. return client, otelEnv
  123. }