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
// Cadence - An extensible Statsd client for Rust!
//
// Copyright 2020 Nick Pillitteri
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::builder::MetricBuilder;
use crate::client::{Gauged, Histogrammed, Metered, StatsdClient, Timed};
use crate::types::{Gauge, Histogram, Meter, MetricResult, Timer};
use std::time::Duration;

/// Backwards compatibility shim for removed and deprecated methods.
///
/// To allow people time to migrate, the removed methods are implemented here.
/// These methods should be considered deprecated and not viable to use long-term
/// (this trait will be removed in a future release).
///
/// For more information about how to migrate away from the methods in this
/// trait, see the `MIGRATION.md` file in the root of the repository.
pub trait Compat {
    #[deprecated(note = "Use `client.time(key, val)`")]
    #[allow(deprecated)]
    fn time_duration(&self, key: &str, val: Duration) -> MetricResult<Timer> {
        self.time_duration_with_tags(key, val).try_send()
    }

    #[deprecated(note = "Use `client.time_with_tags(key, val)`")]
    fn time_duration_with_tags<'a>(&'a self, key: &'a str, val: Duration) -> MetricBuilder<'_, '_, Timer>;

    #[deprecated(note = "Use `client.gauge(key, val)`")]
    #[allow(deprecated)]
    fn gauge_f64(&self, key: &str, val: f64) -> MetricResult<Gauge> {
        self.gauge_f64_with_tags(key, val).try_send()
    }

    #[deprecated(note = "Use `client.gauge_with_tags(key, val)`")]
    fn gauge_f64_with_tags<'a>(&'a self, key: &'a str, val: f64) -> MetricBuilder<'_, '_, Gauge>;

    #[deprecated(note = "Use `client.meter(key, 1)`")]
    #[allow(deprecated)]
    fn mark(&self, key: &str) -> MetricResult<Meter> {
        self.mark_with_tags(key).try_send()
    }

    #[deprecated(note = "Use `client.meter_with_tags(key, 1)`")]
    fn mark_with_tags<'a>(&'a self, key: &'a str) -> MetricBuilder<'_, '_, Meter>;

    #[deprecated(note = "Use `client.histogram(key, val)`")]
    #[allow(deprecated)]
    fn histogram_duration(&self, key: &str, val: Duration) -> MetricResult<Histogram> {
        self.histogram_duration_with_tags(key, val).try_send()
    }
    #[deprecated(note = "Use `client.histogram_with_tags(key, val)`")]
    fn histogram_duration_with_tags<'a>(&'a self, key: &'a str, val: Duration) -> MetricBuilder<'_, '_, Histogram>;
}

impl Compat for StatsdClient {
    fn time_duration_with_tags<'a>(&'a self, key: &'a str, val: Duration) -> MetricBuilder<'_, '_, Timer> {
        self.time_with_tags(key, val)
    }

    fn gauge_f64_with_tags<'a>(&'a self, key: &'a str, val: f64) -> MetricBuilder<'_, '_, Gauge> {
        self.gauge_with_tags(key, val)
    }

    fn mark_with_tags<'a>(&'a self, key: &'a str) -> MetricBuilder<'_, '_, Meter> {
        self.meter_with_tags(key, 1)
    }

    fn histogram_duration_with_tags<'a>(&'a self, key: &'a str, val: Duration) -> MetricBuilder<'_, '_, Histogram> {
        self.histogram_with_tags(key, val)
    }
}

#[cfg(test)]
mod tests {
    #![allow(deprecated)]

    use super::Compat;
    use crate::client::StatsdClient;
    use crate::sinks::NopMetricSink;
    use std::time::Duration;

    #[test]
    fn test_statsd_client_timer_compat_methods() {
        let client = StatsdClient::from_sink("test.prefix", NopMetricSink);

        client.time_duration("some.timer", Duration::from_millis(123)).unwrap();
        client
            .time_duration_with_tags("some.timer", Duration::from_millis(123))
            .try_send()
            .unwrap();
    }

    #[test]
    fn test_statsd_client_gauge_compat_methods() {
        let client = StatsdClient::from_sink("test.prefix", NopMetricSink);

        client.gauge_f64("some.gauge", 4.9).unwrap();
        client.gauge_f64_with_tags("some.gauge", 4.9).try_send().unwrap();
    }

    #[test]
    fn test_statsd_client_meter_compat_methods() {
        let client = StatsdClient::from_sink("test.prefix", NopMetricSink);

        client.mark("some.meter").unwrap();
        client.mark_with_tags("some.meter").try_send().unwrap();
    }

    #[test]
    fn test_statsd_client_histogram_compat_methods() {
        let client = StatsdClient::from_sink("test.prefix", NopMetricSink);

        client
            .histogram_duration("some.histogram", Duration::from_nanos(4433))
            .unwrap();
        client
            .histogram_duration_with_tags("some.histogram", Duration::from_nanos(4433))
            .try_send()
            .unwrap();
    }
}