In one of the apps I was developing I needed to collect some usage data. However, I did not want to run this logic for requests generated by the ping tests which test the app's availability from Application Insights.
So I thought there must be some kind of headers or something that would say the request is a ping test.
Well, the App Insights SDK knows which request is synthetic right?
So I found this class in the ASP.NET Core SDK source code: SyntheticTelemetryInitializer
/// <summary>
/// This will allow to mark synthetic traffic from availability tests
/// </summary>
internal class SyntheticTelemetryInitializer : TelemetryInitializerBase
{
private const string SyntheticTestRunId = "SyntheticTest-RunId";
private const string SyntheticTestLocation = "SyntheticTest-Location";
private const string SyntheticSourceHeaderValue = "Application Insights Availability Monitoring";
public SyntheticTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
if (string.IsNullOrEmpty(telemetry.Context.Operation.SyntheticSource))
{
var runIdHeader = platformContext.Request.Headers[SyntheticTestRunId];
var locationHeader = platformContext.Request.Headers[SyntheticTestLocation];
if (!string.IsNullOrEmpty(runIdHeader) &&
!string.IsNullOrEmpty(locationHeader))
{
telemetry.Context.Operation.SyntheticSource = SyntheticSourceHeaderValue;
telemetry.Context.User.Id = locationHeader + "_" + runIdHeader;
telemetry.Context.Session.Id = runIdHeader;
}
}
}
}
It clearly identifies two headers which identify the request as a ping test:
- SyntheticTest-RunId
- SyntheticTest-Location
If these two headers exist in a request and have values, then it is a ping test request :)