@@ -18,11 +18,7 @@ fn nil_to_none(distinct_id: Option<&String>) -> Option<&String> {
18
18
Some ( distinct_id)
19
19
}
20
20
21
- /// Generate a sessions-related metric name
22
- /// Would be nice to have this as a `const fn`, but [`Metric`] requires a [`String`] anyway.
23
- fn metric_name ( name : & str ) -> String {
24
- format ! ( "sentry.sessions.{}" , name)
25
- }
21
+ const METRIC_NAMESPACE : & str = "sessions" ;
26
22
27
23
pub fn extract_session_metrics < T : SessionLike > (
28
24
attributes : & SessionAttributes ,
@@ -46,110 +42,119 @@ pub fn extract_session_metrics<T: SessionLike>(
46
42
// Always capture with "init" tag for the first session update of a session. This is used
47
43
// for adoption and as baseline for crash rates.
48
44
if session. total_count ( ) > 0 {
49
- target. push ( Metric {
50
- name : metric_name ( "session" ) ,
51
- unit : MetricUnit :: None ,
52
- value : MetricValue :: Counter ( session. total_count ( ) as f64 ) ,
45
+ target. push ( Metric :: new_mri (
46
+ METRIC_NAMESPACE ,
47
+ "session" ,
48
+ MetricUnit :: None ,
49
+ MetricValue :: Counter ( session. total_count ( ) as f64 ) ,
53
50
timestamp,
54
- tags : with_tag ( & tags, "session.status" , "init" ) ,
55
- } ) ;
51
+ with_tag ( & tags, "session.status" , "init" ) ,
52
+ ) ) ;
56
53
57
54
if let Some ( distinct_id) = nil_to_none ( session. distinct_id ( ) ) {
58
- target. push ( Metric {
59
- name : metric_name ( "user" ) ,
60
- unit : MetricUnit :: None ,
61
- value : MetricValue :: set_from_str ( distinct_id) ,
55
+ target. push ( Metric :: new_mri (
56
+ METRIC_NAMESPACE ,
57
+ "user" ,
58
+ MetricUnit :: None ,
59
+ MetricValue :: set_from_str ( distinct_id) ,
62
60
timestamp,
63
- tags : with_tag ( & tags, "session.status" , "init" ) ,
64
- } ) ;
61
+ with_tag ( & tags, "session.status" , "init" ) ,
62
+ ) ) ;
65
63
}
66
64
}
67
65
68
66
// Mark the session as errored, which includes fatal sessions.
69
67
if let Some ( errors) = session. errors ( ) {
70
68
target. push ( match errors {
71
- SessionErrored :: Individual ( session_id) => Metric {
72
- name : metric_name ( "session.error" ) ,
73
- unit : MetricUnit :: None ,
74
- value : MetricValue :: set_from_display ( session_id) ,
69
+ SessionErrored :: Individual ( session_id) => Metric :: new_mri (
70
+ METRIC_NAMESPACE ,
71
+ "error" ,
72
+ MetricUnit :: None ,
73
+ MetricValue :: set_from_display ( session_id) ,
75
74
timestamp,
76
- tags : tags. clone ( ) ,
77
- } ,
78
- SessionErrored :: Aggregated ( count) => Metric {
79
- name : metric_name ( "session" ) ,
80
- unit : MetricUnit :: None ,
81
- value : MetricValue :: Counter ( count as f64 ) ,
75
+ tags. clone ( ) ,
76
+ ) ,
77
+ SessionErrored :: Aggregated ( count) => Metric :: new_mri (
78
+ METRIC_NAMESPACE ,
79
+ "session" ,
80
+ MetricUnit :: None ,
81
+ MetricValue :: Counter ( count as f64 ) ,
82
82
timestamp,
83
- tags : with_tag ( & tags, "session.status" , "errored_preaggr" ) ,
84
- } ,
83
+ with_tag ( & tags, "session.status" , "errored_preaggr" ) ,
84
+ ) ,
85
85
} ) ;
86
86
87
87
if let Some ( distinct_id) = nil_to_none ( session. distinct_id ( ) ) {
88
- target. push ( Metric {
89
- name : metric_name ( "user" ) ,
90
- unit : MetricUnit :: None ,
91
- value : MetricValue :: set_from_str ( distinct_id) ,
88
+ target. push ( Metric :: new_mri (
89
+ METRIC_NAMESPACE ,
90
+ "user" ,
91
+ MetricUnit :: None ,
92
+ MetricValue :: set_from_str ( distinct_id) ,
92
93
timestamp,
93
- tags : with_tag ( & tags, "session.status" , "errored" ) ,
94
- } ) ;
94
+ with_tag ( & tags, "session.status" , "errored" ) ,
95
+ ) ) ;
95
96
}
96
97
}
97
98
98
99
// Record fatal sessions for crash rate computation. This is a strict subset of errored
99
100
// sessions above.
100
101
if session. abnormal_count ( ) > 0 {
101
- target. push ( Metric {
102
- name : metric_name ( "session" ) ,
103
- unit : MetricUnit :: None ,
104
- value : MetricValue :: Counter ( session. abnormal_count ( ) as f64 ) ,
102
+ target. push ( Metric :: new_mri (
103
+ METRIC_NAMESPACE ,
104
+ "session" ,
105
+ MetricUnit :: None ,
106
+ MetricValue :: Counter ( session. abnormal_count ( ) as f64 ) ,
105
107
timestamp,
106
- tags : with_tag ( & tags, "session.status" , SessionStatus :: Abnormal ) ,
107
- } ) ;
108
+ with_tag ( & tags, "session.status" , SessionStatus :: Abnormal ) ,
109
+ ) ) ;
108
110
109
111
if let Some ( distinct_id) = nil_to_none ( session. distinct_id ( ) ) {
110
- target. push ( Metric {
111
- name : metric_name ( "user" ) ,
112
- unit : MetricUnit :: None ,
113
- value : MetricValue :: set_from_str ( distinct_id) ,
112
+ target. push ( Metric :: new_mri (
113
+ METRIC_NAMESPACE ,
114
+ "user" ,
115
+ MetricUnit :: None ,
116
+ MetricValue :: set_from_str ( distinct_id) ,
114
117
timestamp,
115
- tags : with_tag ( & tags, "session.status" , SessionStatus :: Abnormal ) ,
116
- } ) ;
118
+ with_tag ( & tags, "session.status" , SessionStatus :: Abnormal ) ,
119
+ ) ) ;
117
120
}
118
121
}
122
+
119
123
if session. crashed_count ( ) > 0 {
120
- target. push ( Metric {
121
- name : metric_name ( "session" ) ,
122
- unit : MetricUnit :: None ,
123
- value : MetricValue :: Counter ( session. crashed_count ( ) as f64 ) ,
124
+ target. push ( Metric :: new_mri (
125
+ METRIC_NAMESPACE ,
126
+ "session" ,
127
+ MetricUnit :: None ,
128
+ MetricValue :: Counter ( session. crashed_count ( ) as f64 ) ,
124
129
timestamp,
125
- tags : with_tag ( & tags, "session.status" , SessionStatus :: Crashed ) ,
126
- } ) ;
130
+ with_tag ( & tags, "session.status" , SessionStatus :: Crashed ) ,
131
+ ) ) ;
127
132
128
133
if let Some ( distinct_id) = nil_to_none ( session. distinct_id ( ) ) {
129
- target. push ( Metric {
130
- name : metric_name ( "user" ) ,
131
- unit : MetricUnit :: None ,
132
- value : MetricValue :: set_from_str ( distinct_id) ,
134
+ target. push ( Metric :: new_mri (
135
+ METRIC_NAMESPACE ,
136
+ "user" ,
137
+ MetricUnit :: None ,
138
+ MetricValue :: set_from_str ( distinct_id) ,
133
139
timestamp,
134
- tags : with_tag ( & tags, "session.status" , SessionStatus :: Crashed ) ,
135
- } ) ;
140
+ with_tag ( & tags, "session.status" , SessionStatus :: Crashed ) ,
141
+ ) ) ;
136
142
}
137
143
}
138
144
139
145
// Count durations for all exited/crashed sessions. Note that right now, in the product we
140
146
// really only use durations from session.status=exited, but decided it may be worth ingesting
141
147
// this data in case we need it. If we need to cut cost, this is one place to start though.
142
- // if session.status.is_terminal() {
143
148
if let Some ( ( duration, status) ) = session. final_duration ( ) {
144
- target. push ( Metric {
145
- name : metric_name ( "session.duration" ) ,
146
- unit : MetricUnit :: Duration ( DurationPrecision :: Second ) ,
147
- value : MetricValue :: Distribution ( duration) ,
149
+ target. push ( Metric :: new_mri (
150
+ METRIC_NAMESPACE ,
151
+ "duration" ,
152
+ MetricUnit :: Duration ( DurationPrecision :: Second ) ,
153
+ MetricValue :: Distribution ( duration) ,
148
154
timestamp,
149
- tags : with_tag ( & tags, "session.status" , status) ,
150
- } ) ;
155
+ with_tag ( & tags, "session.status" , status) ,
156
+ ) ) ;
151
157
}
152
- // }
153
158
}
154
159
155
160
#[ cfg( test) ]
@@ -210,14 +215,14 @@ mod tests {
210
215
211
216
let session_metric = & metrics[ 0 ] ;
212
217
assert_eq ! ( session_metric. timestamp, started( ) ) ;
213
- assert_eq ! ( session_metric. name, "sentry. sessions. session" ) ;
218
+ assert_eq ! ( session_metric. name, "c: sessions/ session@ " ) ;
214
219
assert ! ( matches!( session_metric. value, MetricValue :: Counter ( _) ) ) ;
215
220
assert_eq ! ( session_metric. tags[ "session.status" ] , "init" ) ;
216
221
assert_eq ! ( session_metric. tags[ "release" ] , "1.0.0" ) ;
217
222
218
223
let user_metric = & metrics[ 1 ] ;
219
224
assert_eq ! ( session_metric. timestamp, started( ) ) ;
220
- assert_eq ! ( user_metric. name, "sentry. sessions. user" ) ;
225
+ assert_eq ! ( user_metric. name, "s: sessions/ user@ " ) ;
221
226
assert ! ( matches!( user_metric. value, MetricValue :: Set ( _) ) ) ;
222
227
assert_eq ! ( session_metric. tags[ "session.status" ] , "init" ) ;
223
228
assert_eq ! ( user_metric. tags[ "release" ] , "1.0.0" ) ;
@@ -281,13 +286,13 @@ mod tests {
281
286
282
287
let session_metric = & metrics[ expected_metrics - 2 ] ;
283
288
assert_eq ! ( session_metric. timestamp, started( ) ) ;
284
- assert_eq ! ( session_metric. name, "sentry. sessions.session. error" ) ;
289
+ assert_eq ! ( session_metric. name, "s: sessions/ error@ " ) ;
285
290
assert ! ( matches!( session_metric. value, MetricValue :: Set ( _) ) ) ;
286
291
assert_eq ! ( session_metric. tags. len( ) , 1 ) ; // Only the release tag
287
292
288
293
let user_metric = & metrics[ expected_metrics - 1 ] ;
289
294
assert_eq ! ( session_metric. timestamp, started( ) ) ;
290
- assert_eq ! ( user_metric. name, "sentry. sessions. user" ) ;
295
+ assert_eq ! ( user_metric. name, "s: sessions/ user@ " ) ;
291
296
assert ! ( matches!( user_metric. value, MetricValue :: Set ( _) ) ) ;
292
297
assert_eq ! ( user_metric. tags[ "session.status" ] , "errored" ) ;
293
298
assert_eq ! ( user_metric. tags[ "release" ] , "1.0.0" ) ;
@@ -317,19 +322,19 @@ mod tests {
317
322
318
323
assert_eq ! ( metrics. len( ) , 4 ) ;
319
324
320
- assert_eq ! ( metrics[ 0 ] . name, "sentry. sessions.session. error" ) ;
321
- assert_eq ! ( metrics[ 1 ] . name, "sentry. sessions. user" ) ;
325
+ assert_eq ! ( metrics[ 0 ] . name, "s: sessions/ error@ " ) ;
326
+ assert_eq ! ( metrics[ 1 ] . name, "s: sessions/ user@ " ) ;
322
327
assert_eq ! ( metrics[ 1 ] . tags[ "session.status" ] , "errored" ) ;
323
328
324
329
let session_metric = & metrics[ 2 ] ;
325
330
assert_eq ! ( session_metric. timestamp, started( ) ) ;
326
- assert_eq ! ( session_metric. name, "sentry. sessions. session" ) ;
331
+ assert_eq ! ( session_metric. name, "c: sessions/ session@ " ) ;
327
332
assert ! ( matches!( session_metric. value, MetricValue :: Counter ( _) ) ) ;
328
333
assert_eq ! ( session_metric. tags[ "session.status" ] , status. to_string( ) ) ;
329
334
330
335
let user_metric = & metrics[ 3 ] ;
331
336
assert_eq ! ( session_metric. timestamp, started( ) ) ;
332
- assert_eq ! ( user_metric. name, "sentry. sessions. user" ) ;
337
+ assert_eq ! ( user_metric. name, "s: sessions/ user@ " ) ;
333
338
assert ! ( matches!( user_metric. value, MetricValue :: Set ( _) ) ) ;
334
339
assert_eq ! ( user_metric. tags[ "session.status" ] , status. to_string( ) ) ;
335
340
}
@@ -359,7 +364,7 @@ mod tests {
359
364
assert_eq ! ( metrics. len( ) , 1 ) ;
360
365
361
366
let duration_metric = & metrics[ 0 ] ;
362
- assert_eq ! ( duration_metric. name, "sentry. sessions.session. duration" ) ;
367
+ assert_eq ! ( duration_metric. name, "d: sessions/ duration@s " ) ;
363
368
assert ! ( matches!(
364
369
duration_metric. value,
365
370
MetricValue :: Distribution ( _)
@@ -402,7 +407,7 @@ mod tests {
402
407
insta:: assert_debug_snapshot!( metrics, @r###"
403
408
[
404
409
Metric {
405
- name: "sentry. sessions. session",
410
+ name: "c: sessions/ session@ ",
406
411
unit: None,
407
412
value: Counter(
408
413
135.0,
@@ -415,7 +420,7 @@ mod tests {
415
420
},
416
421
},
417
422
Metric {
418
- name: "sentry. sessions. session",
423
+ name: "c: sessions/ session@ ",
419
424
unit: None,
420
425
value: Counter(
421
426
5.0,
@@ -428,7 +433,7 @@ mod tests {
428
433
},
429
434
},
430
435
Metric {
431
- name: "sentry. sessions. session",
436
+ name: "c: sessions/ session@ ",
432
437
unit: None,
433
438
value: Counter(
434
439
7.0,
@@ -441,7 +446,7 @@ mod tests {
441
446
},
442
447
},
443
448
Metric {
444
- name: "sentry. sessions. session",
449
+ name: "c: sessions/ session@ ",
445
450
unit: None,
446
451
value: Counter(
447
452
15.0,
@@ -454,7 +459,7 @@ mod tests {
454
459
},
455
460
},
456
461
Metric {
457
- name: "sentry. sessions. user",
462
+ name: "s: sessions/ user@ ",
458
463
unit: None,
459
464
value: Set(
460
465
3097475539,
@@ -467,7 +472,7 @@ mod tests {
467
472
},
468
473
},
469
474
Metric {
470
- name: "sentry. sessions. session",
475
+ name: "c: sessions/ session@ ",
471
476
unit: None,
472
477
value: Counter(
473
478
3.0,
@@ -480,7 +485,7 @@ mod tests {
480
485
},
481
486
},
482
487
Metric {
483
- name: "sentry. sessions. user",
488
+ name: "s: sessions/ user@ ",
484
489
unit: None,
485
490
value: Set(
486
491
3097475539,
0 commit comments