1
- #if NET6_0_OR_GREATER
1
+ #if NET8_0_OR_GREATER
2
2
using System ;
3
3
using System . Collections . Generic ;
4
4
using System . Diagnostics ;
8
8
9
9
namespace RabbitMQ . AMQP . Client . Impl
10
10
{
11
- #if NET6_0_OR_GREATER
11
+ #if NET8_0_OR_GREATER
12
12
// .NET docs on metric instrumentation: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation
13
13
// OpenTelemetry semantic conventions for messaging metric: https://opentelemetry.io/docs/specs/semconv/messaging/messaging-metrics
14
14
internal sealed class MetricsReporter : IMetricsReporter
15
15
{
16
16
const string Version = "0.1.0" ;
17
17
18
- static readonly Counter < int > s_messagingClientSentMessages ;
19
- static readonly Histogram < double > s_messagingClientOperationDuration ;
18
+ readonly Counter < int > _messagingClientSentMessages ;
19
+ readonly Histogram < double > _messagingClientOperationDuration ;
20
20
21
- static readonly Counter < int > s_messagingClientConsumedMessages ;
22
- static readonly Histogram < double > s_messagingProcessDuration ;
21
+ readonly Counter < int > _messagingClientConsumedMessages ;
22
+ readonly Histogram < double > _messagingProcessDuration ;
23
23
24
24
readonly KeyValuePair < string , object ? >
25
25
_messagingOperationSystemTag = new ( MessagingSystem , MessagingSystemValue ) ;
@@ -44,29 +44,29 @@ internal sealed class MetricsReporter : IMetricsReporter
44
44
private const string SendOperation = "send" ;
45
45
private const string MessagingSystemValue = "rabbitmq" ;
46
46
47
- static MetricsReporter ( )
47
+ public MetricsReporter ( IMeterFactory meterFactory )
48
48
{
49
- Meter meter = new ( "RabbitMQ.Amqp" , Version ) ;
49
+ Meter meter = meterFactory . Create ( "RabbitMQ.Amqp" , Version ) ;
50
50
51
- s_messagingClientSentMessages = meter . CreateCounter < int > (
51
+ _messagingClientSentMessages = meter . CreateCounter < int > (
52
52
"messaging.client.sent.messages" ,
53
53
unit : "{message}" ,
54
54
description :
55
55
"Number of messages producer attempted to send to the broker." ) ;
56
56
57
- s_messagingClientOperationDuration = meter . CreateHistogram < double > (
57
+ _messagingClientOperationDuration = meter . CreateHistogram < double > (
58
58
"messaging.client.operation.duration" ,
59
59
unit : "s" ,
60
60
description :
61
61
"Duration of messaging operation initiated by a producer or consumer client." ) ;
62
62
63
- s_messagingClientConsumedMessages = meter . CreateCounter < int > (
63
+ _messagingClientConsumedMessages = meter . CreateCounter < int > (
64
64
"messaging.client.consumed.messages" ,
65
65
unit : "{message}" ,
66
66
description :
67
67
"Number of messages that were delivered to the application. " ) ;
68
68
69
- s_messagingProcessDuration = meter . CreateHistogram < double > (
69
+ _messagingProcessDuration = meter . CreateHistogram < double > (
70
70
"messaging.process.duration" ,
71
71
unit : "s" ,
72
72
description :
@@ -79,17 +79,12 @@ public void ReportMessageSendSuccess(IMetricsReporter.PublisherContext context,
79
79
var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
80
80
var destination = new KeyValuePair < string , object ? > ( MessageDestinationName , context . Destination ) ;
81
81
82
- s_messagingClientSentMessages . Add ( 1 , serverAddress , serverPort , destination , _messagingOperationSystemTag ,
82
+ _messagingClientSentMessages . Add ( 1 , serverAddress , serverPort , destination , _messagingOperationSystemTag ,
83
83
_sendOperationType , _publishOperationName ) ;
84
84
if ( startTimestamp > 0 )
85
85
{
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 ,
93
88
_messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
94
89
}
95
90
}
@@ -101,18 +96,13 @@ public void ReportMessageSendFailure(IMetricsReporter.PublisherContext context,
101
96
var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
102
97
var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
103
98
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 ,
105
100
_messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
106
101
107
102
if ( startTimestamp > 0 )
108
103
{
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 ,
116
106
destination ,
117
107
_messagingOperationSystemTag , _sendOperationType , _publishOperationName ) ;
118
108
}
@@ -123,20 +113,18 @@ public void ReportMessageDeliverSuccess(IMetricsReporter.ConsumerContext context
123
113
var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
124
114
var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
125
115
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 ,
127
118
_processOperationType , _deliverOperationName ) ;
128
- if ( startTimestamp > 0 )
119
+ if ( startTimestamp <= 0 )
129
120
{
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 ;
139
122
}
123
+
124
+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
125
+ _messagingProcessDuration . Record ( duration . TotalSeconds , serverAddress , serverPort ,
126
+ destination ,
127
+ _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
140
128
}
141
129
142
130
public void ReportMessageDeliverFailure ( IMetricsReporter . ConsumerContext context , long startTimestamp ,
@@ -146,28 +134,19 @@ public void ReportMessageDeliverFailure(IMetricsReporter.ConsumerContext context
146
134
var serverAddress = new KeyValuePair < string , object ? > ( ServerAddress , context . ServerAddress ) ;
147
135
var serverPort = new KeyValuePair < string , object ? > ( ServerPort , context . ServerPort ) ;
148
136
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 ,
150
138
_messagingOperationSystemTag ,
151
139
_processOperationType , _deliverOperationName ) ;
152
- if ( startTimestamp > 0 )
140
+ if ( startTimestamp <= 0 )
153
141
{
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 ;
163
143
}
144
+
145
+ var duration = Stopwatch . GetElapsedTime ( startTimestamp ) ;
146
+ _messagingProcessDuration . Record ( duration . TotalSeconds , errorType , serverAddress , serverPort ,
147
+ destination ,
148
+ _messagingOperationSystemTag , _processOperationType , _deliverOperationName ) ;
164
149
}
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
171
150
}
172
151
#else
173
152
#endif
0 commit comments