Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otel): Don't require entrypoint config to initialize OTel #2579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/relayproxy/api/opentelemetry/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

// 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
}
Expand All @@ -40,6 +40,8 @@
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)

Check warning on line 44 in cmd/relayproxy/api/opentelemetry/otel.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/api/opentelemetry/otel.go#L44

Added line #L44 was not covered by tests
}

exporter, err := autoexport.NewSpanExporter(ctx)
Expand Down
7 changes: 6 additions & 1 deletion cmd/relayproxy/api/opentelemetry/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions cmd/relayproxy/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,10 @@
}

// 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

Check warning on line 126 in cmd/relayproxy/api/server.go

View check run for this annotation

Codecov / codecov/patch

cmd/relayproxy/api/server.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}

// starting the main application
Expand All @@ -138,7 +136,7 @@
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))
}
Expand Down
15 changes: 8 additions & 7 deletions website/docs/relay_proxy/monitor_relay_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading