Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional logging to outgoing http call propagation #2247

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ElasticApmAgent.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ See the LICENSE file in the project root for more information</s:String>
<s:Boolean x:Key="/Default/CodeStyle/LiveTemplatesUseVar/UseVar/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=KM/@EntryIndexedValue">KM</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ private void ProcessStartEvent(TRequest request, Uri requestUrl)
return;
}

if (_realAgent?.TracerInternal.CurrentSpan is Span currentSpan && currentSpan.IsExitSpan)
if (_realAgent?.TracerInternal.CurrentSpan is Span { IsExitSpan: true } currentSpan)
{
Logger.Trace()?
.Log("Exit span detected: {RequestUrl}, propagating without creating a child span", requestUrl.Sanitize());
PropagateTraceContext(request, transaction, currentSpan);
return;
}
Expand All @@ -151,22 +153,20 @@ private void ProcessStartEvent(TRequest request, Uri requestUrl)
var suppressSpanCreation = false;
if (_configuration?.HasTracers ?? false)
{
using (var httpTracers = _configuration.GetTracers())
using var httpTracers = _configuration.GetTracers();
foreach (var httpSpanTracer in httpTracers)
{
foreach (var httpSpanTracer in httpTracers)
suppressSpanCreation = httpSpanTracer.ShouldSuppressSpanCreation();
if (suppressSpanCreation)
break;

if (httpSpanTracer.IsMatch(method, requestUrl,
header => RequestTryGetHeader(request, header, out var value) ? value : null))
{
suppressSpanCreation = httpSpanTracer.ShouldSuppressSpanCreation();
if (suppressSpanCreation)
span = httpSpanTracer.StartSpan(ApmAgent, method, requestUrl,
header => RequestTryGetHeader(request, header, out var value) ? value : null);
if (span != null)
break;

if (httpSpanTracer.IsMatch(method, requestUrl,
header => RequestTryGetHeader(request, header, out var value) ? value : null))
{
span = httpSpanTracer.StartSpan(ApmAgent, method, requestUrl,
header => RequestTryGetHeader(request, header, out var value) ? value : null);
if (span != null)
break;
}
}
}
}
Expand Down Expand Up @@ -223,27 +223,36 @@ private void ProcessStartEvent(TRequest request, Uri requestUrl)

private void PropagateTraceContext(TRequest request, ITransaction transaction, ISpan span)
{
if (!RequestHeadersContain(request, TraceContext.TraceParentHeaderName))
if (RequestTryGetHeader(request, TraceContext.TraceParentHeaderName, out var existingParent))
{
var traceParent = TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData);
Logger.Trace()?
.Log("Skipping adding {HeaderName}: with value {TraceParent} since it already has value: {ExistingParent}",
TraceContext.TraceParentHeaderName, traceParent, existingParent);
}
else
{
// We call TraceParent.BuildTraceparent explicitly instead of DistributedTracingData.SerializeToString because
// in the future we might change DistributedTracingData.SerializeToString to use some other internal format
// but here we want the string to be in W3C 'traceparent' header format.
RequestHeadersAdd(request, TraceContext.TraceParentHeaderName, TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData));
var traceParent = TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData);
RequestHeadersAdd(request, TraceContext.TraceParentHeaderName, traceParent);
}

if (transaction is Transaction t)
if (transaction is Transaction t
&& t.Configuration.UseElasticTraceparentHeader
&& !RequestHeadersContain(request, TraceContext.TraceParentHeaderNamePrefixed))
{
if (t.Configuration.UseElasticTraceparentHeader)
{
if (!RequestHeadersContain(request, TraceContext.TraceParentHeaderNamePrefixed))
{
RequestHeadersAdd(request, TraceContext.TraceParentHeaderNamePrefixed,
TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData));
}
}
var traceParent = TraceContext.BuildTraceparent(span.OutgoingDistributedTracingData);
RequestHeadersAdd(request, TraceContext.TraceParentHeaderNamePrefixed, traceParent);
}

if (!RequestHeadersContain(request, TraceContext.TraceStateHeaderName) && span.OutgoingDistributedTracingData != null
&& span.OutgoingDistributedTracingData.HasTraceState)
RequestHeadersAdd(request, TraceContext.TraceStateHeaderName, span.OutgoingDistributedTracingData.TraceState.ToTextHeader());
if (!RequestHeadersContain(request, TraceContext.TraceStateHeaderName)
&& span.OutgoingDistributedTracingData is { HasTraceState: true })
{
var traceState = span.OutgoingDistributedTracingData.TraceState.ToTextHeader();
RequestHeadersAdd(request, TraceContext.TraceStateHeaderName, traceState);
}

if (!RequestHeadersContain(request, BaggageHeaderName) && Activity.Current != null && Activity.Current.Baggage.Any())
{
Expand Down
1 change: 0 additions & 1 deletion src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ private T TryExecuteFilter<T>(List<Func<T, T>> filters, T item) where T : class
{
try
{
_logger?.Trace()?.Log("Start executing filter on transaction");
var itemAfterFilter = filter(item);
if (itemAfterFilter != null)
{
Expand Down
Loading