diff --git a/cmd/relayproxy/api/opentelemetry/otel.go b/cmd/relayproxy/api/opentelemetry/otel.go index 4afd25f69f6..a7ff6498d93 100644 --- a/cmd/relayproxy/api/opentelemetry/otel.go +++ b/cmd/relayproxy/api/opentelemetry/otel.go @@ -28,9 +28,9 @@ func NewOtelService() OtelService { // Init the OpenTelemetry service func (s *OtelService) Init(ctx context.Context, zapLog *zap.Logger, config config.Config) error { - // OTEL_SDK_DISABLED is not supported by the Go SDK, but is a standard env - // var defined by the OTel spec. We'll use it to disable the trace provider. - if config.OtelConfig.SDK.Disabled { + // Require the endpoint to be set either by the openTelemetryOtlpEndpoint + // config element or otel.exporter.otlp.endpoint + if config.OpenTelemetryOtlpEndpoint == "" && config.OtelConfig.Exporter.Otlp.Endpoint == "" { otel.SetTracerProvider(noop.NewTracerProvider()) return nil } @@ -40,6 +40,8 @@ func (s *OtelService) Init(ctx context.Context, zapLog *zap.Logger, config confi config.OtelConfig.Exporter.Otlp.Endpoint == "" { config.OtelConfig.Exporter.Otlp.Endpoint = config.OpenTelemetryOtlpEndpoint _ = os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", config.OpenTelemetryOtlpEndpoint) + } else if config.OtelConfig.Exporter.Otlp.Endpoint != "" && os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" { + _ = os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", config.OtelConfig.Exporter.Otlp.Endpoint) } exporter, err := autoexport.NewSpanExporter(ctx) diff --git a/cmd/relayproxy/api/opentelemetry/otel_test.go b/cmd/relayproxy/api/opentelemetry/otel_test.go index da1b99bfcfe..7e04622c6e8 100644 --- a/cmd/relayproxy/api/opentelemetry/otel_test.go +++ b/cmd/relayproxy/api/opentelemetry/otel_test.go @@ -213,7 +213,12 @@ func TestInit(t *testing.T) { expectedErr := errors.New("test error") - err := svc.Init(context.Background(), testLogger, config.Config{}) + t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "https://example.com:4318") + f := pflag.NewFlagSet("config", pflag.ContinueOnError) + c, errC := config.New(f, zap.L(), "1.X.X") + require.NoError(t, errC) + c.OpenTelemetryOtlpEndpoint = "https://bogus.com:4317" + err := svc.Init(context.Background(), testLogger, *c) require.NoError(t, err) defer func() { _ = svc.Stop(context.Background()) }() otel.GetErrorHandler().Handle(expectedErr) diff --git a/cmd/relayproxy/api/server.go b/cmd/relayproxy/api/server.go index 8054561cdd8..65185470492 100644 --- a/cmd/relayproxy/api/server.go +++ b/cmd/relayproxy/api/server.go @@ -120,12 +120,10 @@ func (s *Server) Start() { } // start the OpenTelemetry tracing service - if s.config.OpenTelemetryOtlpEndpoint != "" { - err := s.otelService.Init(context.Background(), s.zapLog, *s.config) - if err != nil { - s.zapLog.Error("error while initializing Otel", zap.Error(err)) - // we can continue because otel is not mandatory to start the server - } + err := s.otelService.Init(context.Background(), s.zapLog, *s.config) + if err != nil { + s.zapLog.Error("error while initializing OTel, continuing without tracing enabled", zap.Error(err)) + // we can continue because otel is not mandatory to start the server } // starting the main application @@ -138,7 +136,7 @@ func (s *Server) Start() { zap.String("address", address), zap.String("version", s.config.Version)) - err := s.apiEcho.Start(address) + err = s.apiEcho.Start(address) if err != nil && !errors.Is(err, http.ErrServerClosed) { s.zapLog.Fatal("Error starting relay proxy", zap.Error(err)) } diff --git a/website/docs/relay_proxy/monitor_relay_proxy.md b/website/docs/relay_proxy/monitor_relay_proxy.md index 740c3e1df0f..e4debfbe510 100644 --- a/website/docs/relay_proxy/monitor_relay_proxy.md +++ b/website/docs/relay_proxy/monitor_relay_proxy.md @@ -10,17 +10,18 @@ The **relay proxy** is able to trace the requests it is handling. This is done b ### Configuration -By default, the relay proxy will attempt to send traces to an OpenTelemetry -collector or compatible agent running at `http://localhost:4318` using the -`http/protobuf` protocol. -To override the endpoint, set the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. -To override the protocol, set the `OTEL_EXPORTER_OTLP_PROTOCOL` environment variable. +The relay proxy will attempt to send traces to an OpenTelemetry collector or +compatible agent if an OpenTelemetry exporter endpoint is configured. + +To set the endpoint, set the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable +or set `otel.exporter.otlp.endpoint` in the configuration file. + +To override the protocol, set the `OTEL_EXPORTER_OTLP_PROTOCOL` environment variable (`http/protobuf` is the default) or set `otel.exporter.otlp.protocol` in the configuration file. + See [the OpenTelemetry documentation](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/) for more information. All your requests will be traced and sent to the collector with the service name **`go-feature-flag`**. -To disable tracing, set the `OTEL_SDK_DISABLED` environment variable to `true`. - :::note If you want to try the OpenTelemetry integration locally, follow this [README](https://github.com/thomaspoignant/go-feature-flag/tree/main/cmd/relayproxy/testdata/opentelemetry) to setup Jaeger and see your traces.