From 45ec88d10aaa21cfbd44bf40308595a10b45f961 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 09:20:09 -0700 Subject: [PATCH 1/8] calculate timesinceenqueued --- .../src/Customizations/Models/RequestData.cs | 4 + .../src/Internals/TraceHelper.cs | 50 ++++++++++ .../RequestDataTests.cs | 95 ++++++++++++++++++- 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs index 7ad95f2c162f9..91e4b936ff248 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs @@ -41,6 +41,10 @@ public RequestData(int version, Activity activity, ref ActivityTagsProcessor act TraceHelper.AddActivityLinksToProperties(activity, ref activityTagsProcessor.UnMappedTags); TraceHelper.AddPropertiesToTelemetry(Properties, ref activityTagsProcessor.UnMappedTags); + if (activity.Kind == ActivityKind.Consumer) + { + TraceHelper.AddEnqueuedTimeToMeasurements(activity, Measurements); + } } internal static bool IsSuccess(Activity activity, string? responseCode, OperationType operationType) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index 5ac4ec237db3e..d17afeb1100b9 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -300,5 +300,55 @@ private static void AddTelemetryFromActivityEvents(Activity activity, TelemetryI BaseData = exceptionData, }; } + + internal static void AddEnqueuedTimeToMeasurements(Activity activity, IDictionary measurements) + { + if (activity.Links != null && activity.Links.Any()) + { + if (TryGetAverageTimeInQueueForBatch(activity.Links, activity.StartTimeUtc, out long enqueuedTime)) + { + measurements["timeSinceEnqueued"] = enqueuedTime; + } + } + } + + private static bool TryGetAverageTimeInQueueForBatch(IEnumerable links, DateTimeOffset requestStartTime, out long avgTimeInQueue) + { + avgTimeInQueue = 0; + var linksCount = 0; + long startEpochTime = requestStartTime.ToUnixTimeMilliseconds(); + foreach (ActivityLink link in links) + { + if (!TryGetEnqueuedTime(link, out var msgEnqueuedTime)) + { + // instrumentation does not consistently report enqueued time, ignoring whole span + return false; + } + + avgTimeInQueue += Math.Max(startEpochTime - msgEnqueuedTime, 0); + + linksCount++; + } + + avgTimeInQueue /= linksCount; + return true; + } + + private static bool TryGetEnqueuedTime(ActivityLink link, out long enqueuedTime) + { + enqueuedTime = 0; + if (link.Tags != null && link.Tags.Any()) + { + foreach (var attribute in link.Tags) + { + if (attribute.Key == "enqueuedTime") + { + return long.TryParse(attribute.Value?.ToString(), out enqueuedTime); + } + } + } + + return false; + } } } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs index 7f0bbd874a7c2..21bc14089745c 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs @@ -2,9 +2,9 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; - using Azure.Monitor.OpenTelemetry.Exporter.Internals; using Azure.Monitor.OpenTelemetry.Exporter.Models; @@ -133,5 +133,98 @@ public void RequestDataContainsAzureNamespace() Assert.True(activityTagsProcessor.HasAzureNamespace); Assert.Equal("DemoAzureResource", requestData.Properties["az.namespace"]); } + + [Fact] + public void RequestDataContainsTimeSinceEnqueuedForConsumerSpans() + { + using ActivitySource activitySource = new ActivitySource(ActivitySourceName); + List? links = new List(); + long enqueued0 = DateTimeOffset.UtcNow.AddMilliseconds(-100).ToUnixTimeMilliseconds(); + long enqueued1 = DateTimeOffset.UtcNow.AddMilliseconds(-200).ToUnixTimeMilliseconds(); + long enqueued2 = DateTimeOffset.UtcNow.AddMilliseconds(-300).ToUnixTimeMilliseconds(); + + links.Add(AddActivityLink(enqueued0)); + links.Add(AddActivityLink(enqueued1)); + links.Add(AddActivityLink(enqueued2)); + + using var activity = activitySource.StartActivity("Activity", ActivityKind.Consumer, null, null, links); + Assert.NotNull(activity); + var activityTagsProcessor = TraceHelper.EnumerateActivityTags(activity); + + var requestData = new RequestData(2, activity, ref activityTagsProcessor); + + DateTimeOffset startTime = activity.StartTimeUtc; + var startTimeEpoch = startTime.ToUnixTimeMilliseconds(); + + long expectedTimeInQueue = ((startTimeEpoch - enqueued0) + + (startTimeEpoch - enqueued1) + + (startTimeEpoch - enqueued2)) / 3; // avg diff with request start time across links + + Assert.True(requestData.Measurements.TryGetValue("timeSinceEnqueued", out var timeInQueue)); + + Assert.Equal(expectedTimeInQueue, timeInQueue); + } + + [Fact] + public void RequestDataTimeSinceEnqueuedNegative() + { + using ActivitySource activitySource = new ActivitySource(ActivitySourceName); + List? links = new List(); + long enqueued0 = DateTimeOffset.UtcNow.AddMilliseconds(-100).ToUnixTimeMilliseconds(); + long enqueued1 = DateTimeOffset.UtcNow.AddMilliseconds(-200).ToUnixTimeMilliseconds(); + long enqueued2 = DateTimeOffset.UtcNow.AddMilliseconds(300).ToUnixTimeMilliseconds(); // ignored + + links.Add(AddActivityLink(enqueued0)); + links.Add(AddActivityLink(enqueued1)); + links.Add(AddActivityLink(enqueued2)); + + using var activity = activitySource.StartActivity("Activity", ActivityKind.Consumer, null, null, links); + Assert.NotNull(activity); + var activityTagsProcessor = TraceHelper.EnumerateActivityTags(activity); + + var requestData = new RequestData(2, activity, ref activityTagsProcessor); + + DateTimeOffset startTime = activity.StartTimeUtc; + var startTimeEpoch = startTime.ToUnixTimeMilliseconds(); + + long expectedTimeInQueue = ((startTimeEpoch - enqueued0) + + (startTimeEpoch - enqueued1)) / 3; // avg diff with request start time across links + + Assert.True(requestData.Measurements.TryGetValue("timeSinceEnqueued", out var timeInQueue)); + + Assert.Equal(expectedTimeInQueue, timeInQueue); + } + + [Fact] + public void RequestDataTimeSinceEnqueuedInvalidEmqueuedTime() + { + using ActivitySource activitySource = new ActivitySource(ActivitySourceName); + List? links = new List(); + + ActivityTagsCollection tags = new ActivityTagsCollection(); + tags.Add("enqueuedTime", "Invalid"); + var link = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, null), tags); + links.Add(link); + + using var activity = activitySource.StartActivity("Activity", ActivityKind.Consumer, null, null, links); + Assert.NotNull(activity); + var activityTagsProcessor = TraceHelper.EnumerateActivityTags(activity); + + var requestData = new RequestData(2, activity, ref activityTagsProcessor); + + DateTimeOffset startTime = activity.StartTimeUtc; + var startTimeEpoch = startTime.ToUnixTimeMilliseconds(); + + Assert.False(requestData.Measurements.TryGetValue("timeSinceEnqueued", out var timeInQueue)); + } + + private ActivityLink AddActivityLink(long enqueuedTime) + { + ActivityTagsCollection tags = new ActivityTagsCollection(); + tags.Add("enqueuedTime", enqueuedTime.ToString()); + var link = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, null), tags); + + return link; + } } } From 4c36bddba33bf6a32f9c854dd298fdb66a763cf1 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 09:35:36 -0700 Subject: [PATCH 2/8] refactor --- .../RequestDataTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs index 21bc14089745c..a1a236801e95a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/RequestDataTests.cs @@ -220,8 +220,10 @@ public void RequestDataTimeSinceEnqueuedInvalidEmqueuedTime() private ActivityLink AddActivityLink(long enqueuedTime) { - ActivityTagsCollection tags = new ActivityTagsCollection(); - tags.Add("enqueuedTime", enqueuedTime.ToString()); + ActivityTagsCollection tags = new ActivityTagsCollection + { + { "enqueuedTime", enqueuedTime.ToString() } + }; var link = new ActivityLink(new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.None, null), tags); return link; From 1b8e873374df6de3c542f87bfc8b427feb7ee2fc Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 10:03:13 -0700 Subject: [PATCH 3/8] Update sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs Co-authored-by: Rajkumar Rangaraj --- .../src/Internals/TraceHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index d17afeb1100b9..f57b796e3283b 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -317,7 +317,7 @@ private static bool TryGetAverageTimeInQueueForBatch(IEnumerable l avgTimeInQueue = 0; var linksCount = 0; long startEpochTime = requestStartTime.ToUnixTimeMilliseconds(); - foreach (ActivityLink link in links) + foreach (ref readonly var link in activity.EnumerateLinks()) { if (!TryGetEnqueuedTime(link, out var msgEnqueuedTime)) { From bc098563dd5fc1c32842cb3c1f109e6095f7c708 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 11:14:34 -0700 Subject: [PATCH 4/8] address pr comments --- .../src/Internals/TraceHelper.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index f57b796e3283b..9c7c028d14717 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -305,18 +305,19 @@ internal static void AddEnqueuedTimeToMeasurements(Activity activity, IDictionar { if (activity.Links != null && activity.Links.Any()) { - if (TryGetAverageTimeInQueueForBatch(activity.Links, activity.StartTimeUtc, out long enqueuedTime)) + if (TryGetAverageTimeInQueueForBatch(activity, out long enqueuedTime)) { measurements["timeSinceEnqueued"] = enqueuedTime; } } } - private static bool TryGetAverageTimeInQueueForBatch(IEnumerable links, DateTimeOffset requestStartTime, out long avgTimeInQueue) + private static bool TryGetAverageTimeInQueueForBatch(Activity activity, out long avgTimeInQueue) { avgTimeInQueue = 0; var linksCount = 0; - long startEpochTime = requestStartTime.ToUnixTimeMilliseconds(); + DateTimeOffset startTime = activity.StartTimeUtc; + long startEpochTime = startTime.ToUnixTimeMilliseconds(); foreach (ref readonly var link in activity.EnumerateLinks()) { if (!TryGetEnqueuedTime(link, out var msgEnqueuedTime)) From 1f4b2e3b2f782e35eb0de0ce3831f8544138aee1 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 12:11:11 -0700 Subject: [PATCH 5/8] address pr comments --- .../src/Customizations/Models/RequestData.cs | 10 +++- .../src/Internals/TraceHelper.cs | 56 +++++++++++++++++-- .../TraceHelperTests.cs | 36 ++++++------ 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs index 91e4b936ff248..389e3469c7501 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs @@ -39,12 +39,16 @@ public RequestData(int version, Activity activity, ref ActivityTagsProcessor act Properties = new ChangeTrackingDictionary(); Measurements = new ChangeTrackingDictionary(); - TraceHelper.AddActivityLinksToProperties(activity, ref activityTagsProcessor.UnMappedTags); - TraceHelper.AddPropertiesToTelemetry(Properties, ref activityTagsProcessor.UnMappedTags); if (activity.Kind == ActivityKind.Consumer) { - TraceHelper.AddEnqueuedTimeToMeasurements(activity, Measurements); + TraceHelper.AddEnqueuedTimeToMeasurementsAndAddLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags); + } + else + { + TraceHelper.AddActivityLinksToProperties(activity, ref activityTagsProcessor.UnMappedTags); } + + TraceHelper.AddPropertiesToTelemetry(Properties, ref activityTagsProcessor.UnMappedTags); } internal static bool IsSuccess(Activity activity, string? responseCode, OperationType operationType) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index 9c7c028d14717..f8c101c6625c7 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -301,37 +301,83 @@ private static void AddTelemetryFromActivityEvents(Activity activity, TelemetryI }; } - internal static void AddEnqueuedTimeToMeasurements(Activity activity, IDictionary measurements) + internal static void AddEnqueuedTimeToMeasurementsAndAddLinksToProperties(Activity activity, IDictionary measurements, ref AzMonList UnMappedTags) { if (activity.Links != null && activity.Links.Any()) { - if (TryGetAverageTimeInQueueForBatch(activity, out long enqueuedTime)) + if (TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(activity, ref UnMappedTags,out long enqueuedTime)) { measurements["timeSinceEnqueued"] = enqueuedTime; } } } - private static bool TryGetAverageTimeInQueueForBatch(Activity activity, out long avgTimeInQueue) + private static bool TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(Activity activity, ref AzMonList UnMappedTags, out long avgTimeInQueue) { avgTimeInQueue = 0; var linksCount = 0; DateTimeOffset startTime = activity.StartTimeUtc; long startEpochTime = startTime.ToUnixTimeMilliseconds(); + bool isEnqueuedTimeCalculated = true; + + string msLinks = "_MS.links"; + var linksJson = new StringBuilder(); + linksJson.Append('['); foreach (ref readonly var link in activity.EnumerateLinks()) { if (!TryGetEnqueuedTime(link, out var msgEnqueuedTime)) { // instrumentation does not consistently report enqueued time, ignoring whole span - return false; + isEnqueuedTimeCalculated = false; } avgTimeInQueue += Math.Max(startEpochTime - msgEnqueuedTime, 0); linksCount++; + + if (linksCount <= MaxlinksAllowed) + { + linksJson + .Append('{') + .Append("\"operation_Id\":") + .Append('\"') + .Append(link.Context.TraceId.ToHexString()) + .Append('\"') + .Append(','); + linksJson + .Append("\"id\":") + .Append('\"') + .Append(link.Context.SpanId.ToHexString()) + .Append('\"'); + linksJson.Append("},"); + } + } + + if (linksJson.Length > 0) + { + // trim trailing comma - json does not support it + linksJson.Remove(linksJson.Length - 1, 1); + } + + linksJson.Append(']'); + + AzMonList.Add(ref UnMappedTags, new KeyValuePair(msLinks, linksJson.ToString())); + + if (MaxlinksAllowed < linksCount) + { + AzureMonitorExporterEventSource.Log.WriteInformational("ActivityLinksIgnored", $"Max count of {MaxlinksAllowed} has reached."); + } + + if (isEnqueuedTimeCalculated) + { + avgTimeInQueue /= linksCount; + } + else + { + avgTimeInQueue = 0; + return false; } - avgTimeInQueue /= linksCount; return true; } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperTests.cs index ec78da829f40f..5354ce6688b99 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/TraceHelperTests.cs @@ -39,14 +39,15 @@ static TraceHelperTests() } [Theory] - [InlineData("RequestData")] - [InlineData("RemoteDependencyData")] - public void PropertiesDoesNotContainMSLinksWhenActivityHasNoLinks(string telemetryType) + [InlineData("RequestData", ActivityKind.Server)] + [InlineData("RequestData", ActivityKind.Consumer)] + [InlineData("RemoteDependencyData", ActivityKind.Client)] + public void PropertiesDoesNotContainMSLinksWhenActivityHasNoLinks(string telemetryType, ActivityKind kind) { using ActivitySource activitySource = new ActivitySource(ActivitySourceName); using var activity = activitySource.StartActivity( ActivityName, - ActivityKind.Client, + kind, parentContext: default, startTime: DateTime.UtcNow); @@ -67,9 +68,10 @@ public void PropertiesDoesNotContainMSLinksWhenActivityHasNoLinks(string telemet } [Theory] - [InlineData("RequestData")] - [InlineData("RemoteDependencyData")] - public void PropertiesContainMSLinksWhenActivityHasLinks(string telemetryType) + [InlineData("RequestData", ActivityKind.Server)] + [InlineData("RequestData", ActivityKind.Consumer)] + [InlineData("RemoteDependencyData", ActivityKind.Client)] + public void PropertiesContainMSLinksWhenActivityHasLinks(string telemetryType, ActivityKind kind) { using ActivitySource activitySource = new ActivitySource(ActivitySourceName); ActivityLink activityLink = new ActivityLink(new ActivityContext( @@ -82,7 +84,7 @@ public void PropertiesContainMSLinksWhenActivityHasLinks(string telemetryType) using var activity = activitySource.StartActivity( ActivityName, - ActivityKind.Client, + kind, parentContext: default, null, links, @@ -109,9 +111,10 @@ public void PropertiesContainMSLinksWhenActivityHasLinks(string telemetryType) } [Theory] - [InlineData("RequestData")] - [InlineData("RemoteDependencyData")] - public void LinksAreTruncatedWhenCannotFitInMaxLength(string telemetryType) + [InlineData("RequestData", ActivityKind.Server)] + [InlineData("RequestData", ActivityKind.Consumer)] + [InlineData("RemoteDependencyData", ActivityKind.Client)] + public void LinksAreTruncatedWhenCannotFitInMaxLength(string telemetryType, ActivityKind kind) { using ActivitySource activitySource = new ActivitySource(ActivitySourceName); List links = new List(); @@ -133,7 +136,7 @@ public void LinksAreTruncatedWhenCannotFitInMaxLength(string telemetryType) using var activity = activitySource.StartActivity( ActivityName, - ActivityKind.Client, + kind, parentContext: default, null, links, @@ -168,9 +171,10 @@ public void LinksAreTruncatedWhenCannotFitInMaxLength(string telemetryType) } [Theory] - [InlineData("RequestData")] - [InlineData("RemoteDependencyData")] - public void LinksAreNotTruncatedWhenCanBeFitInMaxLength(string telemetryType) + [InlineData("RequestData", ActivityKind.Server)] + [InlineData("RequestData", ActivityKind.Consumer)] + [InlineData("RemoteDependencyData", ActivityKind.Client)] + public void LinksAreNotTruncatedWhenCanBeFitInMaxLength(string telemetryType, ActivityKind kind) { using ActivitySource activitySource = new ActivitySource(ActivitySourceName); List links = new List(); @@ -186,7 +190,7 @@ public void LinksAreNotTruncatedWhenCanBeFitInMaxLength(string telemetryType) using var activity = activitySource.StartActivity( ActivityName, - ActivityKind.Client, + kind, parentContext: default, null, links, From 5d1978ab9501c3474b3042e8ef70b45097414a05 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 30 May 2023 12:12:54 -0700 Subject: [PATCH 6/8] align --- .../src/Internals/TraceHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index f8c101c6625c7..ab1b933dce0d6 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -305,7 +305,7 @@ internal static void AddEnqueuedTimeToMeasurementsAndAddLinksToProperties(Activi { if (activity.Links != null && activity.Links.Any()) { - if (TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(activity, ref UnMappedTags,out long enqueuedTime)) + if (TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(activity, ref UnMappedTags, out long enqueuedTime)) { measurements["timeSinceEnqueued"] = enqueuedTime; } From 3618a3653d04a5228540e2a634a8035a439ac5c2 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 31 May 2023 08:37:44 -0700 Subject: [PATCH 7/8] Address pr comments --- .../src/Customizations/Models/RequestData.cs | 2 +- .../src/Internals/TraceHelper.cs | 76 +++++++++---------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs index 389e3469c7501..b2cef5b60c026 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs @@ -41,7 +41,7 @@ public RequestData(int version, Activity activity, ref ActivityTagsProcessor act if (activity.Kind == ActivityKind.Consumer) { - TraceHelper.AddEnqueuedTimeToMeasurementsAndAddLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags); + TraceHelper.AddEnqueuedTimeToMeasurrementsAndLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags); } else { diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index ab1b933dce0d6..38845c4d2f7d4 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -105,20 +105,7 @@ internal static void AddActivityLinksToProperties(Activity activity, ref AzMonLi linksJson.Append('['); foreach (ref readonly var link in activity.EnumerateLinks()) { - linksJson - .Append('{') - .Append("\"operation_Id\":") - .Append('\"') - .Append(link.Context.TraceId.ToHexString()) - .Append('\"') - .Append(','); - linksJson - .Append("\"id\":") - .Append('\"') - .Append(link.Context.SpanId.ToHexString()) - .Append('\"'); - linksJson.Append("},"); - + AddContextToMSLinks(linksJson, link); maxLinks--; if (maxLinks == 0) { @@ -301,18 +288,18 @@ private static void AddTelemetryFromActivityEvents(Activity activity, TelemetryI }; } - internal static void AddEnqueuedTimeToMeasurementsAndAddLinksToProperties(Activity activity, IDictionary measurements, ref AzMonList UnMappedTags) + internal static void AddEnqueuedTimeToMeasurrementsAndLinksToProperties(Activity activity, IDictionary measurements, ref AzMonList UnMappedTags) { if (activity.Links != null && activity.Links.Any()) { - if (TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(activity, ref UnMappedTags, out long enqueuedTime)) + if (TryGetAverageQueueTimeWithLinks(activity, ref UnMappedTags, out long enqueuedTime)) { measurements["timeSinceEnqueued"] = enqueuedTime; } } } - private static bool TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(Activity activity, ref AzMonList UnMappedTags, out long avgTimeInQueue) + private static bool TryGetAverageQueueTimeWithLinks(Activity activity, ref AzMonList UnMappedTags, out long avgTimeInQueue) { avgTimeInQueue = 0; var linksCount = 0; @@ -325,31 +312,22 @@ private static bool TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(Acti linksJson.Append('['); foreach (ref readonly var link in activity.EnumerateLinks()) { - if (!TryGetEnqueuedTime(link, out var msgEnqueuedTime)) + long msgEnqueuedTime = 0; + if (isEnqueuedTimeCalculated && !TryGetEnqueuedTime(link, out msgEnqueuedTime)) { // instrumentation does not consistently report enqueued time, ignoring whole span isEnqueuedTimeCalculated = false; } - - avgTimeInQueue += Math.Max(startEpochTime - msgEnqueuedTime, 0); + if (isEnqueuedTimeCalculated) + { + avgTimeInQueue += Math.Max(startEpochTime - msgEnqueuedTime, 0); + } linksCount++; if (linksCount <= MaxlinksAllowed) { - linksJson - .Append('{') - .Append("\"operation_Id\":") - .Append('\"') - .Append(link.Context.TraceId.ToHexString()) - .Append('\"') - .Append(','); - linksJson - .Append("\"id\":") - .Append('\"') - .Append(link.Context.SpanId.ToHexString()) - .Append('\"'); - linksJson.Append("},"); + AddContextToMSLinks(linksJson, link); } } @@ -358,11 +336,8 @@ private static bool TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(Acti // trim trailing comma - json does not support it linksJson.Remove(linksJson.Length - 1, 1); } - linksJson.Append(']'); - AzMonList.Add(ref UnMappedTags, new KeyValuePair(msLinks, linksJson.ToString())); - if (MaxlinksAllowed < linksCount) { AzureMonitorExporterEventSource.Log.WriteInformational("ActivityLinksIgnored", $"Max count of {MaxlinksAllowed} has reached."); @@ -384,18 +359,35 @@ private static bool TryGetAverageTimeInQueueForBatchAndAddLinksToProperties(Acti private static bool TryGetEnqueuedTime(ActivityLink link, out long enqueuedTime) { enqueuedTime = 0; - if (link.Tags != null && link.Tags.Any()) + + foreach (ref readonly var attribute in link.EnumerateTagObjects()) { - foreach (var attribute in link.Tags) + if (attribute.Key == "enqueuedTime") { - if (attribute.Key == "enqueuedTime") - { - return long.TryParse(attribute.Value?.ToString(), out enqueuedTime); - } + return long.TryParse(attribute.Value?.ToString(), out enqueuedTime); } } return false; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void AddContextToMSLinks(StringBuilder linksJson, ActivityLink link) + { + linksJson + .Append('{') + .Append("\"operation_Id\":") + .Append('\"') + .Append(link.Context.TraceId.ToHexString()) + .Append('\"') + .Append(','); + linksJson + .Append("\"id\":") + .Append('\"') + .Append(link.Context.SpanId.ToHexString()) + .Append('\"'); + linksJson + .Append("},"); + } } } From f87620d558e4a4c38832803b95d07dcfa8d32238 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 31 May 2023 10:26:47 -0700 Subject: [PATCH 8/8] spelling --- .../src/Customizations/Models/RequestData.cs | 2 +- .../src/Internals/TraceHelper.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs index b2cef5b60c026..2e27d7d7e133e 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs @@ -41,7 +41,7 @@ public RequestData(int version, Activity activity, ref ActivityTagsProcessor act if (activity.Kind == ActivityKind.Consumer) { - TraceHelper.AddEnqueuedTimeToMeasurrementsAndLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags); + TraceHelper.AddEnqueuedTimeToMeasurementsAndLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags); } else { diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs index 38845c4d2f7d4..f3ca5c8c7a0ad 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TraceHelper.cs @@ -288,7 +288,7 @@ private static void AddTelemetryFromActivityEvents(Activity activity, TelemetryI }; } - internal static void AddEnqueuedTimeToMeasurrementsAndLinksToProperties(Activity activity, IDictionary measurements, ref AzMonList UnMappedTags) + internal static void AddEnqueuedTimeToMeasurementsAndLinksToProperties(Activity activity, IDictionary measurements, ref AzMonList UnMappedTags) { if (activity.Links != null && activity.Links.Any()) {