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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use std::{ffi::CString, time::Duration};

use log::{debug, error};
use newrelic_sys as ffi;

use crate::{
    app::App,
    error::{Error, Result},
    event::CustomEvent,
    segment::{DatastoreParams, ExternalParams, Segment},
};

/// A type of transaction monitored by New Relic.
pub enum TransactionType {
    /// A web transaction.
    Web,
    /// A non-web transaction.
    NonWeb,
}

/// An attribute to add to a transaction.
#[derive(Debug, From)]
pub enum Attribute<'a> {
    /// A short (i32) integer attribute.
    Int(i32),
    /// A long (i64) integer attribute.
    Long(i64),
    /// A float (f64) attribute.
    Float(f64),
    /// A string attribute.
    String(&'a str),
    /// An owned string attribute.
    OwnedString(&'a String),
}

#[derive(PartialEq)]
enum State {
    Running,
    Ended,
}

/// A transaction monitored by New Relic.
pub struct Transaction {
    pub(crate) inner: *mut ffi::newrelic_txn_t,
    _type: TransactionType,
    state: State,
}

impl Transaction {
    pub(crate) fn web(app: &App, name: &str) -> Result<Self> {
        let name = CString::new(name)?;
        let inner = unsafe { ffi::newrelic_start_web_transaction(app.inner, name.as_ptr()) };
        if inner.is_null() {
            error!("Could not start web transaction");
            Err(Error::TransactionStartError)
        } else {
            debug!("Started web transaction");
            Ok(Transaction {
                inner,
                _type: TransactionType::Web,
                state: State::Running,
            })
        }
    }

    pub(crate) fn non_web(app: &App, name: &str) -> Result<Self> {
        let name = CString::new(name)?;
        let inner = unsafe { ffi::newrelic_start_non_web_transaction(app.inner, name.as_ptr()) };
        if inner.is_null() {
            error!("Could not start non-web transaction");
            Err(Error::TransactionStartError)
        } else {
            debug!("Started non-web transaction");
            Ok(Transaction {
                inner,
                _type: TransactionType::NonWeb,
                state: State::Running,
            })
        }
    }

    /// Get the type of the transaction.
    pub fn r#type(&self) -> &TransactionType {
        &self._type
    }

    /// Add an attribute to the transaction.
    ///
    /// Returns an error if the New Relic SDK returns an error.
    pub fn add_attribute<'a, T>(&self, name: &str, attribute: T) -> Result<()>
    where
        T: Into<Attribute<'a>>,
    {
        let name = CString::new(name)?;
        let ok = match attribute.into() {
            Attribute::Int(i) => unsafe {
                ffi::newrelic_add_attribute_int(self.inner, name.as_ptr(), i)
            },
            Attribute::Float(f) => unsafe {
                ffi::newrelic_add_attribute_double(self.inner, name.as_ptr(), f)
            },
            Attribute::Long(l) => unsafe {
                ffi::newrelic_add_attribute_long(self.inner, name.as_ptr(), l)
            },
            Attribute::String(s) => {
                let s = CString::new(s)?;
                unsafe { ffi::newrelic_add_attribute_string(self.inner, name.as_ptr(), s.as_ptr()) }
            }
            Attribute::OwnedString(s) => {
                let s = CString::new(s.as_str())?;
                unsafe { ffi::newrelic_add_attribute_string(self.inner, name.as_ptr(), s.as_ptr()) }
            }
        };
        if ok {
            Ok(())
        } else {
            Err(Error::AttributeError)
        }
    }

    /// Create a custom segment within this transaction.
    ///
    /// Example:
    ///
    /// ```rust
    /// use std::{thread, time::Duration};
    ///
    /// use newrelic::App;
    ///
    /// # if false {
    /// let app = App::new("Test app", "Test license key")
    ///     .expect("Could not create app");
    /// let transaction = app
    ///     .web_transaction("Test transaction")
    ///     .expect("Could not start transaction");
    /// transaction.custom_segment("Test segment", "Test category", |_| {
    ///     thread::sleep(Duration::from_secs(1))
    /// });
    /// # }
    /// ```
    pub fn custom_segment<F, V>(&self, name: &str, category: &str, func: F) -> V
    where
        F: FnOnce(Segment) -> V,
    {
        let segment = Segment::custom(self, name, category);
        func(segment)
    }

    /// Create a datastore segment within this transaction.
    ///
    /// Example:
    ///
    /// ```rust
    /// use std::{thread, time::Duration};
    ///
    /// use newrelic::{App, Datastore, DatastoreParamsBuilder};
    ///
    /// # if false {
    /// let app = App::new("Test app", "Test license key")
    ///     .expect("Could not create app");
    /// let transaction = app
    ///     .web_transaction("Test transaction")
    ///     .expect("Could not start transaction");
    /// let segment_params = DatastoreParamsBuilder::new(Datastore::Postgres)
    ///     .collection("people")
    ///     .operation("select")
    ///     .build()
    ///     .expect("Invalid datastore segment parameters");
    /// transaction.datastore_segment(&segment_params, |_| {
    ///     thread::sleep(Duration::from_secs(1))
    /// });
    /// # }
    /// ```
    pub fn datastore_segment<F, V>(&self, params: &DatastoreParams, func: F) -> V
    where
        F: FnOnce(Segment) -> V,
    {
        let segment = Segment::datastore(self, params);
        func(segment)
    }

    /// Create an external segment within this transaction.
    ///
    /// Example:
    ///
    /// ```rust
    /// use std::{thread, time::Duration};
    ///
    /// use newrelic::{App, ExternalParamsBuilder};
    ///
    /// # if false {
    /// let app = App::new("Test app", "Test license key")
    ///     .expect("Could not create app");
    /// let transaction = app
    ///     .web_transaction("Test transaction")
    ///     .expect("Could not start transaction");
    /// let segment_params = ExternalParamsBuilder::new("https://www.rust-lang.org/")
    ///     .procedure("GET")
    ///     .library("reqwest")
    ///     .build()
    ///     .expect("Invalid external segment parameters");
    /// transaction.external_segment(&segment_params, |_| {
    ///     thread::sleep(Duration::from_secs(1))
    /// });
    /// # }
    /// ```
    pub fn external_segment<F, V>(&self, params: &ExternalParams, func: F) -> V
    where
        F: FnOnce(Segment) -> V,
    {
        let segment = Segment::external(self, params);
        func(segment)
    }

    /// Record an error in this transaction.
    ///
    /// `priority` is an arbitrary integer indicating the error priority.
    /// `message` is the error message; `class` is the error class or type.
    pub fn notice_error(&self, priority: i32, message: &str, class: &str) -> Result<()> {
        let message = CString::new(message)?;
        let class = CString::new(class)?;
        unsafe {
            ffi::newrelic_notice_error(self.inner, priority, message.as_ptr(), class.as_ptr());
        }
        Ok(())
    }

    /// Ignore this transaction.
    ///
    /// Data for this transaction will not be sent to New Relic.
    pub fn ignore(&self) -> Result<()> {
        let ok = unsafe { ffi::newrelic_ignore_transaction(self.inner) };
        if ok {
            Ok(())
        } else {
            Err(Error::IgnoreError)
        }
    }

    /// Record a custom metric for this transaction.
    ///
    /// The metric will be named according to `metric_name` and will
    /// record for `duration`.
    pub fn record_custom_metric(&self, metric_name: &str, duration: Duration) -> Result<()> {
        let metric_name = CString::new(metric_name)?;
        let ok = unsafe {
            ffi::newrelic_record_custom_metric(
                self.inner,
                metric_name.as_ptr(),
                duration.subsec_millis() as f64,
            )
        };
        if ok {
            Ok(())
        } else {
            Err(Error::CustomMetricError)
        }
    }

    /// Create a custom event attached to this transaction.
    ///
    /// Example:
    ///
    /// ```rust
    /// use std::{thread, time::Duration};
    ///
    /// use newrelic::App;
    ///
    /// # if false {
    /// let app = App::new("Test app", "Test license key")
    ///     .expect("Could not create app");
    /// let transaction = app
    ///     .web_transaction("Test transaction")
    ///     .expect("Could not start transaction");
    /// let custom_event = transaction.custom_event("My event")
    ///     .expect("Could not create custom event");
    /// custom_event.add_attribute("number of foos", 1_000);
    /// custom_event.record();
    /// # }
    /// ```
    pub fn custom_event(&self, event_type: &str) -> Result<CustomEvent> {
        CustomEvent::new(self, event_type)
    }

    /// Explicitly end this transaction.
    ///
    /// If this is not called, the transaction is automatically ended
    /// when dropped.
    pub fn end(&mut self) {
        //println!("Ending transaction from library");
        if let State::Running = self.state {
            unsafe {
                ffi::newrelic_end_transaction(&mut self.inner);
            }
            debug!("Ended transaction");
            self.state = State::Ended;
        }
    }
}

//impl Drop for Transaction {
//    fn drop(&mut self) {
//        self.end();
//    }
//}

unsafe impl Send for Transaction {}
unsafe impl Sync for Transaction {}