Skip to content

Configuring the SDK with Environment Variables and Files: OTCA practice questions

Every SDK must offer programmatic configuration, and most also read a common set of environment variables. Support for these variables varies by language, so the compliance matrix is the place to check a specific SDK. Values are parsed in a fixed way. An empty value counts as unset.

By Ibrahim Yesilay · Published · Updated

Independent community practice, not affiliated with or endorsed by CNCF or The Linux Foundation.

What Configuring the SDK with Environment Variables and Files covers in OTCA

Every SDK must offer programmatic configuration, and most also read a common set of environment variables. Support for these variables varies by language, so the compliance matrix is the place to check a specific SDK. Values are parsed in a fixed way. An empty value counts as unset. Booleans become true only for the case-insensitive string true, so setting OTEL_SDK_DISABLED=1 leaves the SDK enabled. Enum values are case-insensitive, and unknown values produce a warning and are ignored.

The resource is usually set first. OTEL_SERVICE_NAME sets service.name and wins over a service.name entry in OTEL_RESOURCE_ATTRIBUTES. That second variable takes comma-separated key=value pairs whose values are strings; commas and equals signs inside keys or values must be percent-encoded. Resource data provided in code has higher priority than the environment variable. If no service name is configured, the semantic conventions require unknown_service: followed by the executable name.

Sampling is controlled by OTEL_TRACES_SAMPLER, which defaults to parentbased_always_on. Other values include always_on, always_off, traceidratio and parentbased_traceidratio. OTEL_TRACES_SAMPLER_ARG supplies an argument such as a ratio between 0 and 1, but it is used only when OTEL_TRACES_SAMPLER is also set. Exporters are chosen per signal with OTEL_TRACES_EXPORTER, OTEL_METRICS_EXPORTER and OTEL_LOGS_EXPORTER, which all default to otlp. The value none turns off the automatically configured exporter for that signal, while console prints to standard output. For periodic metric export, OTEL_METRIC_EXPORT_INTERVAL defaults to 60000 milliseconds and OTEL_METRIC_EXPORT_TIMEOUT to 30000.

The OTLP exporter has its own family of variables. OTEL_EXPORTER_OTLP_PROTOCOL accepts grpc, http/protobuf and http/json, and the spec says the default should be http/protobuf unless an SDK keeps grpc for backward compatibility. Default endpoints are localhost on port 4317 for gRPC and 4318 for HTTP. With OTLP/HTTP, OTEL_EXPORTER_OTLP_ENDPOINT is a base URL to which v1/traces, v1/metrics or v1/logs is appended, but the signal-specific endpoint variables are used exactly as written. Headers use the key1=value1,key2=value2 format, and the default export timeout is ten seconds.

Limits protect memory. OTEL_ATTRIBUTE_COUNT_LIMIT defaults to 128 attributes, and attribute value length is unlimited unless configured. Model-specific variables such as OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT are checked before the general one.

Declarative configuration describes the whole SDK in a YAML file named by OTEL_CONFIG_FILE, which replaces the deprecated OTEL_EXPERIMENTAL_CONFIG_FILE. The schema is marked stable, but SDK support is still experimental. Once the file is in use, other environment variables are ignored unless the file references them with substitution syntax such as ${OTEL_EXPORTER_OTLP_ENDPOINT}.

Key points

  • OTEL_SERVICE_NAME takes precedence over service.name in OTEL_RESOURCE_ATTRIBUTES.
  • The default sampler is parentbased_always_on, and OTEL_TRACES_SAMPLER_ARG is used only when a sampler is set.
  • Trace, metric and log exporters default to otlp, and the value none disables automatic exporter setup for a signal.
  • OTLP/HTTP appends v1/<signal> to the base endpoint but uses signal-specific endpoints as written.
  • When OTEL_CONFIG_FILE is set, environment variables not referenced in the file are ignored.

Three sample questions

22 OTCA questions on Configuring the SDK with Environment Variables and Files, 22 with explanations. How practice works

Question 1

A container starts with these environment variables. Which service.name will its telemetry carry?
OTEL_RESOURCE_ATTRIBUTES=service.name=billing,deployment.environment.name=prod
OTEL_SERVICE_NAME=billing-api
  1. A.billing, because OTEL_RESOURCE_ATTRIBUTES is applied after OTEL_SERVICE_NAME
  2. B.Both values, joined as billing,billing-api in one attribute
  3. C.unknown_service, because conflicting values cancel each other
  4. D.billing-api, because OTEL_SERVICE_NAME takes precedence

Correct answer: D

The Environment Variable Specification says OTEL_SERVICE_NAME "Sets the value of the service.name resource attribute" and "If service.name is also provided in OTEL_RESOURCE_ATTRIBUTES, then OTEL_SERVICE_NAME takes precedence." The General SDK Configuration page repeats this rule. deployment.environment.name=prod is still applied from OTEL_RESOURCE_ATTRIBUTES. Why the other options are wrong. "billing, because OTEL_RESOURCE_ATTRIBUTES is applied after…": the precedence rule is the other way round. "Both values, joined…": attribute keys are unique in a resource, and service.name gets one value. "unknown_service…": that fallback applies only when no service name is configured at all.

Sources: OpenTelemetry spec: Environment Variable Specification, OpenTelemetry docs: General SDK Configuration

Question 2

An OTLP exporter must send two authentication headers to a backend. Which value of OTEL_EXPORTER_OTLP_HEADERS uses the format the specification defines?
  1. A.api-key=abc123,tenant=team-a
  2. B.X-Api-Key: abc123; X-Tenant: team-a
  3. C.{"api-key": "abc123", "x-tenant": "team-a"}
  4. D.api-key=abc123;tenant=team-a;region=eu

Correct answer: A

The OTLP Exporter spec says the OTEL_EXPORTER_OTLP_HEADERS variables "will contain a list of key value pairs, and these are expected to be represented in a format matching to the W3C Baggage i.e., key1=value1,key2=value2. Semi-colon delimited metadata is not supported." The OTLP Exporter Configuration docs repeat the same format, and there are signal-specific variants such as OTEL_EXPORTER_OTLP_TRACES_HEADERS. Why the other options are wrong. "X-Api-Key: abc123; X-Tenant: team-a": HTTP header-line syntax with colons and semicolons is not the specified format. "{"api-key": …": JSON is not accepted; the value is a plain comma-separated list of key=value pairs. "api-key=abc123;tenant=team-a;region=eu": semicolons are not pair separators, and semicolon-delimited metadata is explicitly not supported.

Sources: OpenTelemetry spec: OTLP Exporter configuration, OpenTelemetry docs: OTLP Exporter Configuration

Question 3

Application code creates a Resource with deployment.environment.name=staging, and OTEL_RESOURCE_ATTRIBUTES sets deployment.environment.name=prod. Per the Resource SDK, which value wins?
  1. A.prod, because environment variables always override values set in code
  2. B.staging, because user-provided resource data has higher priority
  3. C.Neither; the SDK reports a merge error and drops the attribute
  4. D.Both, kept as two attributes with the same key

Correct answer: B

The Resource SDK says: "The SDK MUST extract information from the OTEL_RESOURCE_ATTRIBUTES environment variable and merge this, as the secondary resource, with any resource information provided by the user, i.e. the user provided resource information has higher priority." So the value set in code (staging) wins. Why the other options are wrong. "prod, because environment variables always override code": the spec makes the environment resource secondary. "Neither; the SDK reports a merge error…": merge errors are defined for conflicting non-empty Schema URLs, not for conflicting attribute values; with attribute conflicts the updating resource's value is picked. "Both, kept as two attributes…": exported attribute collections must contain only unique keys.

Sources: OpenTelemetry spec: Resource SDK, OpenTelemetry spec: Common concepts (attribute limits)

Practice this topic

Focus mode keeps every question on Configuring the SDK with Environment Variables and Files until you leave it. 10 free guest answers, then a free account; no credit card.

Or open OTCA practice across all topics.

Sources

The explanation above was checked against these official documents.

Frequently asked questions

Why does OTEL_SDK_DISABLED=1 not disable the SDK?

Boolean variables are true only for the case-insensitive string true. Any other value, including 1, is read as false, and the SDK should log a warning.

What is the default OTLP protocol?

The specification says it should be http/protobuf, but SDKs may keep grpc for backward compatibility, so the docs describe the default as SDK-dependent.