Skip to content

Instantly share code, notes, and snippets.

@lmolkova
Last active September 18, 2020 21:49
Show Gist options
  • Save lmolkova/b8a8b1df398421ff83f2636aecbbdaff to your computer and use it in GitHub Desktop.
Save lmolkova/b8a8b1df398421ff83f2636aecbbdaff to your computer and use it in GitHub Desktop.
resourceidprocessor
public class ResourceIdProcessor : ActivityProcessor
{
private readonly AsyncLocal<string> resourceId = new AsyncLocal<string>();
private readonly IHttpContextAccessor httpContextAccessor;
public ResourceIdProcessor(IHttpContextAccessor httpContextAccessor)
{
resourceId = null;
}
public override void OnStart(Activity activity)
{
if (this.resourceId.Value != null)
{
activity.AddTag("resourceId", this.resourceId.Value);
}
else
{
activity.AddTag("resourceId", this.GetResourceId());
}
}
public string GetResourceId()
{
var ctx = this.httpContextAccessor.HttpContext;
if (ctx == null)
{
return string.Empty;
}
return this.GetResourceId(ctx);
}
private string GetResourceId(HttpContext context)
{
// HttpContext.Current on ASP.NET classic
// use HttpContext to calculate resource id
// this code runs before any middleware, no way to get any callback before that
return "/SUBSCRIPTIONS/123/RESOURCEGROUPS/MY-RG/PROVIDERS/MICROSOFT.NAMESPACE/TYPE/MY-RESOURCE";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment