use std::time::{Duration, SystemTime};
use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
use aws_sigv4::http_request::{SignableBody, SignableRequest, SigningSettings, sign};
use aws_sigv4::sign::v4;
use bytes::Bytes;
use opentelemetry_http::{HttpClient, HttpError, Request, Response};
use opentelemetry_otlp::{WithExportConfig, WithHttpConfig};
use crate::{InitError, TelemetryBuilder};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AwsError {
#[error("no AWS region: set AWS_REGION or a profile region")]
NoRegion,
#[error("no AWS credentials provider")]
NoCredentials,
#[error("set up CloudWatch export from inside a tokio runtime")]
NoRuntime,
#[error("could not build the HTTP client for CloudWatch")]
HttpClient,
}
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
fn export_timeout(signal_var: &str) -> Duration {
export_timeout_from(signal_var, |var| std::env::var(var).ok())
}
fn export_timeout_from(signal_var: &str, lookup: impl Fn(&str) -> Option<String>) -> Duration {
[signal_var, "OTEL_EXPORTER_OTLP_TIMEOUT"]
.into_iter()
.find_map(|var| lookup(var)?.trim().parse::<u64>().ok().filter(|ms| *ms > 0))
.map_or(DEFAULT_TIMEOUT, Duration::from_millis)
}
#[derive(Debug, Clone)]
struct SigV4Client {
http: reqwest::Client,
credentials: SharedCredentialsProvider,
region: String,
runtime: tokio::runtime::Handle,
}
impl SigV4Client {
async fn sign_and_send(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
let (parts, body) = request.into_parts();
let service = parts
.uri
.host()
.and_then(|host| host.split('.').next())
.unwrap_or("xray")
.to_owned();
let credentials = self.credentials.provide_credentials().await?;
let identity = credentials.into();
let params = v4::SigningParams::builder()
.identity(&identity)
.region(&self.region)
.name(&service)
.time(SystemTime::now())
.settings(SigningSettings::default())
.build()?
.into();
let uri = parts.uri.to_string();
let headers: Vec<(&str, &str)> = parts
.headers
.iter()
.filter_map(|(name, value)| Some((name.as_str(), value.to_str().ok()?)))
.collect();
let signable = SignableRequest::new(
parts.method.as_str(),
&uri,
headers.into_iter(),
SignableBody::Bytes(&body),
)?;
let (instructions, _signature) = sign(signable, ¶ms)?.into_parts();
let mut signed = http::Request::from_parts(parts, body);
instructions.apply_to_request_http1x(&mut signed);
let response = self.http.execute(signed.try_into()?).await?;
let mut builder = http::Response::builder().status(response.status());
for (name, value) in response.headers() {
builder = builder.header(name, value);
}
Ok(builder.body(response.bytes().await?)?)
}
}
#[async_trait::async_trait]
impl HttpClient for SigV4Client {
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
if tokio::runtime::Handle::try_current().is_ok() {
self.sign_and_send(request).await
} else {
self.runtime.block_on(self.sign_and_send(request))
}
}
}
impl TelemetryBuilder {
pub async fn aws_cloudwatch(self) -> Result<Self, InitError> {
let runtime = tokio::runtime::Handle::try_current()
.map_err(|_| InitError::Aws(AwsError::NoRuntime))?;
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
let region = config
.region()
.ok_or(InitError::Aws(AwsError::NoRegion))?
.to_string();
let credentials = config
.credentials_provider()
.ok_or(InitError::Aws(AwsError::NoCredentials))?;
let spans_url = format!("https://xray.{region}.amazonaws.com/v1/traces");
let metrics_url = format!("https://monitoring.{region}.amazonaws.com/v1/metrics");
self.cloudwatch_exporters(
runtime,
region,
credentials,
(
spans_url,
export_timeout("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"),
),
(
metrics_url,
export_timeout("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT"),
),
)
}
fn cloudwatch_exporters(
self,
runtime: tokio::runtime::Handle,
region: String,
credentials: SharedCredentialsProvider,
spans: (String, Duration),
metrics: (String, Duration),
) -> Result<Self, InitError> {
let client = |timeout: Duration| -> Result<SigV4Client, InitError> {
let http = reqwest::Client::builder()
.connect_timeout(CONNECT_TIMEOUT)
.timeout(timeout)
.build()
.map_err(|error| {
tracing::error!(%error, "building the CloudWatch HTTP client failed");
InitError::Aws(AwsError::HttpClient)
})?;
Ok(SigV4Client {
http,
credentials: credentials.clone(),
region: region.clone(),
runtime: runtime.clone(),
})
};
let spans_exporter = opentelemetry_otlp::SpanExporter::builder()
.with_http()
.with_http_client(client(spans.1)?)
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
.with_endpoint(spans.0)
.build()
.map_err(InitError::SpanExporter)?;
let metrics_exporter = opentelemetry_otlp::MetricExporter::builder()
.with_http()
.with_http_client(client(metrics.1)?)
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
.with_endpoint(metrics.0)
.build()
.map_err(InitError::MetricExporter)?;
Ok(self
.span_exporter(spans_exporter)
.metric_exporter(metrics_exporter))
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};
use aws_credential_types::Credentials;
use aws_credential_types::provider::SharedCredentialsProvider;
use tokio::net::TcpListener;
use super::{DEFAULT_TIMEOUT, TelemetryBuilder, export_timeout_from};
fn pin_global_default() {
static PIN: std::sync::Once = std::sync::Once::new();
PIN.call_once(|| {
let _ = tracing::subscriber::set_global_default(
tracing::subscriber::NoSubscriber::default(),
);
});
}
use crate::Telemetry;
fn env(pairs: &'static [(&'static str, &'static str)]) -> impl Fn(&str) -> Option<String> {
move |var| {
pairs
.iter()
.find(|(name, _)| *name == var)
.map(|(_, value)| (*value).to_owned())
}
}
#[test]
fn the_signal_variable_beats_the_general_one() {
let lookup = env(&[
("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "1500"),
("OTEL_EXPORTER_OTLP_TIMEOUT", "3000"),
]);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", &lookup),
Duration::from_millis(1500)
);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_METRICS_TIMEOUT", &lookup),
Duration::from_millis(3000)
);
}
#[test]
fn zero_and_junk_fall_through_to_the_general_variable_then_the_default() {
let lookup = env(&[
("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "0"),
("OTEL_EXPORTER_OTLP_TIMEOUT", "0"),
]);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", &lookup),
DEFAULT_TIMEOUT
);
let lookup = env(&[
("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "0"),
("OTEL_EXPORTER_OTLP_TIMEOUT", "2000"),
]);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", &lookup),
Duration::from_millis(2000)
);
let lookup = env(&[("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", "soon")]);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", &lookup),
DEFAULT_TIMEOUT
);
assert_eq!(
export_timeout_from("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", env(&[])),
DEFAULT_TIMEOUT
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_hung_cloudwatch_endpoint_fails_the_export_within_its_timeout() {
pin_global_default();
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let accept = tokio::spawn(async move {
let mut held = Vec::new();
while let Ok((socket, _)) = listener.accept().await {
held.push(socket);
}
});
let credentials = Credentials::new("AKIDEXAMPLE", "dummy-secret", None, None, "test");
let timeout = Duration::from_millis(300);
let url = |path: &str| format!("{base}{path}");
let (telemetry, subscriber): (Telemetry, _) = TelemetryBuilder::default()
.service_name("rig-otel-test")
.cloudwatch_exporters(
tokio::runtime::Handle::current(),
"us-east-1".to_owned(),
SharedCredentialsProvider::new(credentials),
(url("/v1/traces"), timeout),
(url("/v1/metrics"), timeout),
)
.unwrap()
.logs(false)
.build()
.unwrap();
let guard = tracing::subscriber::set_default(subscriber);
tracing::info_span!("hung export").in_scope(|| ());
drop(guard);
let outer = Duration::from_secs(10);
let (telemetry, flushed, flush_took) = tokio::time::timeout(
outer,
tokio::task::spawn_blocking(move || {
let started = Instant::now();
let flushed = telemetry.force_flush();
(telemetry, flushed, started.elapsed())
}),
)
.await
.expect("flush did not return within 10 s")
.unwrap();
assert!(flushed.is_err(), "flush against a hung endpoint must fail");
assert!(
flush_took < Duration::from_secs(2),
"flush took {flush_took:?}"
);
let shutdown_took = tokio::time::timeout(
outer,
tokio::task::spawn_blocking(move || {
let started = Instant::now();
drop(telemetry);
started.elapsed()
}),
)
.await
.expect("shutdown did not return within 10 s")
.unwrap();
assert!(
shutdown_took < Duration::from_secs(2),
"shutdown took {shutdown_took:?}"
);
accept.abort();
}
}