Skip to content

Commit

Permalink
feat: configure TLS with environment variables.
Browse files Browse the repository at this point in the history
Updates the opentelemetry-otlp crate to allow users to configure TLS
using environment variables. Removing the need to crating the TLS config
object and defining it with the `with_tls_config` method. In the same
way other OTLP libraries does (e.g. go lang).

Signed-off-by: José Guilherme Vanz <[email protected]>
  • Loading branch information
jvanz committed Dec 24, 2024
1 parent 6e1032f commit 887a6a6
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 38 deletions.
1 change: 1 addition & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## vNext

- Bump msrv to 1.75.0.
- TLS configuration via environment variables for GRPc exporters.


## 0.27.0
Expand Down
61 changes: 61 additions & 0 deletions opentelemetry-otlp/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL";
/// Compression algorithm to use, defaults to none.
pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION";

/// Certificate file to validate the OTLP server connection
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CERTIFICATE";
/// Path to the certificate file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: &str = "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE";
/// Path to the key file to use for client authentication (mTLS).
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_CLIENT_KEY: &str = "OTEL_EXPORTER_OTLP_CLIENT_KEY";
/// Use insecure connection. Disable TLS
#[cfg(feature = "tls")]
pub const OTEL_EXPORTER_OTLP_INSECURE: &str = "OTEL_EXPORTER_OTLP_INSECURE";

#[cfg(feature = "http-json")]
/// Default protocol, using http-json.
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
Expand Down Expand Up @@ -76,6 +89,18 @@ pub struct ExportConfig {

/// The timeout to the collector.
pub timeout: Duration,

/// Disable TLS
pub insecure: Option<bool>,

/// The certificate file to validate the OTLP server connection
pub certificate: Option<String>,

/// The path to the certificate file to use for client authentication (mTLS).
pub client_certificate: Option<String>,

/// The path to the key file to use for client authentication (mTLS).
pub client_key: Option<String>,
}

impl Default for ExportConfig {
Expand All @@ -88,6 +113,10 @@ impl Default for ExportConfig {
// won't know if user provided a value
protocol,
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
insecure: None,
certificate: None,
client_certificate: None,
client_key: None,
}
}
}
Expand Down Expand Up @@ -195,6 +224,17 @@ pub trait WithExportConfig {
fn with_timeout(self, timeout: Duration) -> Self;
/// Set export config. This will override all previous configuration.
fn with_export_config(self, export_config: ExportConfig) -> Self;
/// Set insecure connection. Disable TLS
fn with_insecure(self) -> Self;
/// Set the certificate file to validate the OTLP server connection
/// This is only available when the `tls` feature is enabled.
fn with_certificate<T: Into<String>>(self, certificate: T) -> Self;
/// Set the path to the certificate file to use for client authentication (mTLS).
/// This is only available when the `tls` feature is enabled.
fn with_client_certificate<T: Into<String>>(self, client_certificate: T) -> Self;
/// Set the path to the key file to use for client authentication (mTLS).
/// This is only available when the `tls` feature is enabled.
fn with_client_key<T: Into<String>>(self, client_key: T) -> Self;
}

impl<B: HasExportConfig> WithExportConfig for B {
Expand All @@ -217,6 +257,27 @@ impl<B: HasExportConfig> WithExportConfig for B {
self.export_config().endpoint = exporter_config.endpoint;
self.export_config().protocol = exporter_config.protocol;
self.export_config().timeout = exporter_config.timeout;
self.export_config().insecure = Some(true);
self
}

Check warning on line 262 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L260-L262

Added lines #L260 - L262 were not covered by tests

fn with_insecure(mut self) -> Self {
self.export_config().insecure = Some(true);
self
}

Check warning on line 267 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L264-L267

Added lines #L264 - L267 were not covered by tests

fn with_certificate<T: Into<String>>(mut self, certificate: T) -> Self {
self.export_config().certificate = Some(certificate.into());
self
}

Check warning on line 272 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L269-L272

Added lines #L269 - L272 were not covered by tests

fn with_client_certificate<T: Into<String>>(mut self, client_certificate: T) -> Self {
self.export_config().client_certificate = Some(client_certificate.into());
self
}

Check warning on line 277 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L274-L277

Added lines #L274 - L277 were not covered by tests

fn with_client_key<T: Into<String>>(mut self, client_key: T) -> Self {
self.export_config().client_key = Some(client_key.into());

Check warning on line 280 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L279-L280

Added lines #L279 - L280 were not covered by tests
self
}
}
Expand Down
Loading

0 comments on commit 887a6a6

Please sign in to comment.