@@ -8,12 +8,17 @@ public interface IRequestMetrics
88{
99 void MessageReceived ( string ? messageType , string ? requestPath , string ? legend , string routeAction ) ;
1010 void MessageSuccessfullySent ( string ? messageType , string ? requestPath , string ? legend , string routeAction ) ;
11+ void RequestCompleted ( string requestPath , string httpMethod , int statusCode , double milliseconds ) ;
12+ void RequestFaulted ( string requestPath , string httpMethod , int statusCode , Exception exception ) ;
1113}
1214
1315public class RequestMetrics : IRequestMetrics
1416{
1517 private readonly Counter < long > messagesReceived ;
1618 private readonly Counter < long > messagesSuccessfullySent ;
19+ private readonly Counter < long > requestsReceived ;
20+ private readonly Counter < long > requestsFaulted ;
21+ private readonly Histogram < double > requestDuration ;
1722
1823 public RequestMetrics ( IMeterFactory meterFactory )
1924 {
@@ -30,6 +35,24 @@ public RequestMetrics(IMeterFactory meterFactory)
3035 Unit . COUNT . ToString ( ) ,
3136 "Count of messages successfully sent"
3237 ) ;
38+
39+ requestsReceived = meter . CreateCounter < long > (
40+ MetricsConstants . InstrumentNames . RequestReceived ,
41+ Unit . COUNT . ToString ( ) ,
42+ "Count of messages received"
43+ ) ;
44+
45+ requestDuration = meter . CreateHistogram < double > (
46+ MetricsConstants . InstrumentNames . RequestDuration ,
47+ Unit . MILLISECONDS . ToString ( ) ,
48+ "Duration of request"
49+ ) ;
50+
51+ requestsFaulted = meter . CreateCounter < long > (
52+ MetricsConstants . InstrumentNames . RequestFaulted ,
53+ Unit . COUNT . ToString ( ) ,
54+ "Count of request faults"
55+ ) ;
3356 }
3457
3558 public void MessageReceived ( string ? messageType , string ? requestPath , string ? legend , string routeAction )
@@ -42,6 +65,19 @@ public void MessageSuccessfullySent(string? messageType, string? requestPath, st
4265 messagesSuccessfullySent . Add ( 1 , BuildTags ( messageType , requestPath , legend , routeAction ) ) ;
4366 }
4467
68+ public void RequestCompleted ( string requestPath , string httpMethod , int statusCode , double milliseconds )
69+ {
70+ requestsReceived . Add ( 1 , BuildRequestTags ( requestPath , httpMethod , statusCode ) ) ;
71+ requestDuration . Record ( milliseconds , BuildRequestTags ( requestPath , httpMethod , statusCode ) ) ;
72+ }
73+
74+ public void RequestFaulted ( string requestPath , string httpMethod , int statusCode , Exception exception )
75+ {
76+ var tagList = BuildRequestTags ( requestPath , httpMethod , statusCode ) ;
77+ tagList . Add ( MetricsConstants . RequestTags . ExceptionType , exception . GetType ( ) . Name ) ;
78+ requestsFaulted . Add ( 1 , tagList ) ;
79+ }
80+
4581 private static TagList BuildTags ( string ? messageType , string ? requestPath , string ? legend , string routeAction )
4682 {
4783 return new TagList
@@ -53,4 +89,15 @@ private static TagList BuildTags(string? messageType, string? requestPath, strin
5389 { MetricsConstants . RequestTags . RouteAction , routeAction } ,
5490 } ;
5591 }
92+
93+ private static TagList BuildRequestTags ( string requestPath , string httpMethod , int statusCode )
94+ {
95+ return new TagList
96+ {
97+ { MetricsConstants . RequestTags . Service , Process . GetCurrentProcess ( ) . ProcessName } ,
98+ { MetricsConstants . RequestTags . RequestPath , requestPath } ,
99+ { MetricsConstants . RequestTags . HttpMethod , httpMethod } ,
100+ { MetricsConstants . RequestTags . StatusCode , statusCode } ,
101+ } ;
102+ }
56103}
0 commit comments