1
1
use opentelemetry:: global;
2
- use opentelemetry:: Key ;
3
2
use opentelemetry:: KeyValue ;
4
- use opentelemetry_sdk:: metrics:: { Aggregation , Instrument , SdkMeterProvider , Stream , Temporality } ;
3
+ use opentelemetry_sdk:: metrics:: { Instrument , SdkMeterProvider , Stream , Temporality } ;
5
4
use opentelemetry_sdk:: Resource ;
6
5
use std:: error:: Error ;
7
6
@@ -20,23 +19,9 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
20
19
} ;
21
20
22
21
// for example 2
23
- let my_view_drop_attributes = |i : & Instrument | {
24
- if i. name == "my_counter" {
25
- Some ( Stream :: new ( ) . allowed_attribute_keys ( vec ! [ Key :: from( "mykey1" ) ] ) )
26
- } else {
27
- None
28
- }
29
- } ;
30
-
31
- // for example 3
32
- let my_view_change_aggregation = |i : & Instrument | {
22
+ let my_view_change_cardinality = |i : & Instrument | {
33
23
if i. name == "my_second_histogram" {
34
- Some (
35
- Stream :: new ( ) . aggregation ( Aggregation :: ExplicitBucketHistogram {
36
- boundaries : vec ! [ 0.9 , 1.0 , 1.1 , 1.2 , 1.3 , 1.4 , 1.5 ] ,
37
- record_min_max : false ,
38
- } ) ,
39
- )
24
+ Some ( Stream :: new ( ) . cardinality_limit ( 2 ) )
40
25
} else {
41
26
None
42
27
}
@@ -55,8 +40,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
55
40
. with_periodic_exporter ( exporter)
56
41
. with_resource ( resource)
57
42
. with_view ( my_view_rename_and_unit)
58
- . with_view ( my_view_drop_attributes)
59
- . with_view ( my_view_change_aggregation)
43
+ . with_view ( my_view_change_cardinality)
60
44
. build ( ) ;
61
45
global:: set_meter_provider ( provider. clone ( ) ) ;
62
46
provider
@@ -88,65 +72,31 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
88
72
] ,
89
73
) ;
90
74
91
- // Example 2 - Drop unwanted attributes using view.
92
- let counter = meter. u64_counter ( "my_counter" ) . build ( ) ;
93
-
94
- // Record measurements using the Counter instrument.
95
- // Though we are passing 4 attributes here, only 1 will be used
96
- // for aggregation as view is configured to use only "mykey1"
97
- // attribute.
98
- counter. add (
99
- 10 ,
100
- & [
101
- KeyValue :: new ( "mykey1" , "myvalue1" ) ,
102
- KeyValue :: new ( "mykey2" , "myvalue2" ) ,
103
- KeyValue :: new ( "mykey3" , "myvalue3" ) ,
104
- KeyValue :: new ( "mykey4" , "myvalue4" ) ,
105
- ] ,
106
- ) ;
107
-
108
- // Example 3 - Change Aggregation configuration using View.
109
- // Histograms are by default aggregated using ExplicitBucketHistogram
110
- // with default buckets. The configured view will change the aggregation to
111
- // use a custom set of boundaries, and min/max values will not be recorded.
75
+ // Example 2 - Change cardinality using View.
112
76
let histogram2 = meter
113
77
. f64_histogram ( "my_second_histogram" )
114
78
. with_unit ( "ms" )
115
79
. with_description ( "My histogram example description" )
116
80
. build ( ) ;
117
81
118
82
// Record measurements using the histogram instrument.
119
- // The values recorded are in the range of 1.2 to 1.5, warranting
120
- // the change of boundaries.
121
- histogram2. record (
122
- 1.5 ,
123
- & [
124
- KeyValue :: new ( "mykey1" , "myvalue1" ) ,
125
- KeyValue :: new ( "mykey2" , "myvalue2" ) ,
126
- KeyValue :: new ( "mykey3" , "myvalue3" ) ,
127
- KeyValue :: new ( "mykey4" , "myvalue4" ) ,
128
- ] ,
129
- ) ;
83
+ // This metric will have a cardinality limit of 2,
84
+ // as set in the view. Because of this, only the first two
85
+ // measurements will be recorded, and the rest will be folded
86
+ // into the overflow attribute.
87
+ histogram2. record ( 1.5 , & [ KeyValue :: new ( "mykey1" , "v1" ) ] ) ;
130
88
131
- histogram2. record (
132
- 1.2 ,
133
- & [
134
- KeyValue :: new ( "mykey1" , "myvalue1" ) ,
135
- KeyValue :: new ( "mykey2" , "myvalue2" ) ,
136
- KeyValue :: new ( "mykey3" , "myvalue3" ) ,
137
- KeyValue :: new ( "mykey4" , "myvalue4" ) ,
138
- ] ,
139
- ) ;
89
+ histogram2. record ( 1.2 , & [ KeyValue :: new ( "mykey1" , "v2" ) ] ) ;
140
90
141
- histogram2. record (
142
- 1.23 ,
143
- & [
144
- KeyValue :: new ( "mykey1" , "myvalue1" ) ,
145
- KeyValue :: new ( "mykey2 " , "myvalue2" ) ,
146
- KeyValue :: new ( "mykey3" , "myvalue3" ) ,
147
- KeyValue :: new ( "mykey4 " , "myvalue4" ) ,
148
- ] ,
149
- ) ;
91
+ histogram2. record ( 1.23 , & [ KeyValue :: new ( "mykey1" , "v3" ) ] ) ;
92
+
93
+ histogram2 . record ( 1.4 , & [ KeyValue :: new ( "mykey1" , "v4" ) ] ) ;
94
+
95
+ histogram2 . record ( 1.6 , & [ KeyValue :: new ( "mykey1 " , "v5" ) ] ) ;
96
+
97
+ histogram2 . record ( 1.7 , & [ KeyValue :: new ( "mykey1 " , "v6" ) ] ) ;
98
+
99
+ histogram2 . record ( 1.8 , & [ KeyValue :: new ( "mykey1" , "v7" ) ] ) ;
150
100
151
101
// Metrics are exported by default every 30 seconds when using stdout exporter,
152
102
// however shutting down the MeterProvider here instantly flushes
0 commit comments