aboutsummaryrefslogtreecommitdiff
path: root/crates/tor-config-shared/src/opentelemetry.rs
blob: c3b2a999234a49b8987bf6fdd576bc56cff3aa41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! 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.
    #[deftly(tor_config(default))]
    http: Option<OpentelemetryHttpExporterConfig>,
}

/// Configuration for the OpenTelemetry HTTP exporter.
#[derive(Debug, Clone, Deftly, Eq, PartialEq, Serialize, Deserialize, Getters)]
#[derive_deftly(TorConfig)]
#[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.
#[derive(Debug, Clone, Deftly, Eq, PartialEq, Serialize, Deserialize, Getters)]
#[derive_deftly(TorConfig)]
#[deftly(tor_config(no_default_trait))]
pub struct OpentelemetryFileExporterConfig {
    /// The path to write the JSON file to.
    #[deftly(tor_config(no_default))]
    path: CfgPath,
    /// Configuration for how to batch writes.
    #[deftly(tor_config(sub_builder))]
    batch: OpentelemetryBatchConfig,
}

/// Configuration for the Opentelemetry batch exporting.
///
/// This is a copy of [`opentelemetry_sdk::trace::BatchConfig`].
#[derive(Debug, Clone, Deftly, Eq, PartialEq, Serialize, Deserialize, Getters)]
#[derive_deftly(TorConfig)]
pub struct OpentelemetryBatchConfig {
    /// Maximum queue size. See [`opentelemetry_sdk::trace::BatchConfig::max_queue_size`].
    #[deftly(tor_config(default))]
    max_queue_size: Option<usize>,
    /// Maximum export batch size. See [`opentelemetry_sdk::trace::BatchConfig::max_export_batch_size`].
    #[deftly(tor_config(default))]
    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)
        } else {
            batch_config
        };

        let batch_config = batch_config.with_scheduled_delay(config.scheduled_delay);

        batch_config.build()
    }
}