cadence_macros/macros.rs
1// Cadence - An extensible Statsd client for Rust!
2//
3// Copyright 2020-2021 Nick Pillitteri
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11// NOTE: Comments here are mostly just copy/pasted. Make sure to update all of
12// them if you make changes!
13
14/// Emit a counter using the default global client, optionally with tags
15///
16/// The counter will use the prefix from the default global client combined
17/// with the provided key.
18///
19/// Any errors encountered sending metrics will be handled by the error handler
20/// registered with the default global client. This error handler is a no-op
21/// unless explicitly set. Callers should set the error handler for the default
22/// client if you wish to handle these errors (by logging them or something similar).
23///
24/// # Panics
25///
26/// This macro will panic if the default global client has not been set when
27/// it is invoked (via `cadence_macros::set_global_default`).
28///
29/// # Examples
30///
31/// ```
32/// use cadence::{StatsdClient, NopMetricSink};
33/// use cadence_macros::statsd_count;
34///
35/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
36/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
37/// .build();
38///
39/// cadence_macros::set_global_default(client);
40///
41/// // "my.prefix.some.counter:123|c"
42/// statsd_count!("some.counter", 123);
43/// // "my.prefix.some.counter:123|c|#tag:val"
44/// statsd_count!("some.counter", 123, "tag" => "val");
45/// // "my.prefix.some.counter:123|c|#tag:val,another:thing"
46/// statsd_count!("some.counter", 123, "tag" => "val", "another" => "thing");
47/// ```
48///
49/// # Limitations
50///
51/// Only key-value style tags are supported. Value style tags are not
52/// supported, e.g. `builder.with_tag_value("val")`.
53#[macro_export]
54macro_rules! statsd_count {
55 ($key:expr, $val:expr) => {
56 $crate::statsd_count!($key, $val,)
57 };
58
59 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
60 $crate::_generate_impl!(count_with_tags, $key, $val, $($tag_key => $tag_val),*)
61 }
62}
63
64/// Emit a timer using the default global client, optionally with tags
65///
66/// The timer will use the prefix from the default global client combined
67/// with the provided key.
68///
69/// Any errors encountered sending metrics will be handled by the error handler
70/// registered with the default global client. This error handler is a no-op
71/// unless explicitly set. Callers should set the error handler for the default
72/// client if you wish to handle these errors (by logging them or something similar).
73///
74/// # Panics
75///
76/// This macro will panic if the default global client has not been set when
77/// it is invoked (via `cadence_macros::set_global_default`).
78///
79/// # Examples
80///
81/// ```
82/// use cadence::{StatsdClient, NopMetricSink};
83/// use cadence_macros::statsd_time;
84///
85/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
86/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
87/// .build();
88///
89/// cadence_macros::set_global_default(client);
90///
91/// // "my.prefix.some.timer:123|ms"
92/// statsd_time!("some.timer", 123);
93/// // "my.prefix.some.timer:123|ms|#tag:val"
94/// statsd_time!("some.timer", 123, "tag" => "val");
95/// // "my.prefix.some.timer:123|ms|#tag:val,another:thing"
96/// statsd_time!("some.timer", 123, "tag" => "val", "another" => "thing");
97/// ```
98///
99/// # Limitations
100///
101/// Only key-value style tags are supported. Value style tags are not
102/// supported, e.g. `builder.with_tag_value("val")`.
103#[macro_export]
104macro_rules! statsd_time {
105 ($key:expr, $val:expr) => {
106 $crate::statsd_time!($key, $val,)
107 };
108
109 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
110 $crate::_generate_impl!(time_with_tags, $key, $val, $($tag_key => $tag_val),*)
111 }
112}
113
114/// Emit a gauge using the default global client, optionally with tags
115///
116/// The gauge will use the prefix from the default global client combined
117/// with the provided key.
118///
119/// Any errors encountered sending metrics will be handled by the error handler
120/// registered with the default global client. This error handler is a no-op
121/// unless explicitly set. Callers should set the error handler for the default
122/// client if you wish to handle these errors (by logging them or something similar).
123///
124/// # Panics
125///
126/// This macro will panic if the default global client has not been set when
127/// it is invoked (via `cadence_macros::set_global_default`).
128///
129/// # Examples
130///
131/// ```
132/// use cadence::{StatsdClient, NopMetricSink};
133/// use cadence_macros::statsd_gauge;
134///
135/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
136/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
137/// .build();
138///
139/// cadence_macros::set_global_default(client);
140///
141/// // "my.prefix.some.gauge:123|g"
142/// statsd_gauge!("some.gauge", 123);
143/// // "my.prefix.some.gauge:123|g|#tag:val"
144/// statsd_gauge!("some.gauge", 123, "tag" => "val");
145/// // "my.prefix.some.gauge:123|g|#tag:val,another:thing"
146/// statsd_gauge!("some.gauge", 123, "tag" => "val", "another" => "thing");
147/// ```
148///
149/// # Limitations
150///
151/// Only key-value style tags are supported. Value style tags are not
152/// supported, e.g. `builder.with_tag_value("val")`.
153#[macro_export]
154macro_rules! statsd_gauge {
155 ($key:expr, $val:expr) => {
156 $crate::statsd_gauge!($key, $val,)
157 };
158
159 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
160 $crate::_generate_impl!(gauge_with_tags, $key, $val, $($tag_key => $tag_val),*)
161 }
162}
163
164/// Emit a meter using the default global client, optionally with tags
165///
166/// The meter will use the prefix from the default global client combined
167/// with the provided key.
168///
169/// Any errors encountered sending metrics will be handled by the error handler
170/// registered with the default global client. This error handler is a no-op
171/// unless explicitly set. Callers should set the error handler for the default
172/// client if you wish to handle these errors (by logging them or something similar).
173///
174/// # Panics
175///
176/// This macro will panic if the default global client has not been set when
177/// it is invoked (via `cadence_macros::set_global_default`).
178///
179/// # Examples
180///
181/// ```
182/// use cadence::{StatsdClient, NopMetricSink};
183/// use cadence_macros::statsd_meter;
184///
185/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
186/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
187/// .build();
188///
189/// cadence_macros::set_global_default(client);
190///
191/// // "my.prefix.some.meter:123|m"
192/// statsd_meter!("some.meter", 123);
193/// // "my.prefix.some.meter:123|m|#tag:val"
194/// statsd_meter!("some.meter", 123, "tag" => "val");
195/// // "my.prefix.some.meter:123|m|#tag:val,another:thing"
196/// statsd_meter!("some.meter", 123, "tag" => "val", "another" => "thing");
197/// ```
198///
199/// # Limitations
200///
201/// Only key-value style tags are supported. Value style tags are not
202/// supported, e.g. `builder.with_tag_value("val")`.
203#[macro_export]
204macro_rules! statsd_meter {
205 ($key:expr, $val:expr) => {
206 $crate::statsd_meter!($key, $val,)
207 };
208
209 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
210 $crate::_generate_impl!(meter_with_tags, $key, $val, $($tag_key => $tag_val),*)
211 }
212}
213
214/// Emit a histogram using the default global client, optionally with tags
215///
216/// The histogram will use the prefix from the default global client combined
217/// with the provided key.
218///
219/// Any errors encountered sending metrics will be handled by the error handler
220/// registered with the default global client. This error handler is a no-op
221/// unless explicitly set. Callers should set the error handler for the default
222/// client if you wish to handle these errors (by logging them or something similar).
223///
224/// # Panics
225///
226/// This macro will panic if the default global client has not been set when
227/// it is invoked (via `cadence_macros::set_global_default`).
228///
229/// # Examples
230///
231/// ```
232/// use cadence::{StatsdClient, NopMetricSink};
233/// use cadence_macros::statsd_histogram;
234///
235/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
236/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
237/// .build();
238///
239/// cadence_macros::set_global_default(client);
240///
241/// // "my.prefix.some.histogram:123|h"
242/// statsd_histogram!("some.histogram", 123);
243/// // "my.prefix.some.histogram:123|h|#tag:val"
244/// statsd_histogram!("some.histogram", 123, "tag" => "val");
245/// // "my.prefix.some.histogram:123|h|#tag:val,another:thing"
246/// statsd_histogram!("some.histogram", 123, "tag" => "val", "another" => "thing");
247/// ```
248///
249/// # Limitations
250///
251/// Only key-value style tags are supported. Value style tags are not
252/// supported, e.g. `builder.with_tag_value("val")`.
253#[macro_export]
254macro_rules! statsd_histogram {
255 ($key:expr, $val:expr) => {
256 $crate::statsd_histogram!($key, $val,)
257 };
258
259 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
260 $crate::_generate_impl!(histogram_with_tags, $key, $val, $($tag_key => $tag_val),*)
261 }
262}
263
264/// Emit a distribution using the default global client, optionally with tags
265///
266/// The distribution will use the prefix from the default global client combined
267/// with the provided key.
268///
269/// Any errors encountered sending metrics will be handled by the error handler
270/// registered with the default global client. This error handler is a no-op
271/// unless explicitly set. Callers should set the error handler for the default
272/// client if you wish to handle these errors (by logging them or something similar).
273///
274/// # Panics
275///
276/// This macro will panic if the default global client has not been set when
277/// it is invoked (via `cadence_macros::set_global_default`).
278///
279/// # Examples
280///
281/// ```
282/// use cadence::{StatsdClient, NopMetricSink};
283/// use cadence_macros::statsd_distribution;
284///
285/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
286/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
287/// .build();
288///
289/// cadence_macros::set_global_default(client);
290///
291/// // "my.prefix.some.distribution:123|d"
292/// statsd_distribution!("some.distribution", 123);
293/// // "my.prefix.some.distribution:123|d|#tag:val"
294/// statsd_distribution!("some.distribution", 123, "tag" => "val");
295/// // "my.prefix.some.distribution:123|d|#tag:val,another:thing"
296/// statsd_distribution!("some.distribution", 123, "tag" => "val", "another" => "thing");
297/// ```
298///
299/// # Limitations
300///
301/// Only key-value style tags are supported. Value style tags are not
302/// supported, e.g. `builder.with_tag_value("val")`.
303#[macro_export]
304macro_rules! statsd_distribution {
305 ($key:expr, $val:expr) => {
306 $crate::statsd_distribution!($key, $val,)
307 };
308
309 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
310 $crate::_generate_impl!(distribution_with_tags, $key, $val, $($tag_key => $tag_val),*)
311 }
312}
313
314/// Emit a set using the default global client, optionally with tags
315///
316/// The set will use the prefix from the default global client combined
317/// with the provided key.
318///
319/// Any errors encountered sending metrics will be handled by the error handler
320/// registered with the default global client. This error handler is a no-op
321/// unless explicitly set. Callers should set the error handler for the default
322/// client if you wish to handle these errors (by logging them or something similar).
323///
324/// # Panics
325///
326/// This macro will panic if the default global client has not been set when
327/// it is invoked (via `cadence_macros::set_global_default`).
328///
329/// # Examples
330///
331/// ```
332/// use cadence::{StatsdClient, NopMetricSink};
333/// use cadence_macros::statsd_set;
334///
335/// let client = StatsdClient::builder("my.prefix", NopMetricSink)
336/// .with_error_handler(|e| { eprintln!("metric error: {}", e) })
337/// .build();
338///
339/// cadence_macros::set_global_default(client);
340///
341/// // "my.prefix.some.set:123|s"
342/// statsd_set!("some.set", 123);
343/// // "my.prefix.some.set:123|s|#tag:val"
344/// statsd_set!("some.set", 123, "tag" => "val");
345/// // "my.prefix.some.set:123|s|#tag:val,another:thing"
346/// statsd_set!("some.set", 123, "tag" => "val", "another" => "thing");
347/// ```
348///
349/// # Limitations
350///
351/// Only key-value style tags are supported. Value style tags are not
352/// supported, e.g. `builder.with_tag_value("val")`.
353#[macro_export]
354macro_rules! statsd_set {
355 ($key:expr, $val:expr) => {
356 $crate::statsd_set!($key, $val,)
357 };
358
359 ($key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
360 $crate::_generate_impl!(set_with_tags, $key, $val, $($tag_key => $tag_val),*)
361 }
362}
363
364#[macro_export]
365#[doc(hidden)]
366macro_rules! _generate_impl {
367 ($method:ident, $key:expr, $val:expr, $($tag_key:expr => $tag_val:expr),*) => {
368 use cadence::prelude::*;
369 let client = $crate::get_global_default().unwrap();
370 let builder = client.$method($key, $val);
371 $(let builder = builder.with_tag($tag_key, $tag_val);)*
372 builder.send()
373 }
374}