Lines
0 %
Functions
Branches
100 %
//! Configuration for OpenTelemetry exporter
use amplify::Getters;
use derive_deftly::Deftly;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tor_config::derive::prelude::*;
use tor_config_path::CfgPath;
/// Configuration for exporting spans with OpenTelemetry.
#[derive(Debug, Clone, Deftly, Eq, PartialEq, Serialize, Deserialize, Getters)]
#[derive_deftly(TorConfig)]
pub struct OpentelemetryConfig {
/// Write spans to a file in OTLP JSON format.
#[deftly(tor_config(default))]
file: Option<OpentelemetryFileExporterConfig>,
/// Export spans via HTTP.
http: Option<OpentelemetryHttpExporterConfig>,
}
/// Configuration for the OpenTelemetry HTTP exporter.
#[deftly(tor_config(no_default_trait))]
pub struct OpentelemetryHttpExporterConfig {
/// HTTP(S) endpoint to send spans to.
///
/// For Jaeger, this should be something like: `http://localhost:4318/v1/traces`
#[deftly(tor_config(no_default))]
endpoint: String,
/// Configuration for how to batch exports.
#[deftly(tor_config(sub_builder))]
batch: OpentelemetryBatchConfig,
// TODO: A different approach to this may be better, as getting the default in this way
// prevents the use of environment variables to override this, and also is inconsistent with
// other aspects of configuration.
/// Timeout for sending data.
#[deftly(tor_config(default = "opentelemetry_otlp::OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT"))]
timeout: Duration,
// TODO: Once opentelemetry-otlp supports more than one protocol over HTTP, add a config option
// to choose protocol here.
/// Configuration for the OpenTelemetry File exporter.
pub struct OpentelemetryFileExporterConfig {
/// The path to write the JSON file to.
path: CfgPath,
/// Configuration for how to batch writes.
/// Configuration for the Opentelemetry batch exporting.
/// This is a copy of [`opentelemetry_sdk::trace::BatchConfig`].
pub struct OpentelemetryBatchConfig {
/// Maximum queue size. See [`opentelemetry_sdk::trace::BatchConfig::max_queue_size`].
max_queue_size: Option<usize>,
/// Maximum export batch size. See [`opentelemetry_sdk::trace::BatchConfig::max_export_batch_size`].
max_export_batch_size: Option<usize>,
/// Scheduled delay. See [`opentelemetry_sdk::trace::BatchConfig::scheduled_delay`].
#[deftly(tor_config(default = "Duration::from_secs(5)"))]
scheduled_delay: Duration,
impl From<OpentelemetryBatchConfig> for opentelemetry_sdk::trace::BatchConfig {
fn from(config: OpentelemetryBatchConfig) -> opentelemetry_sdk::trace::BatchConfig {
let batch_config = opentelemetry_sdk::trace::BatchConfigBuilder::default();
let batch_config = if let Some(max_queue_size) = config.max_queue_size {
batch_config.with_max_queue_size(max_queue_size)
} else {
batch_config
};
let batch_config = if let Some(max_export_batch_size) = config.max_export_batch_size {
batch_config.with_max_export_batch_size(max_export_batch_size)
let batch_config = batch_config.with_scheduled_delay(config.scheduled_delay);
batch_config.build()