Skip to content

Commit

Permalink
Only set activity custom property when sampling AllData
Browse files Browse the repository at this point in the history
This optimization means we don't pay the extra allocation and cost unnecessarily unless someone is actually requesting all data from activities.

See open-telemetry/opentelemetry-dotnet#1397
  • Loading branch information
kzu committed Jul 13, 2023
1 parent 7f7cd7b commit 1567989
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Merq.Core/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/Merq.Tests/MessageBusSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityContext> _) => ActivitySamplingResult.AllData,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => _.Source.Name == "Merq" ? ActivitySamplingResult.AllData : ActivitySamplingResult.None,
ShouldListenTo = source => source.Name == "Merq",
};

Expand All @@ -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<ActivityContext> _) => ActivitySamplingResult.AllData,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => _.Source.Name == "Merq" ? ActivitySamplingResult.AllData : ActivitySamplingResult.None,
ShouldListenTo = source => source.Name == "Merq",
};

Expand Down

0 comments on commit 1567989

Please sign in to comment.