Skip to content

added support for otlp headers #3201

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
95 changes: 91 additions & 4 deletions crates/telemetry/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::env::VarError;
use std::{collections::HashMap, env::VarError};

use opentelemetry_otlp::{
OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_HEADERS, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_HEADERS, OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_HEADERS,
};

const OTEL_SDK_DISABLED: &str = "OTEL_SDK_DISABLED";
Expand Down Expand Up @@ -76,6 +77,92 @@ pub(crate) fn otel_sdk_disabled() -> bool {
std::env::var_os(OTEL_SDK_DISABLED).is_some_and(|val| !val.is_empty())
}

pub(crate) struct OtelExporterOtlpHeaders;

impl OtelExporterOtlpHeaders {
/// Returns a list of key value pairs (when provided) for:
/// - `OTEL_EXPORTER_OTLP_HEADERS`
/// - `OTEL_EXPORTER_OTLP_TRACES_HEADERS`,
/// - `OTEL_EXPORTER_OTLP_METRICS_HEADERS`,
/// - `OTEL_EXPORTER_OTLP_LOGS_HEADERS`
pub(crate) fn headers() -> HashMap<String, String> {
let mut all_headers: HashMap<String, String> = HashMap::new();

if let Some(otel_exporter_otlp_headers) = Self::otel_exporter_otlp_headers() {
all_headers.extend(otel_exporter_otlp_headers);
}

if let Some(otel_exporter_otlp_log_headers) = Self::otel_exporter_otlp_log_headers() {
all_headers.extend(otel_exporter_otlp_log_headers);
}

if let Some(otel_exporter_otlp_metrics_headers) = Self::otel_exporter_otlp_metrics_headers()
{
all_headers.extend(otel_exporter_otlp_metrics_headers);
}

if let Some(otel_exporter_otlp_traces_headers) = Self::otel_exporter_otlp_traces_headers() {
all_headers.extend(otel_exporter_otlp_traces_headers);
}

all_headers
}

/// Returns a boolean indicating if the OTEL headers layer should be enabled.
///
/// It is considered enabled if `OTEL_EXPORTER_OTLP_HEADERS` is set and not empty.
///
/// Note that this is overridden if OTEL_SDK_DISABLED is set and not empty.
pub(crate) fn otel_exporter_otlp_headers() -> Option<HashMap<String, String>> {
Self::var_get(OTEL_EXPORTER_OTLP_HEADERS)
}

/// Returns a boolean indicating if the OTEL log headers layer should be enabled.
///
/// It is considered enabled if `OTEL_EXPORTER_OTLP_LOGS_HEADERS` is set and not empty.
///
/// Note that this is overridden if OTEL_SDK_DISABLED is set and not empty.
/// Also Note this is only supported for HTTP.
pub(crate) fn otel_exporter_otlp_log_headers() -> Option<HashMap<String, String>> {
Self::var_get(OTEL_EXPORTER_OTLP_LOGS_HEADERS)
}

/// Returns a boolean indicating if the OTEL metrics headers layer should be enabled.
///
/// It is considered enabled if `OTEL_EXPORTER_OTLP_METRICS_HEADERS` is set and not empty.
///
/// Note that this is overridden if OTEL_SDK_DISABLED is set and not empty.
/// Also Note this is only supported for HTTP.
pub(crate) fn otel_exporter_otlp_metrics_headers() -> Option<HashMap<String, String>> {
Self::var_get(OTEL_EXPORTER_OTLP_METRICS_HEADERS)
}

/// Returns a boolean indicating if the OTEL traces headers layer should be enabled.
///
/// It is considered enabled if `OTEL_EXPORTER_OTLP_TRACES_HEADERS` is set and not empty.
///
/// Note that this is overridden if OTEL_SDK_DISABLED is set and not empty.
/// Also Note this is only supported for HTTP.
pub(crate) fn otel_exporter_otlp_traces_headers() -> Option<HashMap<String, String>> {
Self::var_get(OTEL_EXPORTER_OTLP_TRACES_HEADERS)
}

fn var_get(var_name: &str) -> Option<HashMap<String, String>> {
match std::env::var(var_name) {
Ok(var_value) if !var_value.trim().is_empty() && !otel_sdk_disabled() => {
let mut key_pair_header = HashMap::new();
for pair in var_value.split(',') {
if let Some((k, v)) = pair.split_once('=') {
key_pair_header.insert(k.trim().to_owned(), v.trim().to_owned());
}
}
Some(key_pair_header)
}
_ => None,
}
}
}

/// The protocol to use for OTLP exporter.
#[derive(Debug)]
pub(crate) enum OtlpProtocol {
Expand Down
6 changes: 6 additions & 0 deletions crates/telemetry/src/propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ use opentelemetry::{
};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use crate::env::OtelExporterOtlpHeaders;

/// Injects the current W3C TraceContext into the provided request.
pub fn inject_trace_context<'a>(req: impl Into<HeaderInjector<'a>>) {
let mut injector = req.into();
for (key, value) in OtelExporterOtlpHeaders::headers() {
injector.set(&key, value);
}

global::get_text_map_propagator(|propagator| {
let context = tracing::Span::current().context();
propagator.inject_context(&context, &mut injector);
Expand Down