Skip to content

Instantly share code, notes, and snippets.

@MihaZupan
Created July 13, 2021 20:52
Show Gist options
  • Save MihaZupan/c01a2689f5ec85e95682830fb0df8e17 to your computer and use it in GitHub Desktop.
Save MihaZupan/c01a2689f5ec85e95682830fb0df8e17 to your computer and use it in GitHub Desktop.
DistributedContextPropagator.Current = new SkipHttpClientActivityPropagator();
public sealed class SkipHttpClientActivityPropagator : DistributedContextPropagator
{
private readonly DistributedContextPropagator _originalPropagator = Current;
public override IReadOnlyCollection<string> Fields => _originalPropagator.Fields;
public override void Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter)
{
if (activity?.OperationName == "System.Net.Http.HttpRequestOut")
{
activity = activity.Parent;
}
_originalPropagator.Inject(activity, carrier, setter);
}
public override void ExtractTraceIdAndState(object? carrier, PropagatorGetterCallback? getter, out string? traceId, out string? traceState) =>
_originalPropagator.ExtractTraceIdAndState(carrier, getter, out traceId, out traceState);
public override IEnumerable<KeyValuePair<string, string?>>? ExtractBaggage(object? carrier, PropagatorGetterCallback? getter) =>
_originalPropagator.ExtractBaggage(carrier, getter);
}
@wkoeter
Copy link

wkoeter commented Jul 19, 2023

I was happy with this workaround so I could create a tree that was completely traceable without having to log all activities created by e.g. HttpClient.

However, using this in .NET Isolated functions with Application Insights will make the application map incomplete. I had a case where I was making calls to CosmosDB which only became visible after removing this workaround.
This workaround has its benefits, but the user should consider the trade-offs before implementing it.

@paku-he
Copy link

paku-he commented Apr 29, 2024

@wkoeter would you be able to provide more details ?
What do you mean that this workaround caused application map incomplete ?

I would appreciate your expertise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment