From 48c354afa88fde4664a70d9c0fc8c20c7e035bf9 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Mon, 18 Sep 2023 09:53:51 -0400 Subject: [PATCH] activity duration or stopwatch --- .../HttpWebRequestActivitySource.netfx.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs index a3905c0f1fa..66de2bada89 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs @@ -404,10 +404,24 @@ private static void ProcessResult(IAsyncResult asyncResult, AsyncCallback asyncC if (HttpClientDuration.Enabled) { + double duration = 0; + + if (activity != null) + { + duration = activity.Duration.TotalMilliseconds; + } + else + { + var endTimestamp = Stopwatch.GetTimestamp(); + var durationS = (endTimestamp - startTimestamp) / Stopwatch.Frequency; + var durationMs = durationS * 1000; + duration = durationMs; + } + if (httpStatusCode.HasValue) { HttpClientDuration.Record( - activity.Duration.TotalMilliseconds, + duration, new(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion)), new(SemanticConventions.AttributeHttpMethod, request.Method), new(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme), @@ -419,7 +433,7 @@ private static void ProcessResult(IAsyncResult asyncResult, AsyncCallback asyncC else { HttpClientDuration.Record( - activity.Duration.TotalMilliseconds, + duration, new(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion)), new(SemanticConventions.AttributeHttpMethod, request.Method), new(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme),