From 1567989f94fa760c5be6fbea5568e57f7681eb79 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Thu, 13 Jul 2023 12:22:48 -0300 Subject: [PATCH] Only set activity custom property when sampling AllData This optimization means we don't pay the extra allocation and cost unnecessarily unless someone is actually requesting all data from activities. See https://github.com/open-telemetry/opentelemetry-dotnet/issues/1397 --- src/Merq.Core/Telemetry.cs | 7 +++++-- src/Merq.Tests/MessageBusSpec.cs | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Merq.Core/Telemetry.cs b/src/Merq.Core/Telemetry.cs index f763993..8b2caac 100644 --- a/src/Merq.Core/Telemetry.cs +++ b/src/Merq.Core/Telemetry.cs @@ -71,8 +71,11 @@ static Telemetry() ?.SetTag("messaging.protocol.name", type.Assembly.GetName().Name) ?.SetTag("messaging.protocol.version", type.Assembly.GetName().Version?.ToString() ?? "unknown"); - if (property != null && value != null) - activity?.SetCustomProperty(property, value); + if (property != null && value != null && + // Additional optimization so we don't incur allocation of activity custom props storage + // unless someone is actually requesting the data. See https://github.com/open-telemetry/opentelemetry-dotnet/issues/1397 + activity?.IsAllDataRequested == true) + activity.SetCustomProperty(property, value); activity?.Start(); diff --git a/src/Merq.Tests/MessageBusSpec.cs b/src/Merq.Tests/MessageBusSpec.cs index eac64f3..a7b287d 100644 --- a/src/Merq.Tests/MessageBusSpec.cs +++ b/src/Merq.Tests/MessageBusSpec.cs @@ -478,7 +478,7 @@ public void when_executing_can_access_command_from_activity_started() if (a.GetTagItem("messaging.destination.name") is "Merq.CommandWithResult") activity = a; }, - Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData, + Sample = (ref ActivityCreationOptions _) => _.Source.Name == "Merq" ? ActivitySamplingResult.AllData : ActivitySamplingResult.None, ShouldListenTo = source => source.Name == "Merq", }; @@ -497,7 +497,7 @@ public void when_execute_throws_activity_has_error_status() using var listener = new ActivityListener { ActivityStarted = x => activity = x, - Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData, + Sample = (ref ActivityCreationOptions _) => _.Source.Name == "Merq" ? ActivitySamplingResult.AllData : ActivitySamplingResult.None, ShouldListenTo = source => source.Name == "Merq", };