33using System . Web ;
44#endif
55using System ;
6+ using System . Collections . Concurrent ;
67using System . Collections . Generic ;
8+ using System . Diagnostics ;
79using System . Linq ;
810using System . Net ;
911using System . Net . Http ;
@@ -19,16 +21,21 @@ public class ApiClient : IApiClient
1921 private readonly ILogger _log = LogProvider . GetLogger ( typeof ( ApiClient ) ) ;
2022#endif
2123
24+ private static string sdkTelemetryHeader = "cko-sdk-telemetry" ;
25+ private static int maxCountInTelemetryQueue = 10 ;
2226 private readonly HttpClient _httpClient ;
2327 private readonly Uri _baseUri ;
2428 private readonly ISerializer _serializer = new JsonSerializer ( ) ;
29+ private ConcurrentQueue < RequestMetrics > requestMetricsQueue = new ConcurrentQueue < RequestMetrics > ( ) ;
30+ private readonly bool _enableTelemetry ;
2531
26- public ApiClient ( IHttpClientFactory httpClientFactory , Uri baseUri )
32+ public ApiClient ( IHttpClientFactory httpClientFactory , Uri baseUri , bool enableTelemetry )
2733 {
2834 CheckoutUtils . ValidateParams ( "httpClientFactory" , httpClientFactory , "baseUri" , baseUri ) ;
2935 var httpClient = httpClientFactory . CreateClient ( ) ;
3036 _baseUri = baseUri ;
3137 _httpClient = httpClient ;
38+ _enableTelemetry = enableTelemetry ;
3239 }
3340
3441 public async Task < TResult > Get < TResult > (
@@ -170,7 +177,7 @@ public async Task<TResult> Query<TResult>(
170177 $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}" ) ) ;
171178#endif
172179
173- path = $ "{ path } ?{ queryString } ";
180+ path = $ "{ path } ?{ queryString } ";
174181 }
175182 }
176183
@@ -246,8 +253,37 @@ private async Task<HttpResponseMessage> Invoke(
246253 {
247254 httpRequest . Headers . Add ( "Cko-Idempotency-Key" , idempotencyKey ) ;
248255 }
256+
257+ if ( _enableTelemetry )
258+ {
259+ var currentRequestId = Guid . NewGuid ( ) . ToString ( ) ;
260+ if ( requestMetricsQueue . TryDequeue ( out var lastRequestMetric ) )
261+ {
262+ lastRequestMetric . RequestId = currentRequestId ;
263+ httpRequest . Headers . TryAddWithoutValidation ( sdkTelemetryHeader , _serializer . Serialize ( lastRequestMetric ) ) ;
264+ }
249265
250- return await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
266+ Stopwatch stopwatch = new Stopwatch ( ) ;
267+ stopwatch . Start ( ) ;
268+ var result = await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
269+ stopwatch . Stop ( ) ;
270+
271+ if ( requestMetricsQueue . Count < maxCountInTelemetryQueue )
272+ {
273+ lastRequestMetric . PrevRequestDuration = stopwatch . ElapsedMilliseconds ;
274+ requestMetricsQueue . Enqueue ( new RequestMetrics ( )
275+ {
276+ PrevRequestDuration = stopwatch . ElapsedMilliseconds ,
277+ PrevRequestId = currentRequestId
278+ } ) ;
279+ }
280+
281+ return result ;
282+ }
283+ else
284+ {
285+ return await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
286+ }
251287 }
252288
253289 private async Task ValidateResponseAsync ( HttpResponseMessage httpResponse )
0 commit comments