Home > Enterprise >  Configuring OTLP exporter through environment variables
Configuring OTLP exporter through environment variables

Time:11-17

Currently I am trying to configure my OTLP exporter using environment variables. This is supposed to be possible as per the official docs.

In particular, I want to focus on the OTEL_EXPORTER_OTLP_ENDPOINT one, which is allowed for the OTLPtrace exporter. According to the comments in their code, the environment variable takes precedence over any other value set in the code.

I wrote a very basic HTTP application in Go, which is instrumented with OpenTelemetry. When I specify the exporter endpoint explicitly in the code like:

exporter, err := otlptrace.New(
        context.Background(),
        otlptracegrpc.NewClient(
            otlptracegrpc.WithInsecure(),
            otlptracegrpc.WithEndpoint("My Endpoint"),
        ),
    )

The instrumentation works just fine like that. However if I remove the otlptracegrpc.NewClient configuration from the code, it does not pick up the values set in the environment, which are set like:

OTEL_EXPORTER_OTLP_ENDPOINT="my endpoint"

So when I run this application in my debugger I can see that the exporter client has an empty value as the endpoint, yet I can pick them up within my program as:

exporterEndpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")

This I interpret as the variables being actually present at the time the code is being executed, which was my main fear.

Why is this? Am I missing something here? Should I populate the environment variable differently (I see there are "options" for the environment variable in the official docs, but no examples)?

CodePudding user response:

From what I see from your code, you're trying to contact the OTLP exporter through a gRPC call. If you see, in their documentation they wrote this in line 71:

This option has no effect if WithGRPCConn is used.

This means that you can completely avoid passing this variable at all to the otlptracegrpc.NewClient function. I instantiate a gRPC client with this code and it works:

func newOtlpExporter(ctx context.Context) (trace.SpanExporter, error) {
    client := otlptracegrpc.NewClient(otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock()))
    exporter, err := otlptrace.New(ctx, client)
    if err != nil {
        panic(err)
    }
    return exporter, err
}

Back to your question, you're right with your guess but only if you're sending metrics, traces, and so on through HTTPS calls.
Let me know if this helps to solve the issue or if anything else is needed!

Edit 1

I overlooked this. The comment you linked in the question is taken from the wrong file. The correct line is this: https://github.com/open-telemetry/opentelemetry-go/blob/48a05478e238698e02b4025ac95a11ecd6bcc5ad/exporters/otlp/otlptrace/otlptracegrpc/options.go#L71
As you can see, the comment is clearer and you have only two options:

  1. Provide your own endpoint address
  2. Use the default one which is localhost:0.0.0.0:4317

Let me know if helps!

  • Related