-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathlib.rs
More file actions
126 lines (112 loc) · 3.93 KB
/
lib.rs
File metadata and controls
126 lines (112 loc) · 3.93 KB
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
use actix_http::header::{HeaderName, HeaderValue};
use awc::ClientRequest;
use reqwest::RequestBuilder;
use sentry::{ClientInitGuard, TransactionContext};
use std::borrow::Cow;
use std::env;
/// Initializes Sentry if `FELDERA_SENTRY_ENABLED` is set, tagging the service and release.
///
/// Use dsn as the default DSN, which can be overridden by the `SENTRY_DSN` environment variable.
pub fn init(dsn: &str, service_name: &str, release: &str) -> Option<ClientInitGuard> {
env::var("FELDERA_SENTRY_ENABLED")
.ok()
.filter(|v| v == "1")?;
const DEFAULT_SAMPLE_RATE: f32 = 1.0;
const DEFAULT_TRACE_SAMPLE_RATE: f32 = 0.1;
const MAX_BREADCRUMBS: usize = 10;
let dsn = env::var("SENTRY_DSN").ok().unwrap_or(dsn.to_string());
let sample_rate = env::var("SENTRY_SAMPLE_RATE")
.ok()
.and_then(|rate| rate.parse::<f32>().ok())
.unwrap_or(DEFAULT_SAMPLE_RATE);
let traces_sample_rate = env::var("SENTRY_TRACES_SAMPLE_RATE")
.ok()
.and_then(|rate| rate.parse::<f32>().ok())
.unwrap_or(DEFAULT_TRACE_SAMPLE_RATE);
let environment = env::var("SENTRY_ENVIRONMENT")
.unwrap_or_else(|_| String::from("dev"))
.into();
let accept_invalid_certs = environment == "ci" || environment == "dev";
let max_breadcrumbs = env::var("SENTRY_MAX_BREADCRUMBS")
.ok()
.and_then(|rate| rate.parse::<usize>().ok())
.unwrap_or(MAX_BREADCRUMBS);
let guard = sentry::init((
dsn,
sentry::ClientOptions {
environment: Some(environment),
release: Some(Cow::Owned(release.to_string())),
max_breadcrumbs,
sample_rate,
traces_sample_rate,
enable_logs: true,
attach_stacktrace: true,
accept_invalid_certs,
..Default::default()
},
));
sentry::configure_scope(|scope| scope.set_tag("service", service_name));
Some(guard)
}
fn sentry_enabled() -> bool {
sentry::Hub::current().client().is_some()
}
/// Returns an Actix middleware that captures errors and traces when Sentry is enabled.
pub fn actix_middleware() -> sentry::integrations::actix::Sentry {
sentry::integrations::actix::Sentry::builder()
.emit_header(true)
.capture_server_errors(true)
.start_transaction(true)
.finish()
}
pub mod fips;
pub mod json_logging;
fn trace_header_value() -> Option<String> {
if !sentry_enabled() {
return None;
}
let mut header = None;
sentry::configure_scope(|scope| {
if let Some(span) = scope.get_span() {
header = span.iter_headers().next().map(|(_, value)| value);
}
});
if header.is_none() {
let transaction =
sentry::start_transaction(TransactionContext::new("http.client", "http.client"));
header = transaction.iter_headers().next().map(|(_, value)| value);
transaction.finish();
}
header
}
/// Adds Sentry trace headers to outgoing awc requests.
pub trait AwcRequestTracingExt {
fn with_sentry_tracing(self) -> Self;
}
impl AwcRequestTracingExt for ClientRequest {
fn with_sentry_tracing(mut self) -> Self {
if let Some(value) = trace_header_value()
&& let Ok(header_value) = HeaderValue::from_str(&value)
{
self = self.insert_header((HeaderName::from_static("sentry-trace"), header_value));
}
self
}
}
/// Adds Sentry trace headers to outgoing reqwest requests.
pub trait ReqwestTracingExt {
fn with_sentry_tracing(self) -> Self;
}
impl ReqwestTracingExt for RequestBuilder {
fn with_sentry_tracing(self) -> Self {
if let Some(value) = trace_header_value()
&& let Ok(header_value) = reqwest::header::HeaderValue::from_str(&value)
{
return self.header(
reqwest::header::HeaderName::from_static("sentry-trace"),
header_value,
);
}
self
}
}