1- #if NET6_0_OR_GREATER
1+ #if NET8_0_OR_GREATER
22using System ;
33using System . Collections . Generic ;
44using System . Diagnostics ;
88
99namespace RabbitMQ . AMQP . Client . Impl
1010{
11- #if NET6_0_OR_GREATER
11+ #if NET8_0_OR_GREATER
1212 // .NET docs on metric instrumentation: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation
1313 // OpenTelemetry semantic conventions for messaging metric: https://opentelemetry.io/docs/specs/semconv/messaging/messaging-metrics
1414 internal sealed class MetricsReporter : IMetricsReporter
1515 {
1616 const string Version = "0.1.0" ;
1717
18- static readonly Counter < int > s_messagingClientSentMessages ;
19- static readonly Histogram < double > s_messagingClientOperationDuration ;
18+ readonly Counter < int > _messagingClientSentMessages ;
19+ readonly Histogram < double > _messagingClientOperationDuration ;
2020
21- static readonly Counter < int > s_messagingClientConsumedMessages ;
22- static readonly Histogram < double > s_messagingProcessDuration ;
21+ readonly Counter < int > _messagingClientConsumedMessages ;
22+ readonly Histogram < double > _messagingProcessDuration ;
2323
2424 readonly KeyValuePair < string , object ? >
2525 _messagingOperationSystemTag = new ( MessagingSystem , MessagingSystemValue ) ;
@@ -44,29 +44,29 @@ internal sealed class MetricsReporter : IMetricsReporter
4444 private const string SendOperation = "send" ;
4545 private const string MessagingSystemValue = "rabbitmq" ;
4646
47- static MetricsReporter ( )
47+ public MetricsReporter ( IMeterFactory meterFactory )
4848 {
49- Meter meter = new ( "RabbitMQ.Amqp" , Version ) ;
49+ Meter meter = meterFactory . Create ( "RabbitMQ.Amqp" , Version ) ;
5050
51- s_messagingClientSentMessages = meter . CreateCounter < int > (
51+ _messagingClientSentMessages = meter . CreateCounter < int > (
5252 "messaging.client.sent.messages" ,
5353 unit : "{message}" ,
5454 description :
5555 "Number of messages producer attempted to send to the broker." ) ;
5656
57- s_messagingClientOperationDuration = meter . CreateHistogram < double > (
57+ _messagingClientOperationDuration = meter . CreateHistogram < double > (
5858 "messaging.client.operation.duration" ,
5959 unit : "s" ,
6060 description :
6161 "Duration of messaging operation initiated by a producer or consumer client." ) ;
6262
63- s_messagingClientConsumedMessages = meter . CreateCounter < int > (
63+ _messagingClientConsumedMessages = meter . CreateCounter < int > (
6464 "messaging.client.consumed.messages" ,
6565 unit : "{message}" ,
6666 description :
6767 "Number of messages that were delivered to the application. " ) ;
6868
69- s_messagingProcessDuration = meter . CreateHistogram < double > (
69+ _messagingProcessDuration = meter . CreateHistogram < double > (
7070 "messaging.process.duration" ,
7171 unit : "s" ,
7272 description :
@@ -79,17 +79,12 @@ public void ReportMessageSendSuccess(IMetricsReporter.PublisherContext context,
7979 var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
8080 var destination = new KeyValuePair < string , object ? > ( MessageDestinationName , context . Destination ) ;
8181
82- s_messagingClientSentMessages . Add ( 1 , serverAddress , serverPort , destination , _messagingOperationSystemTag ,
82+ _messagingClientSentMessages . Add ( 1 , serverAddress , serverPort , destination , _messagingOperationSystemTag ,
8383 _sendOperationType , _publishOperationName ) ;
8484 if ( startTimestamp > 0 )
8585 {
86- #if NET7_0_OR_GREATER
87- var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
88- #else
89- var duration =
90- new TimeSpan ( ( long ) ( ( Stopwatch . GetTimestamp ( ) - startTimestamp ) * s_stopWatchTickFrequency ) ) ;
91- #endif
92- s_messagingClientOperationDuration . Record ( duration . TotalSeconds , serverAddress , serverPort , destination ,
86+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
87+ _messagingClientOperationDuration . Record ( duration . TotalSeconds , serverAddress , serverPort , destination ,
9388 _messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
9489 }
9590 }
@@ -101,18 +96,13 @@ public void ReportMessageSendFailure(IMetricsReporter.PublisherContext context,
10196 var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
10297 var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
10398 var destination = new KeyValuePair < string , object ? > ( MessageDestinationName , context . Destination ) ;
104- s_messagingClientSentMessages . Add ( 1 , errorType , serverAddress , serverPort , destination ,
99+ _messagingClientSentMessages . Add ( 1 , errorType , serverAddress , serverPort , destination ,
105100 _messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
106101
107102 if ( startTimestamp > 0 )
108103 {
109- #if NET7_0_OR_GREATER
110- var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
111- #else
112- var duration =
113- new TimeSpan ( ( long ) ( ( Stopwatch . GetTimestamp ( ) - startTimestamp ) * s_stopWatchTickFrequency ) ) ;
114- #endif
115- s_messagingClientOperationDuration . Record ( duration . TotalSeconds , errorType , serverAddress , serverPort ,
104+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
105+ _messagingClientOperationDuration . Record ( duration . TotalSeconds , errorType , serverAddress , serverPort ,
116106 destination ,
117107 _messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
118108 }
@@ -123,20 +113,18 @@ public void ReportMessageDeliverSuccess(IMetricsReporter.ConsumerContext context
123113 var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
124114 var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
125115 var destination = new KeyValuePair < string , object ? > ( MessageDestinationName , context . Destination ) ;
126- s_messagingClientConsumedMessages . Add ( 1 , serverAddress , serverPort , destination , _messagingOperationSystemTag ,
116+ _messagingClientConsumedMessages . Add ( 1 , serverAddress , serverPort , destination ,
117+ _messagingOperationSystemTag ,
127118 _processOperationType , _deliverOperationName ) ;
128- if ( startTimestamp > 0 )
119+ if ( startTimestamp <= 0 )
129120 {
130- #if NET7_0_OR_GREATER
131- var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
132- #else
133- var duration =
134- new TimeSpan ( ( long ) ( ( Stopwatch . GetTimestamp ( ) - startTimestamp ) * s_stopWatchTickFrequency ) ) ;
135- #endif
136- s_messagingProcessDuration . Record ( duration . TotalSeconds , serverAddress , serverPort ,
137- destination ,
138- _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
121+ return ;
139122 }
123+
124+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
125+ _messagingProcessDuration . Record ( duration . TotalSeconds , serverAddress , serverPort ,
126+ destination ,
127+ _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
140128 }
141129
142130 public void ReportMessageDeliverFailure ( IMetricsReporter . ConsumerContext context , long startTimestamp ,
@@ -146,28 +134,19 @@ public void ReportMessageDeliverFailure(IMetricsReporter.ConsumerContext context
146134 var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
147135 var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
148136 var destination = new KeyValuePair < string , object ? > ( MessageDestinationName , context . Destination ) ;
149- s_messagingClientConsumedMessages . Add ( 1 , errorType , serverAddress , serverPort , destination ,
137+ _messagingClientConsumedMessages . Add ( 1 , errorType , serverAddress , serverPort , destination ,
150138 _messagingOperationSystemTag ,
151139 _processOperationType , _deliverOperationName ) ;
152- if ( startTimestamp > 0 )
140+ if ( startTimestamp <= 0 )
153141 {
154- #if NET7_0_OR_GREATER
155- var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
156- #else
157- var duration =
158- new TimeSpan ( ( long ) ( ( Stopwatch . GetTimestamp ( ) - startTimestamp ) * s_stopWatchTickFrequency ) ) ;
159- #endif
160- s_messagingProcessDuration . Record ( duration . TotalSeconds , errorType , serverAddress , serverPort ,
161- destination ,
162- _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
142+ return ;
163143 }
144+
145+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
146+ _messagingProcessDuration . Record ( duration . TotalSeconds , errorType , serverAddress , serverPort ,
147+ destination ,
148+ _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
164149 }
165- #if ! NET7_0_OR_GREATER
166- const long TicksPerMicrosecond = 10 ;
167- const long TicksPerMillisecond = TicksPerMicrosecond * 1000 ;
168- const long TicksPerSecond = TicksPerMillisecond * 1000 ; // 10,000,000
169- static readonly double s_stopWatchTickFrequency = ( double ) TicksPerSecond / Stopwatch . Frequency ;
170- #endif
171150 }
172151#else
173152#endif
0 commit comments