tracing_betterstack/
lib.rs

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
mod client;
mod dispatch;
mod export;
mod layer;

pub use client::{BetterstackClient, BetterstackClientTrait, BetterstackError};
pub use export::{ExportConfig, LogDestination};
pub use layer::{layer, BetterstackLayer};

#[cfg(test)]
mod tests {
    use super::*;
    use std::{env, time::Duration};
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

    fn init_env() -> Result<(String, String), Box<dyn std::error::Error>> {
        dotenv::dotenv().ok();

        let token =
            env::var("BETTERSTACK_SOURCE_TOKEN").map_err(|_| "BETTERSTACK_SOURCE_TOKEN not set")?;
        let url =
            env::var("BETTERSTACK_INGEST_URL").map_err(|_| "BETTERSTACK_INGEST_URL not set")?;

        Ok((token, url))
    }

    #[test]
    fn test_export_config() {
        let config = ExportConfig::default()
            .with_batch_size(50)
            .with_interval(Duration::from_secs(10));

        assert_eq!(config.batch_size, 50);
        assert_eq!(config.interval, Duration::from_secs(10));
    }

    #[tokio::test]
    async fn test_basic_initialization() {
        let (token, url) = match init_env() {
            Ok(env_vars) => env_vars,
            Err(e) => {
                eprintln!("Skipping test: {}", e);
                return;
            }
        };

        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::filter::filter_fn(|metadata| {
                // Only allow logs from our crate
                metadata.target().starts_with("tracing_betterstack")
            }))
            .with(
                layer()
                    .with_client(
                        token,
                        url,
                        ExportConfig::default()
                            .with_batch_size(10)
                            .with_interval(Duration::from_millis(100)),
                    )
                    .with_code_location(true)
                    .with_target(true),
            );

        let _guard = subscriber.set_default();
        tracing::info!(target: "tracing_betterstack::test", "Test log message");
        tokio::time::sleep(Duration::from_millis(200)).await;
    }

    #[tokio::test]
    async fn test_custom_formatting() {
        let (token, url) = match init_env() {
            Ok(env_vars) => env_vars,
            Err(e) => {
                eprintln!("Skipping test: {}", e);
                return;
            }
        };

        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::filter::filter_fn(|metadata| {
                // Only allow logs from our crate
                metadata.target().starts_with("tracing_betterstack")
            }))
            .with(
                layer()
                    .with_client(token, url, ExportConfig::default())
                    .with_fmt_layer(
                        tracing_subscriber::fmt::layer()
                            .json()
                            .with_current_span(true)
                            .with_span_list(true),
                    ),
            );

        let _guard = subscriber.set_default();
        let span = tracing::info_span!(
            target: "tracing_betterstack::test",
            "test_span",
            field = "value"
        );
        let _span_guard = span.enter();
        tracing::info!(target: "tracing_betterstack::test", "Test log message with custom format");
        tokio::time::sleep(Duration::from_millis(200)).await;
    }

    #[tokio::test]
    async fn test_batch_behavior() {
        let (token, url) = match init_env() {
            Ok(env_vars) => env_vars,
            Err(e) => {
                eprintln!("Skipping test: {}", e);
                return;
            }
        };

        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::filter::filter_fn(|metadata| {
                metadata.target().starts_with("tracing_betterstack")
            }))
            .with(
                layer().with_client(
                    token,
                    url,
                    ExportConfig::default()
                        .with_batch_size(2)
                        .with_interval(Duration::from_millis(500)),
                ),
            );

        let _guard = subscriber.set_default();

        tracing::info!(target: "tracing_betterstack::test", "Batch test message 1");
        tracing::info!(target: "tracing_betterstack::test", "Batch test message 2");

        tokio::time::sleep(Duration::from_millis(600)).await;
    }

    #[tokio::test]
    async fn test_interval_flush() {
        let (token, url) = match init_env() {
            Ok(env_vars) => env_vars,
            Err(e) => {
                eprintln!("Skipping test: {}", e);
                return;
            }
        };

        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::filter::filter_fn(|metadata| {
                metadata.target().starts_with("tracing_betterstack")
            }))
            .with(
                layer().with_client(
                    token,
                    url,
                    ExportConfig::default()
                        .with_batch_size(10)
                        .with_interval(Duration::from_millis(200)),
                ),
            );

        let _guard = subscriber.set_default();

        tracing::info!(target: "tracing_betterstack::test", "Interval flush test message");

        tokio::time::sleep(Duration::from_millis(300)).await;
    }
}