Last active
September 18, 2020 21:49
-
-
Save lmolkova/b8a8b1df398421ff83f2636aecbbdaff to your computer and use it in GitHub Desktop.
resourceidprocessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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