-
Notifications
You must be signed in to change notification settings - Fork 8
/
futures.rs
229 lines (214 loc) · 6.31 KB
/
futures.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::{future::Future, pin::Pin, task::Context, task::Poll};
use pin_project::pin_project;
use crate::{segment, transaction::Transaction};
/// A trait to make a lifetime scoped reference to a `Transaction` optional
///
/// Example:
///
/// ```rust
/// # use newrelic::Error;
/// # async fn run() -> Result<(), Error> {
/// use newrelic::{App, ExternalParamsBuilder, Segmented, Transaction};
///
/// let license_key = std::env::var("NEW_RELIC_LICENSE_KEY").unwrap();
///
/// let app = App::new("my app", &license_key).expect("Could not create app");
///
/// let transaction = app
/// .web_transaction("Transaction name")
/// .expect("Could not start transaction");
///
/// let possibly_a_transaction: Option<&Transaction> = Some(&transaction);
///
/// let not_a_transaction: Option<&Transaction> = None;
///
/// async { }
/// .custom_segment(&transaction, "Segment name", "Segment category")
/// .await;
///
/// async { }
/// .custom_segment(&possibly_a_transaction, "Segment name", "Segment category")
/// .await;
///
/// async { }
/// .custom_segment(¬_a_transaction, "Segment name", "Segment category")
/// .await;
///
/// # Ok(())
/// # }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub trait OptionalTransaction<'a> {
/// Return an optional transaction
fn get_transaction(&'a self) -> Option<&'a Transaction>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
impl<'a> OptionalTransaction<'a> for Transaction {
fn get_transaction(&'a self) -> Option<&'a Transaction> {
Some(self)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
impl<'a> OptionalTransaction<'a> for Option<&'a Transaction> {
fn get_transaction(&'a self) -> Option<&'a Transaction> {
*self
}
}
/// Extension trait allowing a `Future` to be instrumented inside a `Segment`
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub trait Segmented: Sized {
/// Instruments this future inside a custom `Segment`
///
/// Example:
///
/// ```rust
/// # use newrelic::Error;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Error> {
/// use newrelic::{App, Segmented};
///
/// let license_key = std::env::var("NEW_RELIC_LICENSE_KEY").unwrap();
///
/// let app = App::new("my app", &license_key).expect("Could not create app");
///
/// let transaction = app
/// .web_transaction("Transaction name")
/// .expect("Could not start transaction");
///
/// async { }
/// .custom_segment(&transaction, "Segment name", "Segment category")
/// .await;
///
/// # Ok(())
/// # }
/// ```
fn custom_segment<'a, T>(
self,
to_trans: &'a T,
name: &str,
category: &str,
) -> SegmentedFuture<'a, Self>
where
T: OptionalTransaction<'a>,
{
SegmentedFuture {
inner: self,
segment: to_trans
.get_transaction()
.map(|transaction| segment::Segment::custom(transaction, name, category)),
}
}
/// Instruments this future inside a datastore `Segment`
///
/// Example:
///
/// ```rust
/// # use newrelic::Error;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Error> {
/// use newrelic::{App, Datastore, DatastoreParamsBuilder, Segmented};
///
/// let license_key = std::env::var("NEW_RELIC_LICENSE_KEY").unwrap();
///
/// let app = App::new("my app", &license_key).expect("Could not create app");
///
/// let transaction = app
/// .web_transaction("Transaction name")
/// .expect("Could not start transaction");
///
/// async { }
/// .datastore_segment(
/// &transaction,
/// &DatastoreParamsBuilder::new(Datastore::Postgres)
/// .collection("people")
/// .operation("select")
/// .build()?
/// )
/// .await;
///
/// # Ok(())
/// # }
/// ```
fn datastore_segment<'a, T>(
self,
to_trans: &'a T,
params: &segment::DatastoreParams,
) -> SegmentedFuture<'a, Self>
where
T: OptionalTransaction<'a>,
{
SegmentedFuture {
inner: self,
segment: to_trans
.get_transaction()
.map(|transaction| segment::Segment::datastore(transaction, params)),
}
}
/// Instruments this future inside an external `Segment`
///
/// Example:
///
/// ```rust
/// # use newrelic::Error;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Error> {
/// use newrelic::{App, ExternalParamsBuilder, Segmented};
///
/// let license_key = std::env::var("NEW_RELIC_LICENSE_KEY").unwrap();
///
/// let app = App::new("my app", &license_key).expect("Could not create app");
///
/// let transaction = app
/// .web_transaction("Transaction name")
/// .expect("Could not start transaction");
///
/// async { }
/// .external_segment(
/// &transaction,
/// &ExternalParamsBuilder::new("https://www.rust-lang.org/")
/// .procedure("GET")
/// .library("reqwest")
/// .build()?
/// )
/// .await;
///
/// # Ok(())
/// # }
/// ```
fn external_segment<'a, T>(
self,
to_trans: &'a T,
params: &segment::ExternalParams,
) -> SegmentedFuture<'a, Self>
where
T: OptionalTransaction<'a>,
{
SegmentedFuture {
inner: self,
segment: to_trans
.get_transaction()
.map(|transaction| segment::Segment::external(transaction, params)),
}
}
}
impl<T: Sized> Segmented for T {}
/// A future that has been instrumented inside a `Segment`
#[pin_project]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub struct SegmentedFuture<'a, T> {
#[pin]
inner: T,
segment: Option<segment::Segment<'a>>,
}
impl<'a, T: Future> Future for SegmentedFuture<'a, T> {
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let result = this.inner.poll(cx);
if result.is_ready() {
// Drop the segment
*this.segment = None;
}
result
}
}