in BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/OperationHolder.cs [64:145]
protected virtual void Dispose(bool disposing)
{
if (disposing && !this.isDisposed)
{
// We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
// to make sure that the customer is tracking the right telemetry.
lock (LockObj)
{
if (!this.isDisposed)
{
var operationTelemetry = this.Telemetry;
operationTelemetry.Stop();
bool isActivityAvailable = false;
isActivityAvailable = ActivityExtensions.TryRun(() =>
{
var currentActivity = Activity.Current;
if (currentActivity == null
|| (Activity.DefaultIdFormat != ActivityIdFormat.W3C && operationTelemetry.Id != currentActivity.Id)
|| (Activity.DefaultIdFormat == ActivityIdFormat.W3C && operationTelemetry.Id != currentActivity.SpanId.ToHexString()))
{
// W3COperationCorrelationTelemetryInitializer changes Id
// but keeps an original one in 'ai_legacyRequestId' property
if (!operationTelemetry.Properties.TryGetValue("ai_legacyRequestId", out var legacyId) ||
legacyId != currentActivity?.Id)
{
// this is for internal error reporting
CoreEventSource.Log.InvalidOperationToStopError();
// this are details with unique ids for debugging
CoreEventSource.Log.InvalidOperationToStopDetails(
string.Format(
CultureInfo.InvariantCulture,
"Telemetry Id '{0}' does not match current Activity '{1}'",
operationTelemetry.Id,
currentActivity?.Id));
return;
}
}
this.telemetryClient.Track(operationTelemetry);
currentActivity?.Stop();
if (this.originalActivity != null &&
Activity.Current != this.originalActivity &&
this.originalActivity is Activity activity)
{
Activity.Current = activity;
}
});
if (!isActivityAvailable)
{
var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
{
// this is for internal error reporting
CoreEventSource.Log.InvalidOperationToStopError();
// this are details with unique ids for debugging
CoreEventSource.Log.InvalidOperationToStopDetails(
string.Format(
CultureInfo.InvariantCulture,
"Telemetry Id '{0}' does not match current Activity '{1}'",
operationTelemetry.Id,
currentOperationContext?.ParentOperationId));
return;
}
this.telemetryClient.Track(operationTelemetry);
CallContextHelpers.RestoreOperationContext(this.ParentContext);
}
}
this.isDisposed = true;
}
}
}