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))