lambda_otel_utils/
protocol.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use opentelemetry_otlp::Protocol;
use std::env;

/// Determines the OpenTelemetry protocol to use based on the OTEL_EXPORTER_OTLP_PROTOCOL environment variable.
///
/// # Returns
///
/// A `Protocol` enum value indicating which protocol format to use:
/// - `Protocol::HttpBinary` if OTEL_EXPORTER_OTLP_PROTOCOL is set to "http/protobuf"
/// - `Protocol::HttpJson` if OTEL_EXPORTER_OTLP_PROTOCOL is set to "http/json" or empty/unset
///
/// # Environment Variables
///
/// - `OTEL_EXPORTER_OTLP_PROTOCOL`: The protocol format to use. Supported values are:
///   - "http/protobuf" - Use Protocol Buffers over HTTP
///   - "http/json" - Use JSON over HTTP (default)
///
/// If an unsupported protocol value is provided, defaults to HTTP JSON with a warning message.
pub fn get_protocol() -> Protocol {
    match env::var("OTEL_EXPORTER_OTLP_PROTOCOL")
        .unwrap_or_default()
        .to_lowercase()
        .as_str()
    {
        "http/protobuf" => Protocol::HttpBinary,
        "http/json" | "" => Protocol::HttpJson,
        unsupported => {
            eprintln!(
                "Warning: OTEL_EXPORTER_OTLP_PROTOCOL value '{}' is not supported. Defaulting to HTTP JSON.",
                unsupported
            );
            Protocol::HttpJson
        }
    }
}