Reputation: 4084
I have a web API and would like to record an Internal Application Id agreed with my customer with AppInsight.
This is my code:
var requestTelemetry = System.Web.HttpContextExtension.GetRequestTelemetry(HttpContext.Current);
var appId= GetAppIdFromRequestHeader();
requestTelemetry.Context.GlobalProperties["ApplicationName"] = appId;
client.TrackRequest(requestTelemetry);
I am not sure about updating this GlobalProperties["ApplicationName"]
. If multiple clients with different appId
visited my app the same time, wouldn't this potentially trigger some concurrency issue? (I am thinking it won't, as HttpContext.Current
will isolate different requests)
Also, do I need to call client.TrackRequest
to send the request? I actually think I don't really need to do that.
Upvotes: 0
Views: 1236
Reputation: 3171
Each request gets a new RequestTelemetry instance with own Context (and hence GlobalProperties), so you are safe to use it like the way you showed.
You don't need to call (and shouldn't) call TrackRequest yourself. TrackRequest() is to be called when you yourself track the telemetry manually. In this case you are relying on SDK to track the telemetry, as you are just retrieving the RequestTelemetry from context. Retrieve it, modify it, and let SDK sent it at the end of the request. Doing TrackRequest will cause duplicate, and the one you tracked will have less properties. (some properties are set at the end of the request by SDK)
Also if the cardinality of the "appid" is high, I suggest to use requestTelemetry.Properties instead of GlobalProperties.
Upvotes: 1
Reputation: 29995
If you're using requestTelemetry.Context.GlobalProperties
to add custom fields:
1.It will not trigger concurrency issue, please refer to this github issue for more details.
2.You need to call client.TrackRequest, otherwise you cannot see the custom fields from portal.
And I suggest you can use ITelemetryInitializer and add your logic there, it will automatically add custom properties even if you don't call TrackRequest
method.
Upvotes: 0