diff --git a/src/Elastic.Apm/Model/Error.cs b/src/Elastic.Apm/Model/Error.cs index 6aa8fe7ae..85176fae0 100644 --- a/src/Elastic.Apm/Model/Error.cs +++ b/src/Elastic.Apm/Model/Error.cs @@ -2,7 +2,6 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -66,13 +65,16 @@ private void CheckAndCaptureBaggage(Transaction transaction) //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(); + Context ??= transaction?.Context.DeepCopy(); foreach (var baggage in Activity.Current.Baggage) { if (!WildcardMatcher.IsAnyMatch(Configuration.BaggageToAttach, baggage.Key)) continue; + // The context is created only if there is a baggage value to insert. + Context ??= new Context(); + var newKey = $"baggage.{baggage.Key}"; var labels = Context.InternalLabels.Value; labels[newKey] = baggage.Value;