22 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
5-
5+
66use crate :: config:: { Bind , TlsConfig } ;
7+ use crate :: message:: MessageType ;
78use crate :: { serve_at, Result } ;
89use serde:: { Serialize , Serializer } ;
910use std:: fmt:: Write ;
@@ -22,6 +23,10 @@ pub struct Metrics {
2223 mapping_query_count : AtomicUsize ,
2324 events_received : AtomicUsize ,
2425 messages_sent : AtomicUsize ,
26+ messages_sent_file : AtomicUsize ,
27+ messages_sent_activity : AtomicUsize ,
28+ messages_sent_notification : AtomicUsize ,
29+ messages_sent_custom : AtomicUsize ,
2530}
2631
2732#[ derive( Serialize ) ]
@@ -32,6 +37,10 @@ struct SerializeMetrics {
3237 mapping_query_count : usize ,
3338 events_received : usize ,
3439 messages_sent : usize ,
40+ messages_sent_file : usize ,
41+ messages_sent_activity : usize ,
42+ messages_sent_notification : usize ,
43+ messages_sent_custom : usize ,
3544}
3645
3746impl From < & Metrics > for SerializeMetrics {
@@ -43,6 +52,10 @@ impl From<&Metrics> for SerializeMetrics {
4352 mapping_query_count : metrics. mapping_query_count ( ) ,
4453 events_received : metrics. events_received ( ) ,
4554 messages_sent : metrics. messages_sent ( ) ,
55+ messages_sent_file : metrics. messages_sent_file ( ) ,
56+ messages_sent_activity : metrics. messages_sent_activity ( ) ,
57+ messages_sent_notification : metrics. messages_sent_notification ( ) ,
58+ messages_sent_custom : metrics. messages_sent_custom ( ) ,
4659 }
4760 }
4861}
@@ -65,6 +78,10 @@ impl Metrics {
6578 mapping_query_count : AtomicUsize :: new ( 0 ) ,
6679 events_received : AtomicUsize :: new ( 0 ) ,
6780 messages_sent : AtomicUsize :: new ( 0 ) ,
81+ messages_sent_file : AtomicUsize :: new ( 0 ) ,
82+ messages_sent_activity : AtomicUsize :: new ( 0 ) ,
83+ messages_sent_notification : AtomicUsize :: new ( 0 ) ,
84+ messages_sent_custom : AtomicUsize :: new ( 0 ) ,
6885 }
6986 }
7087
@@ -88,6 +105,22 @@ impl Metrics {
88105 self . messages_sent . load ( Ordering :: Relaxed )
89106 }
90107
108+ pub fn messages_sent_file ( & self ) -> usize {
109+ self . messages_sent_file . load ( Ordering :: Relaxed )
110+ }
111+
112+ pub fn messages_sent_activity ( & self ) -> usize {
113+ self . messages_sent_activity . load ( Ordering :: Relaxed )
114+ }
115+
116+ pub fn messages_sent_notification ( & self ) -> usize {
117+ self . messages_sent_notification . load ( Ordering :: Relaxed )
118+ }
119+
120+ pub fn messages_sent_custom ( & self ) -> usize {
121+ self . messages_sent_custom . load ( Ordering :: Relaxed )
122+ }
123+
91124 pub fn add_connection ( & self ) {
92125 self . total_connection_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
93126 self . active_connection_count . fetch_add ( 1 , Ordering :: Relaxed ) ;
@@ -98,7 +131,7 @@ impl Metrics {
98131 }
99132
100133 pub fn active_user_count ( & self ) -> usize {
101- self . active_user_count . load ( Ordering :: Relaxed )
134+ self . active_user_count . load ( Ordering :: Relaxed )
102135 }
103136
104137 pub fn add_user ( & self ) {
@@ -117,7 +150,15 @@ impl Metrics {
117150 self . events_received . fetch_add ( 1 , Ordering :: Relaxed ) ;
118151 }
119152
120- pub fn add_message ( & self ) {
153+ pub fn add_message ( & self , ty : MessageType ) {
154+ match ty {
155+ MessageType :: File => self . messages_sent_file . fetch_add ( 1 , Ordering :: Relaxed ) ,
156+ MessageType :: Activity => self . messages_sent_activity . fetch_add ( 1 , Ordering :: Relaxed ) ,
157+ MessageType :: Notification => self
158+ . messages_sent_notification
159+ . fetch_add ( 1 , Ordering :: Relaxed ) ,
160+ MessageType :: Custom => self . messages_sent_custom . fetch_add ( 1 , Ordering :: Relaxed ) ,
161+ } ;
121162 self . messages_sent . fetch_add ( 1 , Ordering :: Relaxed ) ;
122163 }
123164}
@@ -159,6 +200,26 @@ pub fn serve_metrics(
159200 "message_count_total {}" ,
160201 METRICS . messages_sent( )
161202 ) ;
203+ let _ = writeln ! (
204+ & mut response,
205+ "message_count_total{{type=\" file\" }} {}" ,
206+ METRICS . messages_sent_file( )
207+ ) ;
208+ let _ = writeln ! (
209+ & mut response,
210+ "message_count_total{{type=\" notification\" }} {}" ,
211+ METRICS . messages_sent_notification( )
212+ ) ;
213+ let _ = writeln ! (
214+ & mut response,
215+ "message_count_total{{type=\" activity\" }} {}" ,
216+ METRICS . messages_sent_activity( )
217+ ) ;
218+ let _ = writeln ! (
219+ & mut response,
220+ "message_count_total{{type=\" custom\" }} {}" ,
221+ METRICS . messages_sent_custom( )
222+ ) ;
162223 response
163224 } ) ;
164225
0 commit comments