From c4b051354c4c21ae5430abc4e23837ddab2da627 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Mon, 19 Aug 2024 11:02:50 +0200 Subject: [PATCH] Ensure we capture baggage when capturing Error's during unsampled transactions --- src/Elastic.Apm/Model/Error.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Elastic.Apm/Model/Error.cs b/src/Elastic.Apm/Model/Error.cs index be282f45d..f2c15f8db 100644 --- a/src/Elastic.Apm/Model/Error.cs +++ b/src/Elastic.Apm/Model/Error.cs @@ -44,7 +44,7 @@ private Error(Transaction transaction, string parentId, IApmLogger loggerArg, Di ParentId = parentId; - if (transaction != null && transaction.IsSampled) + if (transaction is { IsSampled: true }) { Context = transaction.Context.DeepCopy(); @@ -55,7 +55,7 @@ private Error(Transaction transaction, string parentId, IApmLogger loggerArg, Di } } - CheckAndCaptureBaggage(); + CheckAndCaptureBaggage(transaction); IApmLogger logger = loggerArg?.Scoped($"{nameof(Error)}.{Id}"); logger.Trace() @@ -63,11 +63,15 @@ private Error(Transaction transaction, string parentId, IApmLogger loggerArg, Di this, TimeUtils.FormatTimestampForLog(Timestamp), Timestamp); } - private void CheckAndCaptureBaggage() + private void CheckAndCaptureBaggage(Transaction transaction) { if (Activity.Current == null || !Activity.Current.Baggage.Any()) return; + //if context was not set prior we set it now to ensure we capture baggage for errors + //occuring during unsampled transactions + Context ??= transaction.Context.DeepCopy(); + foreach (var baggage in Activity.Current.Baggage) { if (!WildcardMatcher.IsAnyMatch(Configuration.BaggageToAttach, baggage.Key))