1
1
use std:: { collections:: HashSet , time:: SystemTime } ;
2
2
3
- use metrics:: Key ;
3
+ use metrics:: { Key , Label } ;
4
4
use metrics_util:: registry:: Registry ;
5
5
use tracing:: error;
6
6
@@ -27,6 +27,12 @@ pub struct StateConfiguration {
27
27
28
28
/// Whether or not to emit histograms as distributions.
29
29
pub histograms_as_distributions : bool ,
30
+
31
+ /// Global labels to add to all metrics
32
+ pub global_labels : Vec < Label > ,
33
+
34
+ /// Global prefix/namespace to use for all metrics
35
+ pub global_prefix : Option < String > ,
30
36
}
31
37
32
38
/// Exporter state.
@@ -96,7 +102,19 @@ impl State {
96
102
97
103
active_counters += 1 ;
98
104
99
- let result = writer. write_counter ( & key, value, self . get_aggregation_timestamp ( ) ) ;
105
+ let prefix = if key. name ( ) . starts_with ( "datadog.dogstatsd.client" ) {
106
+ None
107
+ } else {
108
+ self . config . global_prefix . as_deref ( )
109
+ } ;
110
+
111
+ let result = writer. write_counter (
112
+ & key,
113
+ value,
114
+ self . get_aggregation_timestamp ( ) ,
115
+ prefix,
116
+ & self . config . global_labels ,
117
+ ) ;
100
118
if result. any_failures ( ) {
101
119
let points_dropped = result. points_dropped ( ) ;
102
120
error ! (
@@ -117,7 +135,18 @@ impl State {
117
135
118
136
for ( key, gauge) in gauges {
119
137
let ( value, points_flushed) = gauge. flush ( ) ;
120
- let result = writer. write_gauge ( & key, value, self . get_aggregation_timestamp ( ) ) ;
138
+ let prefix = if key. name ( ) . starts_with ( "datadog.dogstatsd.client" ) {
139
+ None
140
+ } else {
141
+ self . config . global_prefix . as_deref ( )
142
+ } ;
143
+ let result = writer. write_gauge (
144
+ & key,
145
+ value,
146
+ self . get_aggregation_timestamp ( ) ,
147
+ prefix,
148
+ & self . config . global_labels ,
149
+ ) ;
121
150
if result. any_failures ( ) {
122
151
let points_dropped = result. points_dropped ( ) ;
123
152
error ! ( metric_name = key. name( ) , points_dropped, "Failed to build gauge payload." ) ;
@@ -137,13 +166,30 @@ impl State {
137
166
}
138
167
139
168
active_histograms += 1 ;
169
+ let prefix = if key. name ( ) . starts_with ( "datadog.dogstatsd.client" ) {
170
+ None
171
+ } else {
172
+ self . config . global_prefix . as_deref ( )
173
+ } ;
140
174
141
175
histogram. flush ( |maybe_sample_rate, values| {
142
176
let points_len = values. len ( ) ;
143
177
let result = if self . config . histograms_as_distributions {
144
- writer. write_distribution ( & key, values, maybe_sample_rate)
178
+ writer. write_distribution (
179
+ & key,
180
+ values,
181
+ maybe_sample_rate,
182
+ prefix,
183
+ & self . config . global_labels ,
184
+ )
145
185
} else {
146
- writer. write_histogram ( & key, values, maybe_sample_rate)
186
+ writer. write_histogram (
187
+ & key,
188
+ values,
189
+ maybe_sample_rate,
190
+ prefix,
191
+ & self . config . global_labels ,
192
+ )
147
193
} ;
148
194
149
195
// Scale the points flushed/dropped values by the sample rate to determine the true number of points flushed/dropped.
0 commit comments