Skip to content
This repository was archived by the owner on Jul 5, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions Src/Common/W3C/W3COperationCorrelationTelemetryInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ namespace Microsoft.ApplicationInsights.W3C.Internal
/// Telemetry Initializer that sets correlation ids for W3C.
/// </summary>
[Obsolete("Not ready for public consumption.")]
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "TelemetryInitializers are intended to be instatiated by the framework when added to a config.")]
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification =
"TelemetryInitializers are intended to be instantiated by the framework when added to a config.")]
[EditorBrowsable(EditorBrowsableState.Never)]
#if DEPENDENCY_COLLECTOR
public
public
#else
internal
#endif
Expand All @@ -45,7 +46,8 @@ public void Initialize(ITelemetry telemetry)
UpdateTelemetry(telemetry, currentActivity, false);
}

[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "This method has different code for Net45/NetCore")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification =
"This method has different code for Net45/NetCore")]
internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bool forceUpdate)
{
if (activity == null)
Expand All @@ -55,7 +57,7 @@ internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bo

activity.UpdateContextOnActivity();

// Requests and dependnecies are initialized from the current Activity
// Requests and dependencies are initialized from the current Activity
// (i.e. telemetry.Id = current.Id). Activity is created for such requests specifically
// Traces, exceptions, events on the other side are children of current activity
// There is one exception - SQL DiagnosticSource where current Activity is a parent
Expand All @@ -67,9 +69,9 @@ internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bo
if (initializeFromCurrent)
{
initializeFromCurrent &= !(opTelemetry is DependencyTelemetry dependency &&
dependency.Type == SqlRemoteDependencyType &&
dependency.Type == SqlRemoteDependencyType &&
dependency.Context.GetInternalContext().SdkVersion
.StartsWith(RddDiagnosticSourcePrefix, StringComparison.Ordinal));
.StartsWith(RddDiagnosticSourcePrefix, StringComparison.Ordinal));
}

string spanId = null, parentSpanId = null;
Expand All @@ -78,15 +80,6 @@ internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bo
switch (tag.Key)
{
case W3CConstants.TraceIdTag:
#if NET45
// on .NET Fx Activities are not always reliable, this code prevents update
// of the telemetry that was forcibly updated during Activity lifetime
// ON .NET Core there is no such problem
if (telemetry.Context.Operation.Id == tag.Value && initializeFromCurrent && !forceUpdate)
{
return;
}
#endif
telemetry.Context.Operation.Id = tag.Value;
break;
case W3CConstants.SpanIdTag:
Expand All @@ -107,15 +100,27 @@ internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bo

if (initializeFromCurrent)
{
#if NET45
// on .NET Fx Activities are not always reliable, this code prevents update
// of the telemetry that was forcibly updated during Activity lifetime
// ON .NET Core there is no such problem
// if spanId is valid already and update is not forced, ignore it
if (!forceUpdate && IsValidTelemetryId(opTelemetry.Id, telemetry.Context.Operation.Id))
{
return;
}
#endif
opTelemetry.Id = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, spanId);
if (parentSpanId != null)
{
telemetry.Context.Operation.ParentId = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, parentSpanId);
telemetry.Context.Operation.ParentId =
StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, parentSpanId);
}
}
else
{
telemetry.Context.Operation.ParentId = StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, spanId);
telemetry.Context.Operation.ParentId =
StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, spanId);
}

if (opTelemetry != null)
Expand All @@ -131,5 +136,16 @@ internal static void UpdateTelemetry(ITelemetry telemetry, Activity activity, bo
}
}
}

#if NET45
private static bool IsValidTelemetryId(string id, string operationId)
{
return id.Length == 51 &&
id[0] == '|' &&
id[33] == '.' &&
id.IndexOf('.', 34) == 50 &&
id.IndexOf(operationId, 1, 33, StringComparison.Ordinal) == 1;
}
#endif
}
}
Loading