7
7
using StackExchange . Redis ;
8
8
using Google . Protobuf ;
9
9
using Microsoft . Extensions . Logging ;
10
+ using System . Diagnostics . Metrics ;
11
+ using System . Diagnostics ;
10
12
11
13
namespace cartservice . cartstore ;
12
14
@@ -23,6 +25,20 @@ public class ValkeyCartStore : ICartStore
23
25
private readonly byte [ ] _emptyCartBytes ;
24
26
private readonly string _connectionString ;
25
27
28
+ private static readonly ActivitySource CartActivitySource = new ( "OpenTelemetry.Demo.Cart" ) ;
29
+ private static readonly Meter CartMeter = new Meter ( "OpenTelemetry.Demo.Cart" ) ;
30
+ private static readonly Histogram < long > addItemHistogram = CartMeter . CreateHistogram < long > (
31
+ "app.cart.add_item.latency" ,
32
+ advice : new InstrumentAdvice < long >
33
+ {
34
+ HistogramBucketBoundaries = [ 500000 , 600000 , 700000 , 800000 , 900000 , 1000000 , 1100000 ]
35
+ } ) ;
36
+ private static readonly Histogram < long > getCartHistogram = CartMeter . CreateHistogram < long > (
37
+ "app.cart.get_cart.latency" ,
38
+ advice : new InstrumentAdvice < long >
39
+ {
40
+ HistogramBucketBoundaries = [ 300000 , 400000 , 500000 , 600000 , 700000 , 800000 , 900000 ]
41
+ } ) ;
26
42
private readonly ConfigurationOptions _redisConnectionOptions ;
27
43
28
44
public ValkeyCartStore ( ILogger < ValkeyCartStore > logger , string valkeyAddress )
@@ -105,6 +121,7 @@ private void EnsureRedisConnected()
105
121
106
122
public async Task AddItemAsync ( string userId , string productId , int quantity )
107
123
{
124
+ var stopwatch = Stopwatch . StartNew ( ) ;
108
125
_logger . LogInformation ( "AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}" , userId , productId , quantity ) ;
109
126
110
127
try
@@ -146,6 +163,10 @@ public async Task AddItemAsync(string userId, string productId, int quantity)
146
163
{
147
164
throw new RpcException ( new Status ( StatusCode . FailedPrecondition , $ "Can't access cart storage. { ex } ") ) ;
148
165
}
166
+ finally
167
+ {
168
+ addItemHistogram . Record ( stopwatch . ElapsedTicks ) ;
169
+ }
149
170
}
150
171
151
172
public async Task EmptyCartAsync ( string userId )
@@ -169,6 +190,7 @@ public async Task EmptyCartAsync(string userId)
169
190
170
191
public async Task < Oteldemo . Cart > GetCartAsync ( string userId )
171
192
{
193
+ var stopwatch = Stopwatch . StartNew ( ) ;
172
194
_logger . LogInformation ( "GetCartAsync called with userId={userId}" , userId ) ;
173
195
174
196
try
@@ -192,6 +214,10 @@ public async Task EmptyCartAsync(string userId)
192
214
{
193
215
throw new RpcException ( new Status ( StatusCode . FailedPrecondition , $ "Can't access cart storage. { ex } ") ) ;
194
216
}
217
+ finally
218
+ {
219
+ getCartHistogram . Record ( stopwatch . ElapsedTicks ) ;
220
+ }
195
221
}
196
222
197
223
public bool Ping ( )
0 commit comments