From ba04ac0d9d809571ac02aceaf81841ce6e4bc154 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 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs index a3905c0f1fa..f894519ea98 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),