lambda_otel_utils/
protocol.rs

1use opentelemetry_otlp::Protocol;
2use std::env;
3
4/// Determines the OpenTelemetry protocol to use based on the OTEL_EXPORTER_OTLP_PROTOCOL environment variable.
5///
6/// # Returns
7///
8/// A `Protocol` enum value indicating which protocol format to use:
9/// - `Protocol::HttpBinary` if OTEL_EXPORTER_OTLP_PROTOCOL is set to "http/protobuf"
10/// - `Protocol::HttpJson` if OTEL_EXPORTER_OTLP_PROTOCOL is set to "http/json" or empty/unset
11///
12/// # Environment Variables
13///
14/// - `OTEL_EXPORTER_OTLP_PROTOCOL`: The protocol format to use. Supported values are:
15///   - "http/protobuf" - Use Protocol Buffers over HTTP
16///   - "http/json" - Use JSON over HTTP (default)
17///
18/// If an unsupported protocol value is provided, defaults to HTTP JSON with a warning message.
19pub fn get_protocol() -> Protocol {
20    match env::var("OTEL_EXPORTER_OTLP_PROTOCOL")
21        .unwrap_or_default()
22        .to_lowercase()
23        .as_str()
24    {
25        "http/protobuf" => Protocol::HttpBinary,
26        "http/json" | "" => Protocol::HttpJson,
27        unsupported => {
28            eprintln!(
29                "Warning: OTEL_EXPORTER_OTLP_PROTOCOL value '{}' is not supported. Defaulting to HTTP JSON.",
30                unsupported
31            );
32            Protocol::HttpJson
33        }
34    }
35}