|
| 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use libdd_common::tag::Tag; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use std::fmt::Debug; |
| 7 | + |
| 8 | +/// The `DogStatsDActionOwned` enum gathers the metric types that can be sent to the DogStatsD |
| 9 | +/// server. This type takes ownership of the relevant data to support the sidecar better. |
| 10 | +/// For documentation on the dogstatsd metric types: https://docs.datadoghq.com/metrics/types/?tab=count#metric-types |
| 11 | +/// |
| 12 | +/// Originally I attempted to combine this type with `DogStatsDAction` but this GREATLY complicates |
| 13 | +/// the types to the point of insanity. I was unable to come up with a satisfactory approach that |
| 14 | +/// allows both the data-pipeline and sidecar crates to use the same type. If a future rustacean |
| 15 | +/// wants to take a stab and open a PR please do so! |
| 16 | +#[derive(Debug, Serialize, Deserialize)] |
| 17 | +pub enum DogStatsDActionOwned { |
| 18 | + #[allow(missing_docs)] |
| 19 | + Count(String, i64, Vec<Tag>), |
| 20 | + #[allow(missing_docs)] |
| 21 | + Distribution(String, f64, Vec<Tag>), |
| 22 | + #[allow(missing_docs)] |
| 23 | + Gauge(String, f64, Vec<Tag>), |
| 24 | + #[allow(missing_docs)] |
| 25 | + Histogram(String, f64, Vec<Tag>), |
| 26 | + /// Cadence only support i64 type as value |
| 27 | + /// but Golang implementation uses string (https://github.com/DataDog/datadog-go/blob/331d24832f7eac97b091efd696278fe2c4192b29/statsd/statsd.go#L230) |
| 28 | + /// and PHP implementation uses float or string (https://github.com/DataDog/php-datadogstatsd/blob/0efdd1c38f6d3dd407efbb899ad1fd2e5cd18085/src/DogStatsd.php#L251) |
| 29 | + Set(String, i64, Vec<Tag>), |
| 30 | +} |
| 31 | + |
| 32 | +/// The `DogStatsDAction` enum gathers the metric types that can be sent to the DogStatsD server. |
| 33 | +#[derive(Debug, Serialize, Deserialize)] |
| 34 | +pub enum DogStatsDAction<'a, T: AsRef<str>, V: IntoIterator<Item = &'a Tag>> { |
| 35 | + // TODO: instead of AsRef<str> we can accept a marker Trait that users of this crate implement |
| 36 | + #[allow(missing_docs)] |
| 37 | + Count(T, i64, V), |
| 38 | + #[allow(missing_docs)] |
| 39 | + Distribution(T, f64, V), |
| 40 | + #[allow(missing_docs)] |
| 41 | + Gauge(T, f64, V), |
| 42 | + #[allow(missing_docs)] |
| 43 | + Histogram(T, f64, V), |
| 44 | + /// Cadence only support i64 type as value |
| 45 | + /// but Golang implementation uses string (https://github.com/DataDog/datadog-go/blob/331d24832f7eac97b091efd696278fe2c4192b29/statsd/statsd.go#L230) |
| 46 | + /// and PHP implementation uses float or string (https://github.com/DataDog/php-datadogstatsd/blob/0efdd1c38f6d3dd407efbb899ad1fd2e5cd18085/src/DogStatsd.php#L251) |
| 47 | + Set(T, i64, V), |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use super::*; |
| 53 | + #[test] |
| 54 | + fn test_owned_sync() { |
| 55 | + // This test ensures that if a new variant is added to either `DogStatsDActionOwned` or |
| 56 | + // `DogStatsDAction` this test will NOT COMPILE to act as a reminder that BOTH locations |
| 57 | + // must be updated. |
| 58 | + let owned_act = DogStatsDActionOwned::Count("test".to_string(), 1, vec![]); |
| 59 | + match owned_act { |
| 60 | + DogStatsDActionOwned::Count(_, _, _) => {} |
| 61 | + DogStatsDActionOwned::Distribution(_, _, _) => {} |
| 62 | + DogStatsDActionOwned::Gauge(_, _, _) => {} |
| 63 | + DogStatsDActionOwned::Histogram(_, _, _) => {} |
| 64 | + DogStatsDActionOwned::Set(_, _, _) => {} |
| 65 | + } |
| 66 | + |
| 67 | + let act = DogStatsDAction::Count("test".to_string(), 1, vec![]); |
| 68 | + match act { |
| 69 | + DogStatsDAction::Count(_, _, _) => {} |
| 70 | + DogStatsDAction::Distribution(_, _, _) => {} |
| 71 | + DogStatsDAction::Gauge(_, _, _) => {} |
| 72 | + DogStatsDAction::Histogram(_, _, _) => {} |
| 73 | + DogStatsDAction::Set(_, _, _) => {} |
| 74 | + } |
| 75 | + // TODO: when std::mem::variant_count is in stable we can do this instead |
| 76 | + // assert_eq!( |
| 77 | + // std::mem::variant_count::<DogStatsDActionOwned>(), |
| 78 | + // std::mem::variant_count::<DogStatsDAction<String, Vec<&Tag>>>(), |
| 79 | + // "DogStatsDActionOwned and DogStatsDAction should have the same number of variants, |
| 80 | + // did you forget to update one?", ); |
| 81 | + } |
| 82 | +} |
0 commit comments