Skip to content
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

feat(unstable): support https otlp endpoints #27743

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ext/telemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ path = "lib.rs"
async-trait.workspace = true
deno_core.workspace = true
deno_error.workspace = true
deno_tls.workspace = true
http-body-util.workspace = true
hyper.workspace = true
hyper-rustls.workspace = true
hyper-util.workspace = true
log.workspace = true
once_cell.workspace = true
Expand Down
46 changes: 40 additions & 6 deletions ext/telemetry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,17 @@ mod hyper_client {
use std::task::Poll;
use std::task::{self};

use deno_tls::create_client_config;
use deno_tls::load_certs;
use deno_tls::load_private_keys;
use deno_tls::SocketUse;
use deno_tls::TlsKey;
use deno_tls::TlsKeys;
use http_body_util::BodyExt;
use http_body_util::Full;
use hyper::body::Body as HttpBody;
use hyper::body::Frame;
use hyper_rustls::HttpsConnector;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::client::legacy::Client;
use opentelemetry_http::Bytes;
Expand All @@ -492,14 +499,41 @@ mod hyper_client {
// same as opentelemetry_http::HyperClient except it uses OtelSharedRuntime
#[derive(Debug, Clone)]
pub struct HyperClient {
inner: Client<HttpConnector, Body>,
inner: Client<HttpsConnector<HttpConnector>, Body>,
}

impl HyperClient {
pub fn new() -> Self {
Self {
inner: Client::builder(OtelSharedRuntime).build(HttpConnector::new()),
}
pub fn new() -> deno_core::anyhow::Result<Self> {
let ca_certs = match std::env::var("OTEL_EXPORTER_OTLP_CERTIFICATE") {
Ok(path) => vec![std::fs::read(path)?],
_ => vec![],
};

let keys = match (
std::env::var("OTEL_EXPORTER_OTLP_CLIENT_KEY"),
std::env::var("OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE"),
) {
(Ok(key_path), Ok(cert_path)) => {
let key = std::fs::read(key_path)?;
let cert = std::fs::read(cert_path)?;

let certs = load_certs(&mut std::io::Cursor::new(cert))?;
let key = load_private_keys(&key)?.into_iter().next().unwrap();

TlsKeys::Static(TlsKey(certs, key))
}
_ => TlsKeys::Null,
};

let tls_config =
create_client_config(None, ca_certs, None, keys, SocketUse::Http)?;
let mut http_connector = HttpConnector::new();
http_connector.enforce_http(false);
let connector = HttpsConnector::from((http_connector, tls_config));

Ok(Self {
inner: Client::builder(OtelSharedRuntime).build(connector),
})
}
}

Expand Down Expand Up @@ -626,7 +660,7 @@ pub fn init(
// `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. Additional headers can
// be specified using `OTEL_EXPORTER_OTLP_HEADERS`.

let client = hyper_client::HyperClient::new();
let client = hyper_client::HyperClient::new()?;

let span_exporter = HttpExporterBuilder::default()
.with_http_client(client.clone())
Expand Down
5 changes: 4 additions & 1 deletion tests/specs/cli/otel_basic/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const data = {

const server = Deno.serve(
{
key: Deno.readTextFileSync("../../../testdata/tls/localhost.key"),
cert: Deno.readTextFileSync("../../../testdata/tls/localhost.crt"),
port: 0,
onListen({ port }) {
const command = new Deno.Command(Deno.execPath(), {
Expand All @@ -16,7 +18,8 @@ const server = Deno.serve(
OTEL_DENO: "true",
DENO_UNSTABLE_OTEL_DETERMINISTIC: "1",
OTEL_EXPORTER_OTLP_PROTOCOL: "http/json",
OTEL_EXPORTER_OTLP_ENDPOINT: `http://localhost:${port}`,
OTEL_EXPORTER_OTLP_ENDPOINT: `https://localhost:${port}`,
OTEL_EXPORTER_OTLP_CERTIFICATE: "../../../testdata/tls/RootCA.crt",
},
stdout: "null",
});
Expand Down
Loading