metrics_datadog_exporter/
lib.rs

1#![warn(missing_docs)]
2
3//! Exports any metrics to DataDog
4
5use metrics::SetRecorderError;
6use std::io;
7use std::sync::Arc;
8use std::time::Duration;
9use thiserror::Error;
10use tokio::task::JoinHandle;
11
12mod builder;
13pub use crate::builder::DataDogBuilder;
14pub mod data;
15pub use crate::data::DataDogMetric;
16pub use crate::data::DataDogMetricType;
17pub use crate::data::DataDogMetricValue;
18pub use metrics;
19pub mod exporter;
20pub use crate::exporter::DataDogExporter;
21mod recorder;
22pub use crate::recorder::DataDogRecorder;
23
24/// Error handling metrics
25#[derive(Error, Debug)]
26pub enum Error {
27    /// Error when serializing metric to JSON
28    #[error("Serialization failed: `{0}`")]
29    SerializationError(#[from] serde_json::Error),
30    /// Error when interacting with DataDog API
31    #[error("API Request Failed: `{0}`")]
32    ApiError(#[from] reqwest::Error),
33    /// Error compressing or decompressing
34    #[error("IO error: `{0}`")]
35    IOError(#[from] io::Error),
36}
37
38/// [`Ok`] or [`enum@Error`]
39pub type Result<T, E = Error> = core::result::Result<T, E>;
40
41/// Handle to metrics
42pub struct DataDogHandle {
43    /// Metric recorder
44    pub recorder: DataDogRecorder,
45    /// Metric exporter
46    pub handle: DataDogExporter,
47}
48
49impl DataDogHandle {
50    /// Install [`DataDogRecorder`] and return [`DataDogExporter`]
51    pub fn install(self) -> Result<DataDogExporter, SetRecorderError> {
52        metrics::set_boxed_recorder(Box::new(self.recorder))?;
53        Ok(self.handle)
54    }
55
56    /// Flush metrics
57    pub async fn flush(&self) -> Result<()> {
58        self.handle.flush().await
59    }
60
61    /// Write metrics every [`Duration`]
62    pub fn schedule(self, interval: Duration) -> (Arc<DataDogExporter>, JoinHandle<()>) {
63        self.handle.schedule(interval)
64    }
65}