diff --git a/CHANGELOG.md b/CHANGELOG.md index cd6207dd3..949ee8d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Version 2.9.0 - [Fix: remove unused reference to Microsoft.AspNet.TelemetryCorrelation package from DependencyCollector](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/1136) +- [Move W3C support from DependencyCollector package to base SDK, deprecate W3C support in DependencyCollector](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/1138) ## Version 2.9.0-beta3 - Update Base SDK to version 2.9.0-beta3 diff --git a/Src/Common/StringUtilities.cs b/Src/Common/StringUtilities.cs index 3dbf01ca2..9a9fac910 100644 --- a/Src/Common/StringUtilities.cs +++ b/Src/Common/StringUtilities.cs @@ -7,8 +7,9 @@ namespace Microsoft.ApplicationInsights.Common.Internal using System; using System.Diagnostics; using System.Globalization; + using Microsoft.ApplicationInsights.Extensibility.W3C; #if DEPENDENCY_COLLECTOR - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.W3C; #else using Microsoft.ApplicationInsights.W3C.Internal; #endif @@ -17,14 +18,12 @@ namespace Microsoft.ApplicationInsights.Common.Internal /// Generic functions to perform common operations on a string. /// #if DEPENDENCY_COLLECTOR - public + public #else internal #endif static class StringUtilities { - private static readonly uint[] Lookup32 = CreateLookup32(); - /// /// Check a strings length and trim to a max length if needed. /// @@ -47,9 +46,10 @@ public static string EnforceMaxLength(string input, int maxLength) /// https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md#trace-id /// /// Random 16 bytes array encoded as hex string + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CUtilities.GenerateTraceId in Microsoft.ApplicationInsights package instead.")] public static string GenerateTraceId() { - return GenerateId(Guid.NewGuid().ToByteArray(), 0, 16); + return W3CUtilities.GenerateTraceId(); } /// @@ -57,9 +57,10 @@ public static string GenerateTraceId() /// https://github.com/w3c/distributed-tracing/blob/master/trace_context/HTTP_HEADER_FORMAT.md#span-id /// /// Random 8 bytes array encoded as hex string + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CUtilities.GenerateTraceId in Microsoft.ApplicationInsights package instead.")] public static string GenerateSpanId() { - return GenerateId(Guid.NewGuid().ToByteArray(), 0, 8); + return W3CUtilities.GenerateTraceId().Substring(0, 16); } /// @@ -68,65 +69,15 @@ public static string GenerateSpanId() /// Trace Id. /// Span id. /// valid Request-Id. + [Obsolete("Obsolete, implement yourself with 'string.Concat(\"|\", traceId, \".\", spanId, \".\").'")] public static string FormatRequestId(string traceId, string spanId) { - return String.Concat("|", traceId, ".", spanId, "."); - } - - /// - /// Gets root id (string between '|' and the first dot) from the hierarchical Id. - /// - /// Id to extract root from. - /// Root operation id. - internal static string GetRootId(string hierarchicalId) - { - // Returns the root Id from the '|' to the first '.' if any. - int rootEnd = hierarchicalId.IndexOf('.'); - if (rootEnd < 0) - { - rootEnd = hierarchicalId.Length; - } - - int rootStart = hierarchicalId[0] == '|' ? 1 : 0; - return hierarchicalId.Substring(rootStart, rootEnd - rootStart); + return string.Concat("|", traceId, ".", spanId, "."); } -#pragma warning disable 612, 618 internal static string FormatAzureTracestate(string appId) { - return String.Concat(W3CConstants.AzureTracestateNamespace, "=", appId); - } -#pragma warning restore 612, 618 - - /// - /// Converts byte array to hex lower case string. - /// - /// Array encoded as hex string - private static string GenerateId(byte[] bytes, int start, int length) - { - // See https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727 - var result = new char[length * 2]; - for (int i = start; i < start + length; i++) - { - var val = Lookup32[bytes[i]]; - result[2 * i] = (char)val; - result[(2 * i) + 1] = (char)(val >> 16); - } - - return new string(result); - } - - private static uint[] CreateLookup32() - { - // See https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa/24343727#24343727 - var result = new uint[256]; - for (int i = 0; i < 256; i++) - { - string s = i.ToString("x2", CultureInfo.InvariantCulture); - result[i] = ((uint)s[0]) + ((uint)s[1] << 16); - } - - return result; + return string.Concat(W3CConstants.AzureTracestateNamespace, "=", appId); } } } diff --git a/Src/Common/W3C/W3CActivityExtensions.cs b/Src/Common/W3C/W3CActivityExtensions.cs index 41b78e490..4f4bbd321 100644 --- a/Src/Common/W3C/W3CActivityExtensions.cs +++ b/Src/Common/W3C/W3CActivityExtensions.cs @@ -1,200 +1,81 @@ #if DEPENDENCY_COLLECTOR - namespace Microsoft.ApplicationInsights.W3C -#else - namespace Microsoft.ApplicationInsights.W3C.Internal -#endif +namespace Microsoft.ApplicationInsights.W3C { using System; using System.ComponentModel; using System.Diagnostics; - using System.Globalization; - using System.Linq; - using System.Text.RegularExpressions; -#if DEPENDENCY_COLLECTOR - using Microsoft.ApplicationInsights.Common; -#else - using Microsoft.ApplicationInsights.Common.Internal; -#endif /// /// Extends Activity to support W3C distributed tracing standard. /// - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions in Microsoft.ApplicationInsights package instead")] [EditorBrowsable(EditorBrowsableState.Never)] -#if DEPENDENCY_COLLECTOR - public -#else - internal -#endif - static class W3CActivityExtensions + public static class W3CActivityExtensions { - private static readonly Regex TraceIdRegex = new Regex("^[a-f0-9]{32}$", RegexOptions.Compiled); - private static readonly Regex SpanIdRegex = new Regex("^[a-f0-9]{16}$", RegexOptions.Compiled); - /// /// Generate new W3C context. /// /// Activity to generate W3C context on. /// The same Activity for chaining. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GenerateW3CContext in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static Activity GenerateW3CContext(this Activity activity) - { - activity.SetVersion(W3CConstants.DefaultVersion); - activity.SetSampled(W3CConstants.TraceFlagRecordedAndNotRequested); - activity.SetSpanId(StringUtilities.GenerateSpanId()); - - activity.SetTraceId(activity.RootId != null && TraceIdRegex.IsMatch(activity.RootId) - ? activity.RootId - : StringUtilities.GenerateTraceId()); - - return activity; - } + public static Activity GenerateW3CContext(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GenerateW3CContext(activity); /// /// Checks if current Activity has W3C properties on it. /// /// Activity to check. /// True if Activity has W3C properties, false otherwise. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.IsW3CActivity in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static bool IsW3CActivity(this Activity activity) - { - return activity != null && activity.Tags.Any(t => t.Key == W3CConstants.TraceIdTag); - } + public static bool IsW3CActivity(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.IsW3CActivity(activity); /// /// Updates context on the Activity based on the W3C Context in the parent Activity tree. /// /// Activity to update W3C context on. /// The same Activity for chaining. - [Obsolete("Not ready for public consumption.")] + [Obsolete( + "Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.UpdateContextOnActivity in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static Activity UpdateContextOnActivity(this Activity activity) - { - if (activity == null || activity.Tags.Any(t => t.Key == W3CConstants.TraceIdTag)) - { - return activity; - } - - // no w3c Tags on Activity - activity.Parent.UpdateContextOnActivity(); - - // at this point, Parent has W3C tags, but current activity does not - update it - return activity.UpdateContextFromParent(); - } + public static Activity UpdateContextOnActivity(this Activity activity) => + Extensibility.W3C.W3CActivityExtensions.UpdateContextOnActivity(activity); /// /// Gets traceparent header value for the Activity or null if there is no W3C context on it. /// /// Activity to read W3C context from. /// traceparent header value. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GetTraceparent in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetTraceparent(this Activity activity) - { - string version = null, traceId = null, spanId = null, sampled = null; - foreach (var tag in activity.Tags) - { - switch (tag.Key) - { - case W3CConstants.TraceIdTag: - traceId = tag.Value; - break; - case W3CConstants.SpanIdTag: - spanId = tag.Value; - break; - case W3CConstants.VersionTag: - version = tag.Value; - break; - case W3CConstants.SampledTag: - sampled = tag.Value; - break; - } - } - - if (traceId == null || spanId == null || version == null || sampled == null) - { - return null; - } - - return string.Join("-", version, traceId, spanId, sampled); - } + public static string GetTraceparent(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GetTraceparent(activity); /// /// Initializes W3C context on the Activity from traceparent header value. /// /// Activity to set W3C context on. /// Valid traceparent header like 00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.SetTraceparent in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static void SetTraceparent(this Activity activity, string value) - { - if (activity.IsW3CActivity()) - { - return; - } - - // we only support 00 version and ignore caller version - activity.SetVersion(W3CConstants.DefaultVersion); - - string traceId = null, parentSpanId = null, sampledStr = null; - bool isValid = false; - - var parts = value?.Split('-'); - if (parts != null && parts.Length == 4) - { - traceId = parts[1]; - parentSpanId = parts[2]; - sampledStr = parts[3]; - isValid = TraceIdRegex.IsMatch(traceId) && SpanIdRegex.IsMatch(parentSpanId); - } - - if (isValid) - { - byte.TryParse(sampledStr, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var sampled); - - // we always defer sampling - if ((sampled & W3CConstants.RequestedTraceFlag) == W3CConstants.RequestedTraceFlag) - { - activity.SetSampled(W3CConstants.TraceFlagRecordedAndRequested); - } - else - { - activity.SetSampled(W3CConstants.TraceFlagRecordedAndNotRequested); - } - - activity.SetParentSpanId(parentSpanId); - activity.SetSpanId(StringUtilities.GenerateSpanId()); - activity.SetTraceId(traceId); - } - else - { - activity.SetSampled(W3CConstants.TraceFlagRecordedAndNotRequested); - activity.SetSpanId(StringUtilities.GenerateSpanId()); - activity.SetTraceId(StringUtilities.GenerateTraceId()); - } - } + public static void SetTraceparent(this Activity activity, string value) => Extensibility.W3C.W3CActivityExtensions.SetTraceparent(activity, value); /// /// Gets tracestate header value from the Activity. /// /// Activity to get tracestate from. /// tracestate header value. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GetTracestate in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetTracestate(this Activity activity) => - activity.Tags.FirstOrDefault(t => t.Key == W3CConstants.TracestateTag).Value; + public static string GetTracestate(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GetTracestate(activity); /// /// Sets tracestate header value on the Activity. /// /// Activity to set tracestate on. /// tracestate header value. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.SetTracestate in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static void SetTracestate(this Activity activity, string value) => - activity.AddTag(W3CConstants.TracestateTag, value); + public static void SetTracestate(this Activity activity, string value) => Extensibility.W3C.W3CActivityExtensions.SetTracestate(activity, value); /// /// Gets TraceId from the Activity. @@ -202,9 +83,9 @@ public static void SetTracestate(this Activity activity, string value) => /// /// Activity to get traceId from. /// TraceId value or null if it does not exist. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GetTraceId in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetTraceId(this Activity activity) => activity.Tags.FirstOrDefault(t => t.Key == W3CConstants.TraceIdTag).Value; + public static string GetTraceId(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GetTraceId(activity); /// /// Gets SpanId from the Activity. @@ -212,9 +93,9 @@ public static void SetTracestate(this Activity activity, string value) => /// /// Activity to get spanId from. /// SpanId value or null if it does not exist. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GetSpanId in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetSpanId(this Activity activity) => activity.Tags.FirstOrDefault(t => t.Key == W3CConstants.SpanIdTag).Value; + public static string GetSpanId(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GetSpanId(activity); /// /// Gets ParentSpanId from the Activity. @@ -222,63 +103,9 @@ public static void SetTracestate(this Activity activity, string value) => /// /// Activity to get ParentSpanId from. /// ParentSpanId value or null if it does not exist. - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3CActivityExtensions.GetParentSpanId in Microsoft.ApplicationInsights package instead.")] [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetParentSpanId(this Activity activity) => activity.Tags.FirstOrDefault(t => t.Key == W3CConstants.ParentSpanIdTag).Value; - - [Obsolete("Not ready for public consumption.")] - [EditorBrowsable(EditorBrowsableState.Never)] - internal static void SetParentSpanId(this Activity activity, string value) => - activity.AddTag(W3CConstants.ParentSpanIdTag, value); - - private static void SetTraceId(this Activity activity, string value) => - activity.AddTag(W3CConstants.TraceIdTag, value); - - private static void SetSpanId(this Activity activity, string value) => - activity.AddTag(W3CConstants.SpanIdTag, value); - - private static void SetVersion(this Activity activity, string value) => - activity.AddTag(W3CConstants.VersionTag, value); - - private static void SetSampled(this Activity activity, string value) => - activity.AddTag(W3CConstants.SampledTag, value); - - private static Activity UpdateContextFromParent(this Activity activity) - { - if (activity != null && activity.Tags.All(t => t.Key != W3CConstants.TraceIdTag)) - { - if (activity.Parent == null) - { - activity.GenerateW3CContext(); - } - else - { - foreach (var tag in activity.Parent.Tags) - { - switch (tag.Key) - { - case W3CConstants.TraceIdTag: - activity.SetTraceId(tag.Value); - break; - case W3CConstants.SpanIdTag: - activity.SetParentSpanId(tag.Value); - activity.SetSpanId(StringUtilities.GenerateSpanId()); - break; - case W3CConstants.VersionTag: - activity.SetVersion(tag.Value); - break; - case W3CConstants.SampledTag: - activity.SetSampled(tag.Value); - break; - case W3CConstants.TracestateTag: - activity.SetTracestate(tag.Value); - break; - } - } - } - } - - return activity; - } + public static string GetParentSpanId(this Activity activity) => Extensibility.W3C.W3CActivityExtensions.GetParentSpanId(activity); } } +#endif \ No newline at end of file diff --git a/Src/Common/W3C/W3CConstants.cs b/Src/Common/W3C/W3CConstants.cs index e6fcd7fc7..1a68130f7 100644 --- a/Src/Common/W3C/W3CConstants.cs +++ b/Src/Common/W3C/W3CConstants.cs @@ -1,19 +1,17 @@ #if DEPENDENCY_COLLECTOR namespace Microsoft.ApplicationInsights.W3C #else -namespace Microsoft.ApplicationInsights.W3C.Internal + namespace Microsoft.ApplicationInsights.W3C.Internal #endif { - using System; using System.ComponentModel; /// /// W3C constants. /// - [Obsolete("Not ready for public consumption.")] [EditorBrowsable(EditorBrowsableState.Never)] #if DEPENDENCY_COLLECTOR - public + public #else internal #endif diff --git a/Src/Common/W3C/W3COperationCorrelationTelemetryInitializer.cs b/Src/Common/W3C/W3COperationCorrelationTelemetryInitializer.cs index f93a07d6d..edc8a6192 100644 --- a/Src/Common/W3C/W3COperationCorrelationTelemetryInitializer.cs +++ b/Src/Common/W3C/W3COperationCorrelationTelemetryInitializer.cs @@ -1,40 +1,22 @@ #if DEPENDENCY_COLLECTOR namespace Microsoft.ApplicationInsights.W3C -#else -namespace Microsoft.ApplicationInsights.W3C.Internal -#endif { using System; using System.ComponentModel; - using System.Diagnostics; using System.Diagnostics.CodeAnalysis; - using System.Linq; using Microsoft.ApplicationInsights.Channel; - using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; - using Microsoft.ApplicationInsights.Extensibility.Implementation; -#if DEPENDENCY_COLLECTOR - using Microsoft.ApplicationInsights.Common; -#else - using Microsoft.ApplicationInsights.Common.Internal; -#endif /// /// Telemetry Initializer that sets correlation ids for W3C. /// - [Obsolete("Not ready for public consumption.")] + [Obsolete("Use Microsoft.ApplicationInsights.Extensibility.W3C.W3COperationCorrelationTelemetryInitializer in Microsoft.ApplicationInsights package instead.")] [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 -#else - internal -#endif - class W3COperationCorrelationTelemetryInitializer : ITelemetryInitializer + public class W3COperationCorrelationTelemetryInitializer : ITelemetryInitializer { - private const string RddDiagnosticSourcePrefix = "rdddsc"; - private const string SqlRemoteDependencyType = "SQL"; + private readonly Extensibility.W3C.W3COperationCorrelationTelemetryInitializer internalInitializer = new Extensibility.W3C.W3COperationCorrelationTelemetryInitializer(); /// /// Initializes telemetry item. @@ -42,110 +24,8 @@ class W3COperationCorrelationTelemetryInitializer : ITelemetryInitializer /// Telemetry item. public void Initialize(ITelemetry telemetry) { - Activity currentActivity = Activity.Current; - UpdateTelemetry(telemetry, currentActivity, false); + this.internalInitializer.Initialize(telemetry); } - - [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) - { - return; - } - - activity.UpdateContextOnActivity(); - - // 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 - // for dependency calls. - - OperationTelemetry opTelemetry = telemetry as OperationTelemetry; - bool initializeFromCurrent = opTelemetry != null; - - if (initializeFromCurrent) - { - initializeFromCurrent &= !(opTelemetry is DependencyTelemetry dependency && - dependency.Type == SqlRemoteDependencyType && - dependency.Context.GetInternalContext().SdkVersion - .StartsWith(RddDiagnosticSourcePrefix, StringComparison.Ordinal)); - } - - string spanId = null, parentSpanId = null; - foreach (var tag in activity.Tags) - { - switch (tag.Key) - { - case W3CConstants.TraceIdTag: - telemetry.Context.Operation.Id = tag.Value; - break; - case W3CConstants.SpanIdTag: - spanId = tag.Value; - break; - case W3CConstants.ParentSpanIdTag: - parentSpanId = tag.Value; - break; - case W3CConstants.TracestateTag: - if (telemetry is OperationTelemetry operation) - { - operation.Properties[W3CConstants.TracestateTag] = tag.Value; - } - - break; - } - } - - 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); - } - } - else - { - telemetry.Context.Operation.ParentId = - StringUtilities.FormatRequestId(telemetry.Context.Operation.Id, spanId); - } - - if (opTelemetry != null) - { - if (opTelemetry.Context.Operation.Id != activity.RootId) - { - opTelemetry.Properties[W3CConstants.LegacyRootIdProperty] = activity.RootId; - } - - if (opTelemetry.Id != activity.Id) - { - opTelemetry.Properties[W3CConstants.LegacyRequestIdProperty] = activity.Id; - } - } - } - -#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 } } +#endif \ No newline at end of file diff --git a/Src/DependencyCollector/DependencyCollector/DependencyCollector.csproj b/Src/DependencyCollector/DependencyCollector/DependencyCollector.csproj index 1647ea004..7255969de 100644 --- a/Src/DependencyCollector/DependencyCollector/DependencyCollector.csproj +++ b/Src/DependencyCollector/DependencyCollector/DependencyCollector.csproj @@ -36,7 +36,7 @@ - + diff --git a/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs b/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs index f43f0b7cd..faa25f597 100644 --- a/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs +++ b/Src/DependencyCollector/Net45.Tests/DependencyTrackingTelemetryModuleHttpTest.cs @@ -19,8 +19,8 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.TestFramework; - using Microsoft.ApplicationInsights.W3C; using Microsoft.ApplicationInsights.Web.TestFramework; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -300,7 +300,6 @@ public void TestDependencyCollectionEventSourceRedirect() this.TestCollectionResponseWithRedirects(false, LocalhostUrlEventSource); } -#pragma warning disable 612, 618 /// /// Tests that outgoing requests emit W3C headers and telemetry is initialized accordingly when configured so. /// @@ -346,19 +345,19 @@ public void TestDependencyCollectionWithW3CHeadersDiagnosticSource() var dependencyIdParts = dependency.Id.Split('.', '|'); Assert.AreEqual(4, dependencyIdParts.Length); Assert.AreEqual(expectedTraceId, dependencyIdParts[1]); - Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers[W3CConstants.TraceParentHeader]); + Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers[W3C.W3CConstants.TraceParentHeader]); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={expectedAppId},state=some", request.Headers[W3CConstants.TraceStateHeader]); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={expectedAppId},state=some", request.Headers[W3C.W3CConstants.TraceStateHeader]); Assert.AreEqual("k=v", request.Headers[RequestResponseHeaders.CorrelationContextHeader]); Assert.AreEqual("v", dependency.Properties["k"]); - Assert.AreEqual("state=some", dependency.Properties[W3CConstants.TracestateTag]); + Assert.AreEqual("state=some", dependency.Properties[W3C.W3CConstants.TracestateTag]); - Assert.IsTrue(dependency.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.IsTrue(dependency.Properties[W3CConstants.LegacyRequestIdProperty].StartsWith("|guid.")); + Assert.IsTrue(dependency.Properties.ContainsKey(W3C.W3CConstants.LegacyRequestIdProperty)); + Assert.IsTrue(dependency.Properties[W3C.W3CConstants.LegacyRequestIdProperty].StartsWith("|guid.")); - Assert.IsTrue(dependency.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty)); - Assert.AreEqual("guid", dependency.Properties[W3CConstants.LegacyRootIdProperty]); + Assert.IsTrue(dependency.Properties.ContainsKey(W3C.W3CConstants.LegacyRootIdProperty)); + Assert.AreEqual("guid", dependency.Properties[W3C.W3CConstants.LegacyRootIdProperty]); } } @@ -399,8 +398,8 @@ public void TestDependencyCollectionWithW3CHeadersDiagnosticSourceAndStartParent Assert.AreEqual(requestTelemetry.Context.Operation.Id, dependencyTelemetry.Context.Operation.Id); Assert.AreEqual(requestTelemetry.Id, dependencyTelemetry.Context.Operation.ParentId); - Assert.AreEqual(operationActvity.RootId, dependencyTelemetry.Properties[W3CConstants.LegacyRootIdProperty]); - Assert.IsTrue(dependencyTelemetry.Properties[W3CConstants.LegacyRequestIdProperty].StartsWith(operationActvity.Id)); + Assert.AreEqual(operationActvity.RootId, dependencyTelemetry.Properties[W3C.W3CConstants.LegacyRootIdProperty]); + Assert.IsTrue(dependencyTelemetry.Properties[W3C.W3CConstants.LegacyRequestIdProperty].StartsWith(operationActvity.Id)); } } @@ -438,14 +437,12 @@ public void TestDependencyCollectionWithW3CHeadersAndStateDiagnosticSource() parent.Stop(); - Assert.IsTrue(request.Headers[W3CConstants.TraceStateHeader].Contains($"{W3CConstants.AzureTracestateNamespace}={expectedAppId}")); - Assert.IsTrue(request.Headers[W3CConstants.TraceStateHeader].Contains("some=state")); - Assert.AreEqual(2, request.Headers[W3CConstants.TraceStateHeader].Split(',').Length); + Assert.IsTrue(request.Headers[W3C.W3CConstants.TraceStateHeader].Contains($"{W3C.W3CConstants.AzureTracestateNamespace}={expectedAppId}")); + Assert.IsTrue(request.Headers[W3C.W3CConstants.TraceStateHeader].Contains("some=state")); + Assert.AreEqual(2, request.Headers[W3C.W3CConstants.TraceStateHeader].Split(',').Length); } } -#pragma warning restore 612, 618 - private void TestCollectionPostRequests(bool enableDiagnosticSource, string url) { using (this.CreateDependencyTrackingModule(enableDiagnosticSource)) diff --git a/Src/DependencyCollector/NetCore.Tests/DependencyTrackingTelemetryModuleTestNetCore.cs b/Src/DependencyCollector/NetCore.Tests/DependencyTrackingTelemetryModuleTestNetCore.cs index b57d32b06..f768cc0ce 100644 --- a/Src/DependencyCollector/NetCore.Tests/DependencyTrackingTelemetryModuleTestNetCore.cs +++ b/Src/DependencyCollector/NetCore.Tests/DependencyTrackingTelemetryModuleTestNetCore.cs @@ -15,16 +15,14 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.TestFramework; - using Microsoft.ApplicationInsights.W3C; using Microsoft.ApplicationInsights.Web.TestFramework; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable 612, 618 - /// /// .NET Core specific tests that verify Http Dependencies are collected for outgoing request /// @@ -237,25 +235,25 @@ public async Task TestDependencyCollectionWithW3CHeadersAndRequestId() Assert.AreEqual(expectedTraceId, dependency.Context.Operation.Id); Assert.AreEqual($"|{expectedTraceId}.{expectedParentId}.", dependency.Context.Operation.ParentId); - Assert.IsTrue(request.Headers.Contains(W3CConstants.TraceParentHeader)); + Assert.IsTrue(request.Headers.Contains(W3C.W3CConstants.TraceParentHeader)); var dependencyIdParts = dependency.Id.Split('.', '|'); Assert.AreEqual(4, dependencyIdParts.Length); Assert.AreEqual(expectedTraceId, dependencyIdParts[1]); - Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers.GetValues(W3CConstants.TraceParentHeader).Single()); + Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers.GetValues(W3C.W3CConstants.TraceParentHeader).Single()); - Assert.IsTrue(request.Headers.Contains(W3CConstants.TraceStateHeader)); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={expectedAppId},state=some", request.Headers.GetValues(W3CConstants.TraceStateHeader).Single()); + Assert.IsTrue(request.Headers.Contains(W3C.W3CConstants.TraceStateHeader)); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={expectedAppId},state=some", request.Headers.GetValues(W3C.W3CConstants.TraceStateHeader).Single()); Assert.IsTrue(request.Headers.Contains(RequestResponseHeaders.CorrelationContextHeader)); Assert.AreEqual("k=v", request.Headers.GetValues(RequestResponseHeaders.CorrelationContextHeader).Single()); Assert.AreEqual("v", dependency.Properties["k"]); - Assert.AreEqual("state=some", dependency.Properties[W3CConstants.TracestateTag]); + Assert.AreEqual("state=some", dependency.Properties[W3C.W3CConstants.TracestateTag]); - Assert.IsTrue(dependency.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.IsTrue(dependency.Properties[W3CConstants.LegacyRequestIdProperty].StartsWith("|guid.")); + Assert.IsTrue(dependency.Properties.ContainsKey(W3C.W3CConstants.LegacyRequestIdProperty)); + Assert.IsTrue(dependency.Properties[W3C.W3CConstants.LegacyRequestIdProperty].StartsWith("|guid.")); } } @@ -295,19 +293,19 @@ public async Task TestDependencyCollectionWithW3CHeadersAndNoParentContext() Assert.AreEqual(expectedTraceId, dependency.Context.Operation.Id); Assert.AreEqual($"|{expectedTraceId}.{expectedParentId}.", dependency.Context.Operation.ParentId); - Assert.IsTrue(request.Headers.Contains(W3CConstants.TraceParentHeader)); + Assert.IsTrue(request.Headers.Contains(W3C.W3CConstants.TraceParentHeader)); var dependencyIdParts = dependency.Id.Split('.', '|'); Assert.AreEqual(4, dependencyIdParts.Length); Assert.AreEqual(expectedTraceId, dependencyIdParts[1]); - Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers.GetValues(W3CConstants.TraceParentHeader).Single()); + Assert.AreEqual($"00-{expectedTraceId}-{dependencyIdParts[2]}-02", request.Headers.GetValues(W3C.W3CConstants.TraceParentHeader).Single()); - Assert.IsTrue(request.Headers.Contains(W3CConstants.TraceStateHeader)); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={expectedAppId}", request.Headers.GetValues(W3CConstants.TraceStateHeader).Single()); + Assert.IsTrue(request.Headers.Contains(W3C.W3CConstants.TraceStateHeader)); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={expectedAppId}", request.Headers.GetValues(W3C.W3CConstants.TraceStateHeader).Single()); - Assert.IsTrue(dependency.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.IsTrue(dependency.Properties[W3CConstants.LegacyRequestIdProperty].StartsWith(parent.Id)); + Assert.IsTrue(dependency.Properties.ContainsKey(W3C.W3CConstants.LegacyRequestIdProperty)); + Assert.IsTrue(dependency.Properties[W3C.W3CConstants.LegacyRequestIdProperty].StartsWith(parent.Id)); } } @@ -343,8 +341,8 @@ public async Task TestDependencyCollectionWithW3CHeadersWithState() parent.Stop(); - var traceState = HttpHeadersUtilities.GetHeaderValues(request.Headers, W3CConstants.TraceStateHeader).First(); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={expectedAppId},some=state", traceState); + var traceState = HttpHeadersUtilities.GetHeaderValues(request.Headers, W3C.W3CConstants.TraceStateHeader).First(); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={expectedAppId},some=state", traceState); } } @@ -445,5 +443,4 @@ public void Configure(IApplicationBuilder app) } } } -#pragma warning restore 612, 618 } diff --git a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems index 6aaaba12e..9efbc4e55 100644 --- a/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems +++ b/Src/DependencyCollector/Shared.Tests/DependencyCollector.Shared.Tests.projitems @@ -30,8 +30,6 @@ - - diff --git a/Src/DependencyCollector/Shared.Tests/HeaderCollectionManipulationTests.cs b/Src/DependencyCollector/Shared.Tests/HeaderCollectionManipulationTests.cs index fbb539f10..81219f478 100644 --- a/Src/DependencyCollector/Shared.Tests/HeaderCollectionManipulationTests.cs +++ b/Src/DependencyCollector/Shared.Tests/HeaderCollectionManipulationTests.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Net; using Microsoft.ApplicationInsights.Common; - using Microsoft.ApplicationInsights.W3C; using VisualStudio.TestTools.UnitTesting; [TestClass] @@ -163,12 +162,11 @@ public void SetNameValueHeaderWithNonEmptyCollectionSetsHeader() Assert.AreEqual("k1=v1,k2=v2,k1=v3", headers["Correlation-Context"]); } -#pragma warning disable 612, 618 [TestMethod] public void GetHeaderValueNoMax() { - WebHeaderCollection headers = new WebHeaderCollection { [W3CConstants.TraceStateHeader] = "k1=v1,k2=v2" }; - var values = headers.GetHeaderValue(W3CConstants.TraceStateHeader)?.ToList(); + WebHeaderCollection headers = new WebHeaderCollection { [W3C.W3CConstants.TraceStateHeader] = "k1=v1,k2=v2" }; + var values = headers.GetHeaderValue(W3C.W3CConstants.TraceStateHeader)?.ToList(); Assert.IsNotNull(values); Assert.AreEqual(2, values.Count); Assert.AreEqual("k1=v1", values.First()); @@ -182,8 +180,8 @@ public void GetHeaderValueNoMax() [Xunit.InlineData(13)] // k1=v1,k2=v2,k".Length public void GetHeaderValueMaxLenTruncatesEnd(int maxLength) { - WebHeaderCollection headers = new WebHeaderCollection { [W3CConstants.TraceStateHeader] = "k1=v1,k2=v2,k3=v3,k4=v4" }; - var values = headers.GetHeaderValue(W3CConstants.TraceStateHeader, maxLength)?.ToList(); + WebHeaderCollection headers = new WebHeaderCollection { [W3C.W3CConstants.TraceStateHeader] = "k1=v1,k2=v2,k3=v3,k4=v4" }; + var values = headers.GetHeaderValue(W3C.W3CConstants.TraceStateHeader, maxLength)?.ToList(); Assert.IsNotNull(values); Assert.AreEqual(2, values.Count); Assert.AreEqual("k1=v1", values.First()); @@ -195,22 +193,20 @@ public void GetHeaderValueMaxLenTruncatesEnd(int maxLength) [Xunit.InlineData(3)] public void GetHeaderValueMaxLenTruncatesEndInvalid(int maxLength) { - WebHeaderCollection headers = new WebHeaderCollection { [W3CConstants.TraceStateHeader] = "k1=v1,k2=v2" }; - var values = headers.GetHeaderValue(W3CConstants.TraceStateHeader, maxLength)?.ToList(); + WebHeaderCollection headers = new WebHeaderCollection { [W3C.W3CConstants.TraceStateHeader] = "k1=v1,k2=v2" }; + var values = headers.GetHeaderValue(W3C.W3CConstants.TraceStateHeader, maxLength)?.ToList(); Assert.IsNull(values); } [TestMethod] public void GetHeaderValueMaxItemsTruncatesEnd() { - WebHeaderCollection headers = new WebHeaderCollection { [W3CConstants.TraceStateHeader] = "k1=v1,k2=v2,k3=v3,k4=v4" }; - var values = headers.GetHeaderValue(W3CConstants.TraceStateHeader, 100500, 2)?.ToList(); + WebHeaderCollection headers = new WebHeaderCollection { [W3C.W3CConstants.TraceStateHeader] = "k1=v1,k2=v2,k3=v3,k4=v4" }; + var values = headers.GetHeaderValue(W3C.W3CConstants.TraceStateHeader, 100500, 2)?.ToList(); Assert.IsNotNull(values); Assert.AreEqual(2, values.Count); Assert.AreEqual("k1=v1", values.First()); Assert.AreEqual("k2=v2", values.Last()); } - -#pragma warning restore 612, 618 } } diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs index a34f01588..0094548f2 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/DependencyCollectorDiagnosticListenerTests.Netstandard20.cs @@ -15,8 +15,8 @@ namespace Microsoft.ApplicationInsights.Tests using Microsoft.ApplicationInsights.DependencyCollector.Implementation; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.TestFramework; - using Microsoft.ApplicationInsights.W3C; using Microsoft.VisualStudio.TestTools.UnitTesting; /// @@ -77,7 +77,6 @@ public void OnActivityStartInjectsLegacyHeaders() } } -#pragma warning disable 612, 618 /// /// Tests that OnStartActivity injects W3C headers. /// @@ -103,8 +102,8 @@ public void OnActivityStartInjectsW3CHeaders() // Request-Id and Correlation-Context are injected by HttpClient // check only W3C headers here Assert.AreEqual(this.testApplicationId1, GetRequestContextKeyValue(requestMsg, RequestResponseHeaders.RequestContextCorrelationSourceKey)); - Assert.AreEqual($"00-{activity.GetTraceId()}-{activity.GetSpanId()}-02", requestMsg.Headers.GetValues(W3CConstants.TraceParentHeader).Single()); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={this.testApplicationId1}", requestMsg.Headers.GetValues(W3CConstants.TraceStateHeader).Single()); + Assert.AreEqual($"00-{activity.GetTraceId()}-{activity.GetSpanId()}-02", requestMsg.Headers.GetValues(W3C.W3CConstants.TraceParentHeader).Single()); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={this.testApplicationId1}", requestMsg.Headers.GetValues(W3C.W3CConstants.TraceStateHeader).Single()); } } @@ -136,16 +135,14 @@ public void OnActivityStartInjectsW3CHeadersAndTracksLegacyId() var telemetry = this.sentTelemetry.Single() as DependencyTelemetry; Assert.IsNotNull(telemetry); - Assert.IsTrue(telemetry.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.AreEqual(activity.Id, telemetry.Properties[W3CConstants.LegacyRequestIdProperty]); + Assert.IsTrue(telemetry.Properties.ContainsKey(W3C.W3CConstants.LegacyRequestIdProperty)); + Assert.AreEqual(activity.Id, telemetry.Properties[W3C.W3CConstants.LegacyRequestIdProperty]); - Assert.IsTrue(telemetry.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty)); - Assert.AreEqual(activity.RootId, telemetry.Properties[W3CConstants.LegacyRootIdProperty]); + Assert.IsTrue(telemetry.Properties.ContainsKey(W3C.W3CConstants.LegacyRootIdProperty)); + Assert.AreEqual(activity.RootId, telemetry.Properties[W3C.W3CConstants.LegacyRootIdProperty]); } } -#pragma warning restore 612, 618 - /// /// Tests that OnStopActivity tracks telemetry. /// diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs index 3d31c8425..7d2d4cb85 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ProfilerHttpProcessingTest.cs @@ -19,13 +19,11 @@ using Microsoft.ApplicationInsights.DependencyCollector.Implementation.Operation; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.TestFramework; - using Microsoft.ApplicationInsights.W3C; using Microsoft.ApplicationInsights.Web.TestFramework; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable 618 - [TestClass] public sealed class ProfilerHttpProcessingTest : IDisposable { @@ -268,7 +266,6 @@ public void RddTestHttpProcessingProfilerOnBeginAddsCorrelationContextHeader() Assert.IsTrue(actualCorrelationContextHeader == "Key2=Value2,Key1=Value1" || actualCorrelationContextHeader == "Key1=Value1,Key2=Value2"); } -#pragma warning disable 612, 618 /// /// Ensures that the source request header is added when request is sent. /// @@ -295,11 +292,11 @@ public void RddTestHttpProcessingProfilerOnBeginAddsW3CHeadersWhenEnabled() using (var op = client.StartOperation("request")) { Activity.Current.AddBaggage("k", "v"); - Activity.Current.AddTag(W3CConstants.TracestateTag, "some=state"); + Activity.Current.AddTag(W3C.W3CConstants.TracestateTag, "some=state"); httpProcessingW3C.OnBeginForGetResponse(request); Assert.AreEqual("k=v", request.Headers[RequestResponseHeaders.CorrelationContextHeader]); - Assert.AreEqual($"{W3CConstants.AzureTracestateNamespace}={TestApplicationId},some=state", request.Headers[W3CConstants.TraceStateHeader]); + Assert.AreEqual($"{W3C.W3CConstants.AzureTracestateNamespace}={TestApplicationId},some=state", request.Headers[W3C.W3CConstants.TraceStateHeader]); requestTelemetry = op.Telemetry; @@ -319,11 +316,10 @@ public void RddTestHttpProcessingProfilerOnBeginAddsW3CHeadersWhenEnabled() var dependencyIdParts = dependency.Id.Split('.', '|'); Assert.AreEqual(4, dependencyIdParts.Length); - var traceParent = request.Headers[W3CConstants.TraceParentHeader]; - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{dependencyIdParts[1]}-{dependencyIdParts[2]}-{W3CConstants.TraceFlagRecordedAndNotRequested}", + var traceParent = request.Headers[W3C.W3CConstants.TraceParentHeader]; + Assert.AreEqual($"{W3C.W3CConstants.DefaultVersion}-{dependencyIdParts[1]}-{dependencyIdParts[2]}-{W3C.W3CConstants.TraceFlagRecordedAndNotRequested}", traceParent); } -#pragma warning restore 612, 618 /// /// Ensures that the source request header is not added, as per the config, when request is sent. diff --git a/Src/DependencyCollector/Shared.Tests/Implementation/ServiceBusDiagnosticListenerTests.cs b/Src/DependencyCollector/Shared.Tests/Implementation/ServiceBusDiagnosticListenerTests.cs index ee586b68f..154239413 100644 --- a/Src/DependencyCollector/Shared.Tests/Implementation/ServiceBusDiagnosticListenerTests.cs +++ b/Src/DependencyCollector/Shared.Tests/Implementation/ServiceBusDiagnosticListenerTests.cs @@ -11,7 +11,7 @@ using Microsoft.ApplicationInsights.DependencyCollector.Implementation; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.Web.TestFramework; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -258,7 +258,6 @@ public void ServiceBusProcessHandingWithoutParent() } } -#pragma warning disable 612, 618 [TestMethod] public void ServiceBusProcessHandingW3C() { @@ -330,7 +329,7 @@ public void ServiceBusProcessHandingExternalParentW3C() Assert.IsTrue(requestTelemetry.Success.Value); Assert.AreEqual("parent", requestTelemetry.Context.Operation.ParentId); - Assert.AreEqual("parent", requestTelemetry.Properties[W3CConstants.LegacyRootIdProperty]); + Assert.AreEqual("parent", requestTelemetry.Properties[W3C.W3CConstants.LegacyRootIdProperty]); Assert.AreEqual("messageId", requestTelemetry.Properties["MessageId"]); var traceTelemetry = this.sentItems.OfType(); @@ -411,7 +410,6 @@ public void ServiceBusProcessHandingExternalParentW3CCompatibleRequestId() Assert.AreEqual("messageId", telemetry.Properties["MessageId"]); } } -#pragma warning restore 612, 618 [TestMethod] public void ServiceBusExceptionsAreIgnored() diff --git a/Src/DependencyCollector/Shared.Tests/W3C/W3CActiviityExtentionsTests.cs b/Src/DependencyCollector/Shared.Tests/W3C/W3CActiviityExtentionsTests.cs deleted file mode 100644 index 4b7c19799..000000000 --- a/Src/DependencyCollector/Shared.Tests/W3C/W3CActiviityExtentionsTests.cs +++ /dev/null @@ -1,258 +0,0 @@ -namespace Microsoft.ApplicationInsights.DependencyCollector.W3C -{ - using System.Diagnostics; - using System.Linq; - using Microsoft.ApplicationInsights.W3C; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - [TestClass] - public class W3CActiviityExtentionsTests - { -#pragma warning disable 612, 618 - - private const string TraceId = "01010101010101010101010101010101"; - private const string ParenSpanId = "0202020202020202"; - - [TestCleanup] - public void Cleanup() - { - while (Activity.Current != null) - { - Activity.Current.Stop(); - } - } - - [TestMethod] - public void SetInvalidTraceParent() - { - var invalidTraceParents = new[] - { - "123", string.Empty, null, "00-00", "00-00-00", "00-00-00-", "-00-00-00", "00-00-00-00-00", - "00-00-00- ", " -00-00-00", "---", "00---", "00-00--", "00--00-", "00---00" - }; - foreach (var traceparent in invalidTraceParents) - { - var a = new Activity("foo"); - a.SetTraceparent(traceparent); - - Assert.IsFalse(a.Tags.Any(t => t.Key == W3CConstants.ParentSpanIdTag), traceparent); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNull(a.GetTracestate()); - - Assert.AreEqual(W3CConstants.DefaultVersion, a.Tags.Single(t => t.Key == W3CConstants.VersionTag).Value, traceparent); - Assert.AreEqual(W3CConstants.TraceFlagRecordedAndNotRequested, a.Tags.Single(t => t.Key == W3CConstants.SampledTag).Value, traceparent); - - Assert.IsTrue(a.IsW3CActivity(), traceparent); - Assert.AreEqual(32, a.GetTraceId().Length, traceparent); - Assert.AreEqual(16, a.GetSpanId().Length, traceparent); - - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{a.GetTraceId()}-{a.GetSpanId()}-{W3CConstants.TraceFlagRecordedAndNotRequested}", a.GetTraceparent(), traceparent); - } - } - - [TestMethod] - public void InvalidTraceIdAllTraceparentIsIgnored() - { - var invalidTraceIds = new[] - { - "123", - "000102030405060708090a0b0c0d0f", // 30 chars - "000102030405060708090a0b0c0d0f0", // 31 char - "000102030405060708090a0b0c0d0f0g", // 32 char non-hex - "000102030405060708090a0b0c0d0f0A", // 32 char upper case - "000102030405060708090a0b0c0d0f000" // 33 chars - }; - foreach (var traceId in invalidTraceIds) - { - var a = new Activity("foo"); - - a.SetTraceparent($"00-{traceId}-{ParenSpanId}-00"); - - Assert.IsFalse(a.Tags.Any(t => t.Key == W3CConstants.ParentSpanIdTag), traceId); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNull(a.GetTracestate()); - - Assert.AreEqual(W3CConstants.DefaultVersion, a.Tags.Single(t => t.Key == W3CConstants.VersionTag).Value, traceId); - Assert.AreEqual(W3CConstants.TraceFlagRecordedAndNotRequested, a.Tags.Single(t => t.Key == W3CConstants.SampledTag).Value, traceId); - - Assert.IsTrue(a.IsW3CActivity(), traceId); - Assert.AreEqual(32, a.GetTraceId().Length, traceId); - Assert.AreEqual(16, a.GetSpanId().Length, traceId); - - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{a.GetTraceId()}-{a.GetSpanId()}-{W3CConstants.TraceFlagRecordedAndNotRequested}", a.GetTraceparent(), traceId); - } - } - - [TestMethod] - public void InvalidSapnIdAllTraceparentIsIgnored() - { - var invalidSpanIds = new[] - { - "123", - "00010203040506", // 14 chars - "000102030405060", // 15 char - "000102030405060g", // 16 char non-hex - "000102030405060A", // 16 char upper case - "00010203040506070" // 15 chars - }; - foreach (var parentSpanId in invalidSpanIds) - { - var a = new Activity("foo"); - - a.SetTraceparent($"00-{TraceId}-{parentSpanId}-00"); - - Assert.IsFalse(a.Tags.Any(t => t.Key == W3CConstants.ParentSpanIdTag), parentSpanId); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNull(a.GetTracestate()); - - Assert.AreEqual(W3CConstants.DefaultVersion, a.Tags.Single(t => t.Key == W3CConstants.VersionTag).Value, parentSpanId); - Assert.AreEqual(W3CConstants.TraceFlagRecordedAndNotRequested, a.Tags.Single(t => t.Key == W3CConstants.SampledTag).Value, parentSpanId); - - Assert.IsTrue(a.IsW3CActivity(), parentSpanId); - Assert.AreEqual(32, a.GetTraceId().Length, parentSpanId); - Assert.AreEqual(16, a.GetSpanId().Length, parentSpanId); - - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{a.GetTraceId()}-{a.GetSpanId()}-{W3CConstants.TraceFlagRecordedAndNotRequested}", a.GetTraceparent(), parentSpanId); - } - } - - [TestMethod] - public void SetValidTraceParent() - { - var a = new Activity("foo"); - a.SetTraceparent($"00-{TraceId}-{ParenSpanId}-00"); - - Assert.IsTrue(a.IsW3CActivity()); - Assert.AreEqual(TraceId, a.Tags.SingleOrDefault(t => t.Key == W3CConstants.TraceIdTag).Value); - Assert.AreEqual(ParenSpanId, a.Tags.SingleOrDefault(t => t.Key == W3CConstants.ParentSpanIdTag).Value); - Assert.IsNotNull(a.Tags.SingleOrDefault(t => t.Key == W3CConstants.SpanIdTag)); - Assert.AreEqual(16, a.Tags.Single(t => t.Key == W3CConstants.SpanIdTag).Value.Length); - Assert.AreEqual(W3CConstants.TraceFlagRecordedAndNotRequested, a.Tags.SingleOrDefault(t => t.Key == W3CConstants.SampledTag).Value); - Assert.AreEqual(W3CConstants.DefaultVersion, a.Tags.SingleOrDefault(t => t.Key == W3CConstants.VersionTag).Value); - - Assert.AreEqual(TraceId, a.GetTraceId()); - Assert.AreEqual(ParenSpanId, a.GetParentSpanId()); - Assert.IsNotNull(a.GetSpanId()); - Assert.AreEqual(a.Tags.Single(t => t.Key == W3CConstants.SpanIdTag).Value, a.GetSpanId()); - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{TraceId}-{a.GetSpanId()}-{W3CConstants.TraceFlagRecordedAndNotRequested}", a.GetTraceparent()); - Assert.IsNull(a.GetTracestate()); - } - - [TestMethod] - public void UpdateContextWithoutParent() - { - var a = new Activity("foo"); - - Assert.IsFalse(a.IsW3CActivity()); - - a.UpdateContextOnActivity(); - Assert.IsTrue(a.IsW3CActivity()); - Assert.IsNotNull(a.GetTraceId()); - Assert.IsNotNull(a.GetSpanId()); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNotNull(a.GetSpanId()); - - Assert.AreEqual($"00-{a.GetTraceId()}-{a.GetSpanId()}-02", a.GetTraceparent()); - Assert.IsNull(a.GetTracestate()); - } - - [TestMethod] - public void UpdateContextFromCompatibleRootId() - { - var a = new Activity("foo"); - a.SetParentId(TraceId); - - Assert.IsFalse(a.IsW3CActivity()); - - a.UpdateContextOnActivity(); - Assert.IsTrue(a.IsW3CActivity()); - Assert.AreEqual(TraceId, a.GetTraceId()); - Assert.IsNotNull(a.GetSpanId()); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNotNull(a.GetSpanId()); - - Assert.AreEqual($"00-{a.GetTraceId()}-{a.GetSpanId()}-02", a.GetTraceparent()); - Assert.IsNull(a.GetTracestate()); - } - - [TestMethod] - public void UpdateContextFromIncompatibleRootId() - { - var a = new Activity("foo"); - a.SetParentId("abc"); - - Assert.IsFalse(a.IsW3CActivity()); - - a.UpdateContextOnActivity(); - Assert.IsTrue(a.IsW3CActivity()); - Assert.AreNotEqual("abc", a.GetTraceId()); - Assert.IsNotNull(a.GetTraceId()); - Assert.IsNotNull(a.GetSpanId()); - Assert.IsNull(a.GetParentSpanId()); - Assert.IsNotNull(a.GetSpanId()); - - Assert.AreEqual($"00-{a.GetTraceId()}-{a.GetSpanId()}-02", a.GetTraceparent()); - Assert.IsNull(a.GetTracestate()); - } - - [TestMethod] - public void UpdateContextWithParent() - { - var parent = new Activity("foo").Start(); - parent.SetTraceparent($"00-{TraceId}-{ParenSpanId}-01"); - parent.SetTracestate("some=state"); - var child = new Activity("bar").Start(); - child.UpdateContextOnActivity(); - - Assert.IsTrue(child.IsW3CActivity()); - Assert.AreEqual(TraceId, child.GetTraceId()); - Assert.AreEqual(parent.GetSpanId(), child.GetParentSpanId()); - Assert.AreEqual($"{W3CConstants.DefaultVersion}-{TraceId}-{child.GetSpanId()}-{W3CConstants.TraceFlagRecordedAndRequested}", child.GetTraceparent()); - Assert.AreEqual(parent.GetTracestate(), child.GetTracestate()); - } - - [TestMethod] - public void SetTraceState() - { - var a = new Activity("foo").Start(); - a.SetTracestate("some=state"); - Assert.AreEqual("some=state", a.GetTracestate()); - } - - [TestMethod] - public void UnsupportedVersionsAreIgnored() - { - var a = new Activity("foo").Start(); - a.SetTraceparent($"12-{TraceId}-{ParenSpanId}-00"); - - var b = new Activity("bar").Start(); - b.SetTraceparent($"ff-{TraceId}-{ParenSpanId}-00"); - - Assert.AreEqual($"00-{TraceId}-{a.GetSpanId()}-02", a.GetTraceparent()); - Assert.AreEqual($"00-{TraceId}-{b.GetSpanId()}-02", b.GetTraceparent()); - } - - [TestMethod] - public void RequestedFlagIsRespected() - { - var requestedParents = new[] { "01", "03", "05", "ff" }; - var notRequestedParents = new[] { "00", "02", "04", "fe" }; - - foreach (var req in requestedParents) - { - var a = new Activity("foo").Start(); - a.SetTraceparent($"00-{TraceId}-{ParenSpanId}-{req}"); - Assert.AreEqual($"00-{TraceId}-{a.GetSpanId()}-03", a.GetTraceparent(), req); - } - - foreach (var notReq in notRequestedParents) - { - var a = new Activity("foo").Start(); - a.SetTraceparent($"00-{TraceId}-{ParenSpanId}-{notReq}"); - Assert.AreEqual($"00-{TraceId}-{a.GetSpanId()}-02", a.GetTraceparent(), notReq); - } - } - -#pragma warning restore 612, 618 - } -} diff --git a/Src/DependencyCollector/Shared.Tests/W3C/W3COperationCorrelationTelemetryInitializerTests.cs b/Src/DependencyCollector/Shared.Tests/W3C/W3COperationCorrelationTelemetryInitializerTests.cs deleted file mode 100644 index a4d5cd2a2..000000000 --- a/Src/DependencyCollector/Shared.Tests/W3C/W3COperationCorrelationTelemetryInitializerTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -namespace Microsoft.ApplicationInsights.DependencyCollector.W3C -{ - using System.Diagnostics; - using System.Linq; - using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.Extensibility.Implementation; - using Microsoft.ApplicationInsights.W3C; - using Microsoft.VisualStudio.TestTools.UnitTesting; - -#pragma warning disable 612, 618 - [TestClass] - public class W3COperationCorrelationTelemetryInitializerTests - { - [TestCleanup] - public void Cleanup() - { - while (Activity.Current != null) - { - Activity.Current.Stop(); - } - } - - [TestMethod] - public void InitializerCreatesNewW3CContext() - { - Activity a = new Activity("dummy") - .Start(); - - RequestTelemetry request = new RequestTelemetry(); - - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.IsNotNull(request.Context.Operation.Id); - Assert.IsNull(request.Context.Operation.ParentId); - Assert.AreEqual($"|{a.GetTraceId()}.{a.GetSpanId()}.", request.Id); - - Assert.AreEqual(2, request.Properties.Count); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.AreEqual(a.Id, request.Properties[W3CConstants.LegacyRequestIdProperty]); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty)); - Assert.AreEqual(a.RootId, request.Properties[W3CConstants.LegacyRootIdProperty]); - } - - [TestMethod] - public void InitializerSetsCorrelationIdsOnTraceTelemetry() - { - Activity a = new Activity("dummy") - .Start() - .GenerateW3CContext(); - - string expectedTrace = a.GetTraceId(); - string expectedParent = a.GetSpanId(); - - TraceTelemetry trace = new TraceTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(trace); - - Assert.AreEqual(expectedTrace, trace.Context.Operation.Id); - Assert.AreEqual($"|{expectedTrace}.{expectedParent}.", trace.Context.Operation.ParentId); - - Assert.IsFalse(trace.Properties.Any()); - } - - [TestMethod] - public void InitializerSetsCorrelationIdsOnRequestTelemetry() - { - Activity a = new Activity("dummy") - .Start() - .GenerateW3CContext(); - - string expectedTrace = a.GetTraceId(); - string expectedSpanId = a.GetSpanId(); - - string expectedParent = "0123456789abcdef"; - a.AddTag(W3CConstants.ParentSpanIdTag, expectedParent); - - RequestTelemetry request = new RequestTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.AreEqual(expectedTrace, request.Context.Operation.Id); - Assert.AreEqual($"|{expectedTrace}.{expectedParent}.", request.Context.Operation.ParentId); - Assert.AreEqual($"|{expectedTrace}.{expectedSpanId}.", request.Id); - - Assert.AreEqual(2, request.Properties.Count); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.AreEqual(a.Id, request.Properties[W3CConstants.LegacyRequestIdProperty]); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty)); - Assert.AreEqual(a.RootId, request.Properties[W3CConstants.LegacyRootIdProperty]); - } - - [TestMethod] - public void InitializerSetsCorrelationIdsOnRequestTelemetryNoParent() - { - Activity a = new Activity("dummy") - .Start() - .GenerateW3CContext(); - - string expectedTrace = a.GetTraceId(); - string expectedSpanId = a.GetSpanId(); - - RequestTelemetry request = new RequestTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.AreEqual(expectedTrace, request.Context.Operation.Id); - Assert.IsNull(request.Context.Operation.ParentId); - Assert.AreEqual($"|{expectedTrace}.{expectedSpanId}.", request.Id); - - Assert.AreEqual(2, request.Properties.Count); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRequestIdProperty)); - Assert.AreEqual(a.Id, request.Properties[W3CConstants.LegacyRequestIdProperty]); - - Assert.IsTrue(request.Properties.ContainsKey(W3CConstants.LegacyRootIdProperty)); - Assert.AreEqual(a.RootId, request.Properties[W3CConstants.LegacyRootIdProperty]); - } - - [TestMethod] - public void InitializerNoopWithoutActivity() - { - RequestTelemetry request = new RequestTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.IsNull(request.Context.Operation.Id); - Assert.IsNull(request.Context.Operation.ParentId); - - Assert.IsFalse(request.Properties.Any()); - } - - [TestMethod] - public void InitializerIgnoresExistingValues() - { - Activity a = new Activity("dummy") - .Start() - .GenerateW3CContext(); - - string expectedTrace = a.GetTraceId(); - string expectedSpanId = a.GetSpanId(); - - string expectedParent = "0123456789abcdef"; - a.AddTag(W3CConstants.ParentSpanIdTag, expectedParent); - - RequestTelemetry request = new RequestTelemetry(); - - request.Context.Operation.Id = "operation id"; - request.Context.Operation.ParentId = "parent id"; - request.Id = "id"; - - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.AreEqual(expectedTrace, request.Context.Operation.Id); - Assert.AreEqual($"|{expectedTrace}.{expectedParent}.", request.Context.Operation.ParentId); - Assert.AreEqual($"|{expectedTrace}.{expectedSpanId}.", request.Id); - } - - [TestMethod] - public void InitializerPopulatesTraceStateOnRequestAndDependencyTelemetry() - { - Activity a = new Activity("dummy") - .Start() - .GenerateW3CContext(); - - a.SetTracestate("key=value"); - - string expectedTrace = a.GetTraceId(); - string expectedSpanId = a.GetSpanId(); - - RequestTelemetry request = new RequestTelemetry(); - DependencyTelemetry dependency = new DependencyTelemetry(); - TraceTelemetry trace = new TraceTelemetry(); - var initializer = new W3COperationCorrelationTelemetryInitializer(); - initializer.Initialize(request); - initializer.Initialize(dependency); - initializer.Initialize(trace); - - Assert.AreEqual(expectedTrace, request.Context.Operation.Id); - Assert.AreEqual($"|{expectedTrace}.{expectedSpanId}.", request.Id); - - Assert.AreEqual("key=value", request.Properties[W3CConstants.TracestateTag]); - Assert.AreEqual("key=value", dependency.Properties[W3CConstants.TracestateTag]); - Assert.IsFalse(trace.Properties.Any()); - } - - [TestMethod] - public void InitializerOnNestedActivitities() - { - Activity requestActivity = new Activity("request") - .Start(); - - RequestTelemetry request = new RequestTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Activity nested1 = new Activity("nested1").Start(); - Activity nested2 = new Activity("nested1").Start(); - - DependencyTelemetry dependency2 = new DependencyTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(dependency2); - - Assert.AreEqual(request.Context.Operation.Id, nested2.GetTraceId()); - Assert.AreEqual(request.Context.Operation.Id, nested1.GetTraceId()); - - Assert.AreEqual(request.Id, $"|{nested1.GetTraceId()}.{nested1.GetParentSpanId()}."); - Assert.AreEqual(nested1.GetSpanId(), nested2.GetParentSpanId()); - - Assert.AreEqual(request.Context.Operation.Id, dependency2.Context.Operation.Id); - - nested2.Stop(); - - DependencyTelemetry dependency1 = new DependencyTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(dependency1); - - Assert.AreEqual(request.Id, $"|{nested1.GetTraceId()}.{nested1.GetParentSpanId()}."); - Assert.AreEqual(dependency2.Context.Operation.ParentId, dependency1.Id); - Assert.AreEqual(request.Context.Operation.Id, dependency1.Context.Operation.Id); - Assert.AreEqual(request.Id, dependency1.Context.Operation.ParentId); - } - - [TestMethod] - public void InitializerOnSqlDepenedency() - { - Activity requestActivity = new Activity("request") - .Start() - .GenerateW3CContext(); - - RequestTelemetry request = new RequestTelemetry(); - DependencyTelemetry sqlDependency = new DependencyTelemetry() - { - Type = "SQL" - }; - sqlDependency.Context.GetInternalContext().SdkVersion = "rdddsc:12345"; - string expectedId = sqlDependency.Id; - - new W3COperationCorrelationTelemetryInitializer().Initialize(sqlDependency); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.AreEqual(request.Context.Operation.Id, sqlDependency.Context.Operation.Id); - Assert.AreEqual(request.Id, sqlDependency.Context.Operation.ParentId); - Assert.AreEqual(expectedId, sqlDependency.Id); - } - - [TestMethod] - public void InitializerOnActivityWithParentWithoutW3CTags() - { - Activity parentActivity = new Activity("parent") - .Start(); - Activity childActivity = new Activity("child") - .Start(); - - RequestTelemetry request = new RequestTelemetry(); - new W3COperationCorrelationTelemetryInitializer().Initialize(request); - - Assert.AreEqual(request.Context.Operation.Id, parentActivity.GetTraceId()); - Assert.AreEqual(request.Context.Operation.Id, childActivity.GetTraceId()); - Assert.AreEqual(request.Id, $"|{childActivity.GetTraceId()}.{childActivity.GetSpanId()}."); - Assert.AreEqual(request.Context.Operation.ParentId, $"|{childActivity.GetTraceId()}.{parentActivity.GetSpanId()}."); - } - } -#pragma warning restore 612, 618 -} diff --git a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs index 6860a0f1b..1d09fe8c3 100644 --- a/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs +++ b/Src/DependencyCollector/Shared/HttpCoreDiagnosticSourceListener.cs @@ -16,7 +16,7 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; internal class HttpCoreDiagnosticSourceListener : IObserver>, IDisposable { @@ -326,7 +326,7 @@ internal void OnActivityStart(HttpRequestMessage request) // with W3C support on .NET https://github.com/dotnet/corefx/issues/30331 (TODO) if (currentActivity.Parent == null && currentActivity.ParentId == null) { - currentActivity.UpdateParent(StringUtilities.GenerateTraceId()); + currentActivity.UpdateParent(W3CUtilities.GenerateTraceId()); } // end of workaround @@ -433,7 +433,7 @@ internal void OnRequest(HttpRequestMessage request, Guid loggingRequestId) var dependency = Activity.Current != null ? this.client.StartOperation(resourceName) : - this.client.StartOperation(resourceName, StringUtilities.GenerateTraceId()); + this.client.StartOperation(resourceName, W3CUtilities.GenerateTraceId()); dependency.Telemetry.Target = DependencyTargetNameHelper.GetDependencyTargetName(requestUri); dependency.Telemetry.Type = RemoteDependencyConstants.HTTP; @@ -494,7 +494,6 @@ private static void InjectCorrelationContext(HttpRequestHeaders requestHeaders, } } -#pragma warning disable 612, 618 private void InjectRequestHeaders(HttpRequestMessage request, string instrumentationKey, bool isLegacyEvent = false) { try @@ -549,13 +548,13 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta { currentActivity.UpdateContextOnActivity(); string traceParent = currentActivity.GetTraceparent(); - if (traceParent != null && !requestHeaders.Contains(W3CConstants.TraceParentHeader)) + if (traceParent != null && !requestHeaders.Contains(W3C.W3CConstants.TraceParentHeader)) { - requestHeaders.Add(W3CConstants.TraceParentHeader, traceParent); + requestHeaders.Add(W3C.W3CConstants.TraceParentHeader, traceParent); } string traceState = currentActivity.GetTracestate(); - if (!requestHeaders.Contains(W3CConstants.TraceStateHeader)) + if (!requestHeaders.Contains(W3C.W3CConstants.TraceStateHeader)) { if (sourceApplicationId != null) { @@ -574,7 +573,7 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta if (traceState != null) { - requestHeaders.Add(W3CConstants.TraceStateHeader, traceState); + requestHeaders.Add(W3C.W3CConstants.TraceStateHeader, traceState); } } } @@ -585,7 +584,6 @@ private void InjectRequestHeaders(HttpRequestMessage request, string instrumenta AppMapCorrelationEventSource.Log.UnknownError(ExceptionUtilities.GetExceptionDetailString(e)); } } -#pragma warning restore 612, 618 private void ParseResponse(HttpResponseMessage response, DependencyTelemetry telemetry) { diff --git a/Src/DependencyCollector/Shared/Implementation/ClientServerDependencyTracker.cs b/Src/DependencyCollector/Shared/Implementation/ClientServerDependencyTracker.cs index 5fba2869c..fa836fde3 100644 --- a/Src/DependencyCollector/Shared/Implementation/ClientServerDependencyTracker.cs +++ b/Src/DependencyCollector/Shared/Implementation/ClientServerDependencyTracker.cs @@ -4,9 +4,8 @@ using System.Data.SqlClient; using System.Diagnostics; using System.Net; - using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; internal static class ClientServerDependencyTracker { @@ -72,7 +71,7 @@ internal static DependencyTelemetry BeginTracking(TelemetryClient telemetryClien // with W3C support on .NET https://github.com/dotnet/corefx/issues/30331 (TODO) if (currentActivity == null) { - activity.SetParentId(StringUtilities.GenerateTraceId()); + activity.SetParentId(W3CUtilities.GenerateTraceId()); } // end of workaround @@ -82,9 +81,7 @@ internal static DependencyTelemetry BeginTracking(TelemetryClient telemetryClien if (IsW3CEnabled) { -#pragma warning disable 612, 618 - W3COperationCorrelationTelemetryInitializer.UpdateTelemetry(telemetry, activity, true); -#pragma warning restore 612, 618 + activity.UpdateTelemetry(telemetry, true); } else { diff --git a/Src/DependencyCollector/Shared/Implementation/EventHandlers/EventHubsDiagnosticsEventHandler.cs b/Src/DependencyCollector/Shared/Implementation/EventHandlers/EventHubsDiagnosticsEventHandler.cs index 61c051aab..b36bc565a 100644 --- a/Src/DependencyCollector/Shared/Implementation/EventHandlers/EventHubsDiagnosticsEventHandler.cs +++ b/Src/DependencyCollector/Shared/Implementation/EventHandlers/EventHubsDiagnosticsEventHandler.cs @@ -7,6 +7,7 @@ using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.ApplicationInsights.Extensibility.W3C; /// /// Implements EventHubs DiagnosticSource events handling. @@ -44,7 +45,7 @@ public override void OnEvent(KeyValuePair evnt, DiagnosticListen // with W3C support on .NET https://github.com/dotnet/corefx/issues/30331 (TODO) if (currentActivity.Parent == null && currentActivity.ParentId == null) { - currentActivity.UpdateParent(StringUtilities.GenerateTraceId()); + currentActivity.UpdateParent(W3CUtilities.GenerateTraceId()); } break; diff --git a/Src/DependencyCollector/Shared/Implementation/EventHandlers/ServiceBusDiagnosticsEventHandler.cs b/Src/DependencyCollector/Shared/Implementation/EventHandlers/ServiceBusDiagnosticsEventHandler.cs index cd7983e20..0b7af16dc 100644 --- a/Src/DependencyCollector/Shared/Implementation/EventHandlers/ServiceBusDiagnosticsEventHandler.cs +++ b/Src/DependencyCollector/Shared/Implementation/EventHandlers/ServiceBusDiagnosticsEventHandler.cs @@ -7,6 +7,7 @@ using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.ApplicationInsights.Extensibility.W3C; /// /// Implements ServiceBus DiagnosticSource events handling. @@ -57,7 +58,7 @@ public override void OnEvent(KeyValuePair evnt, DiagnosticListen // with W3C support on .NET https://github.com/dotnet/corefx/issues/30331 (TODO) if (currentActivity.Parent == null && currentActivity.ParentId == null) { - currentActivity.UpdateParent(StringUtilities.GenerateTraceId()); + currentActivity.UpdateParent(W3CUtilities.GenerateTraceId()); } } else if (evnt.Key.EndsWith(TelemetryDiagnosticSourceListener.ActivityStopNameSuffix, StringComparison.Ordinal)) diff --git a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs index 9437885a5..9bf666859 100644 --- a/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs +++ b/Src/DependencyCollector/Shared/Implementation/HttpProcessing.cs @@ -11,7 +11,7 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; /// /// Concrete class with all processing logic to generate RDD data from the callbacks @@ -210,17 +210,16 @@ internal object OnBegin(object thisObj, bool injectCorrelationHeaders = true) } } -#pragma warning disable 612, 618 if (this.injectW3CHeaders && currentActivity != null) { string traceParent = currentActivity.GetTraceparent(); - if (traceParent != null && webRequest.Headers[W3CConstants.TraceParentHeader] == null) + if (traceParent != null && webRequest.Headers[W3C.W3CConstants.TraceParentHeader] == null) { - webRequest.Headers.Add(W3CConstants.TraceParentHeader, traceParent); + webRequest.Headers.Add(W3C.W3CConstants.TraceParentHeader, traceParent); } string traceState = currentActivity.GetTracestate(); - if (webRequest.Headers[W3CConstants.TraceStateHeader] == null) + if (webRequest.Headers[W3C.W3CConstants.TraceStateHeader] == null) { if (applicationId != null) { @@ -238,11 +237,10 @@ internal object OnBegin(object thisObj, bool injectCorrelationHeaders = true) if (traceState != null) { - webRequest.Headers.Add(W3CConstants.TraceStateHeader, traceState); + webRequest.Headers.Add(W3C.W3CConstants.TraceStateHeader, traceState); } } } -#pragma warning restore 612, 618 } } catch (Exception exception) diff --git a/Src/HostingStartup/HostingStartup/HostingStartup.csproj b/Src/HostingStartup/HostingStartup/HostingStartup.csproj index 97c06784a..158a057e9 100644 --- a/Src/HostingStartup/HostingStartup/HostingStartup.csproj +++ b/Src/HostingStartup/HostingStartup/HostingStartup.csproj @@ -36,7 +36,7 @@ - + diff --git a/Src/PerformanceCollector/PerformanceCollector/Perf.csproj b/Src/PerformanceCollector/PerformanceCollector/Perf.csproj index 3935c9d07..7d909ac6d 100644 --- a/Src/PerformanceCollector/PerformanceCollector/Perf.csproj +++ b/Src/PerformanceCollector/PerformanceCollector/Perf.csproj @@ -37,7 +37,7 @@ - + diff --git a/Src/Web/Web.Net45.Tests/AspNetDiagnosticTelemetryModuleTest.cs b/Src/Web/Web.Net45.Tests/AspNetDiagnosticTelemetryModuleTest.cs index 80049555b..0293c262c 100644 --- a/Src/Web/Web.Net45.Tests/AspNetDiagnosticTelemetryModuleTest.cs +++ b/Src/Web/Web.Net45.Tests/AspNetDiagnosticTelemetryModuleTest.cs @@ -11,6 +11,7 @@ using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.W3C.Internal; using Microsoft.ApplicationInsights.Web; using Microsoft.ApplicationInsights.Web.Helpers; @@ -18,7 +19,6 @@ using Microsoft.AspNet.TelemetryCorrelation; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable 612, 618 [TestClass] public class AspNetDiagnosticTelemetryModuleTest : IDisposable { @@ -747,5 +747,4 @@ private void Dispose(bool dispose) } } } -#pragma warning restore 612, 618 } \ No newline at end of file diff --git a/Src/Web/Web.Net45.Tests/RequestTrackingTelemetryModuleTest.Net45.cs b/Src/Web/Web.Net45.Tests/RequestTrackingTelemetryModuleTest.Net45.cs index 12f8f7766..866eafc29 100644 --- a/Src/Web/Web.Net45.Tests/RequestTrackingTelemetryModuleTest.Net45.cs +++ b/Src/Web/Web.Net45.Tests/RequestTrackingTelemetryModuleTest.Net45.cs @@ -10,13 +10,13 @@ using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.W3C.Internal; using Microsoft.ApplicationInsights.Web.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using Assert = Xunit.Assert; -#pragma warning disable 612, 618 /// /// NET 4.5 specific tests for RequestTrackingTelemetryModule. /// @@ -606,5 +606,4 @@ private void TestRequestTrackingWithW3CSupportEnabledAndNoW3CHeaders(bool startA } } } -#pragma warning restore 612, 618 } \ No newline at end of file diff --git a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs index a475b1c3e..691aa1158 100644 --- a/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs +++ b/Src/Web/Web.Shared.Net.Tests/RequestTrackingTelemetryModuleTest.cs @@ -13,8 +13,8 @@ using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.TestFramework; - using Microsoft.ApplicationInsights.W3C.Internal; using Microsoft.ApplicationInsights.Web.Helpers; using Microsoft.ApplicationInsights.Web.Implementation; using Microsoft.ApplicationInsights.Web.TestFramework; @@ -490,12 +490,10 @@ private RequestTrackingTelemetryModule RequestTrackingTelemetryModuleFactory(Tel config = this.CreateDefaultConfig(HttpModuleHelper.GetFakeHttpContext()); } -#pragma warning disable 612, 618 if (enableW3CTracing) { config.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer()); } -#pragma warning restore 612, 618 module.Initialize(config); diff --git a/Src/Web/Web.Shared.Net/Implementation/ActivityHelpers.cs b/Src/Web/Web.Shared.Net/Implementation/ActivityHelpers.cs index 9a733d0fe..eeb23e34c 100644 --- a/Src/Web/Web.Shared.Net/Implementation/ActivityHelpers.cs +++ b/Src/Web/Web.Shared.Net/Implementation/ActivityHelpers.cs @@ -7,10 +7,10 @@ namespace Microsoft.ApplicationInsights.Common using System.Web; using Microsoft.ApplicationInsights.DataContracts; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; + using Microsoft.ApplicationInsights.W3C.Internal; using Microsoft.ApplicationInsights.Web.Implementation; -#pragma warning disable 612, 618 internal class ActivityHelpers { internal const string RequestActivityItemName = "Microsoft.ApplicationInsights.Web.Activity"; @@ -138,5 +138,4 @@ private static bool TryExtractAppIdFromAzureTracestate(string azTracestate, out return true; } } -#pragma warning restore 612, 618 } \ No newline at end of file diff --git a/Src/Web/Web.Shared.Net/Implementation/RequestTrackingExtensions.cs b/Src/Web/Web.Shared.Net/Implementation/RequestTrackingExtensions.cs index a4a91b8fd..83e9a884c 100644 --- a/Src/Web/Web.Shared.Net/Implementation/RequestTrackingExtensions.cs +++ b/Src/Web/Web.Shared.Net/Implementation/RequestTrackingExtensions.cs @@ -6,10 +6,10 @@ using System.Web; using Microsoft.ApplicationInsights.Common; using Microsoft.ApplicationInsights.DataContracts; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.W3C.Internal; using Microsoft.AspNet.TelemetryCorrelation; -#pragma warning disable 612, 618 internal static class RequestTrackingExtensions { internal static RequestTelemetry CreateRequestTelemetryPrivate( @@ -35,7 +35,7 @@ internal static RequestTelemetry CreateRequestTelemetryPrivate( ActivityHelpers.ExtractTracestate(platformContext.Request, currentActivity, result); // length enforced in SetW3CContext currentActivity.SetParentId(currentActivity.GetTraceId()); - W3COperationCorrelationTelemetryInitializer.UpdateTelemetry(result, currentActivity, true); + currentActivity.UpdateTelemetry(result, true); SetLegacyContextIds(platformContext.Request, result); } @@ -64,7 +64,7 @@ internal static RequestTelemetry CreateRequestTelemetryPrivate( // So if there is no current Activity (i.e. there were no Request-Id header in the incoming request), we'll override ParentId on // the current Activity by the properly formatted one. This workaround should go away // with W3C support on .NET https://github.com/dotnet/corefx/issues/30331 - currentActivity.SetParentId(StringUtilities.GenerateTraceId()); + currentActivity.SetParentId(W3CUtilities.GenerateTraceId()); // end of workaround } @@ -82,7 +82,7 @@ internal static RequestTelemetry CreateRequestTelemetryPrivate( ActivityHelpers.ExtractTracestate(platformContext.Request, currentActivity, result); - W3COperationCorrelationTelemetryInitializer.UpdateTelemetry(result, currentActivity, true); + currentActivity.UpdateTelemetry(result, true); SetLegacyContextIds(platformContext.Request, result); } else if (ActivityHelpers.IsHierarchicalRequestId(currentActivity.ParentId)) @@ -217,5 +217,4 @@ private static void SetLegacyContextIds(HttpRequest request, RequestTelemetry re } } } -#pragma warning restore 612, 618 } \ No newline at end of file diff --git a/Src/Web/Web/AspNetDiagnosticTelemetryModule.cs b/Src/Web/Web/AspNetDiagnosticTelemetryModule.cs index 1f445e6f8..3abc1a697 100644 --- a/Src/Web/Web/AspNetDiagnosticTelemetryModule.cs +++ b/Src/Web/Web/AspNetDiagnosticTelemetryModule.cs @@ -8,10 +8,9 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation; using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing; - using Microsoft.ApplicationInsights.W3C; + using Microsoft.ApplicationInsights.Extensibility.W3C; using Microsoft.ApplicationInsights.Web.Implementation; -#pragma warning disable 612, 618 /// /// Listens to ASP.NET DiagnosticSource and enables instrumentation with Activity: let ASP.NET create root Activity for the request. /// @@ -169,7 +168,7 @@ public AspNetEventObserver(RequestTrackingTelemetryModule requestModule, Excepti string traceId = ActivityHelpers.IsW3CTracingEnabled ? activity.GetTraceId() - : StringUtilities.GenerateTraceId(); + : W3CUtilities.GenerateTraceId(); // As a first step in supporting W3C protocol in ApplicationInsights, // we want to generate Activity Ids in the W3C compatible format. @@ -257,5 +256,4 @@ private static bool IsFirstRequest(HttpContext context) } } } -#pragma warning restore 612, 618 } diff --git a/Src/Web/Web/Web.csproj b/Src/Web/Web/Web.csproj index 1c8a7da90..6e56ffa56 100644 --- a/Src/Web/Web/Web.csproj +++ b/Src/Web/Web/Web.csproj @@ -36,7 +36,7 @@ - + diff --git a/Src/WindowsServer/WindowsServer.NetCore.Tests/WindowsServer.NetCore.Tests.csproj b/Src/WindowsServer/WindowsServer.NetCore.Tests/WindowsServer.NetCore.Tests.csproj index 3e1e9a589..3084037f6 100644 --- a/Src/WindowsServer/WindowsServer.NetCore.Tests/WindowsServer.NetCore.Tests.csproj +++ b/Src/WindowsServer/WindowsServer.NetCore.Tests/WindowsServer.NetCore.Tests.csproj @@ -24,7 +24,7 @@ - + diff --git a/Src/WindowsServer/WindowsServer/WindowsServer.csproj b/Src/WindowsServer/WindowsServer/WindowsServer.csproj index 3b81a80fa..ee4622215 100644 --- a/Src/WindowsServer/WindowsServer/WindowsServer.csproj +++ b/Src/WindowsServer/WindowsServer/WindowsServer.csproj @@ -34,8 +34,8 @@ - - + +