From 26a407e3f607f454b8b06f9497eb8b2874ff495f Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Tue, 27 Aug 2019 08:09:10 -0700 Subject: [PATCH] add event log events for correlation --- .../HostingDiagnosticListener.cs | 10 +++++ .../Tracing/AspNetCoreEventSource.cs | 38 ++++++++++++++++++- .../Extensions/RequestCollectionOptions.cs | 11 ++++-- .../RequestTrackingTelemetryModule.cs | 2 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs b/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs index df47cb5..6b857ba 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs @@ -207,12 +207,14 @@ InjectionGuardConstants.TraceParentHeaderMaxLength); originalParentId = parentTraceParent; traceParentPresent = true; + AspNetCoreEventSource.Instance.HostingListenerInformational("2", "Retrieved trace parent from headers."); } // Scenario #1. No incoming correlation headers. if (originalParentId == null) { // Nothing to do here. + AspNetCoreEventSource.Instance.HostingListenerInformational("2", "OriginalParentId is null."); } else if (traceParentPresent) { @@ -220,6 +222,7 @@ // We need to ignore the Activity created by Hosting, as it did not take W3CTraceParent into consideration. newActivity = new Activity(ActivityCreatedByHostingDiagnosticListener); newActivity.SetParentId(originalParentId); + AspNetCoreEventSource.Instance.HostingListenerInformational("2", "Ignoring original Activity from Hosting to create new one using traceparent header retrieved by sdk."); // read and populate tracestate ReadTraceState(httpContext.Request.Headers, newActivity); @@ -238,6 +241,7 @@ { newActivity = new Activity(ActivityCreatedByHostingDiagnosticListener); newActivity.SetParentId(ActivityTraceId.CreateFromString(traceId), default(ActivitySpanId), ActivityTraceFlags.None); + AspNetCoreEventSource.Instance.HostingListenerInformational("2", "Ignoring original Activity from Hosting to create new one using w3c compatible request-id."); foreach (var bag in currentActivity.Baggage) { @@ -248,6 +252,7 @@ { // store rootIdFromOriginalParentId in custom Property legacyRootId = ExtractOperationIdFromRequestId(originalParentId); + AspNetCoreEventSource.Instance.HostingListenerInformational("2", "Incoming Request-ID is not W3C Compatible, and hence will be ignored for ID generation, but stored in custom property legacy_rootID."); } } } @@ -581,6 +586,8 @@ activity.AddBaggage(itemName, itemValue); } } + + AspNetCoreEventSource.Instance.HostingListenerVerboe("Correlation-Context retrived from header and stored into activity baggage."); } } @@ -593,6 +600,7 @@ // make in the request context can continue propogation // of tracestate. activity.TraceStateString = traceState; + AspNetCoreEventSource.Instance.HostingListenerVerboe("TraceState retrived from header and stored into activity.TraceState"); } } @@ -673,11 +681,13 @@ var traceId = activity.TraceId.ToHexString(); requestTelemetry.Id = FormatTelemetryId(traceId, activity.SpanId.ToHexString()); requestTelemetry.Context.Operation.Id = traceId; + AspNetCoreEventSource.Instance.RequestTelemetryCreated("W3C", requestTelemetry.Id, traceId); } else { requestTelemetry.Context.Operation.Id = activity.RootId; requestTelemetry.Id = activity.Id; + AspNetCoreEventSource.Instance.RequestTelemetryCreated("Hierrarchical", requestTelemetry.Id, requestTelemetry.Context.Operation.Id); } if (this.proactiveSamplingEnabled diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Extensibility/Implementation/Tracing/AspNetCoreEventSource.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Extensibility/Implementation/Tracing/AspNetCoreEventSource.cs index 8034aa2..50aa2d1 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Extensibility/Implementation/Tracing/AspNetCoreEventSource.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Extensibility/Implementation/Tracing/AspNetCoreEventSource.cs @@ -193,7 +193,7 @@ namespace Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation. { this.WriteEvent(17, errorMessage, this.ApplicationName); } - + /// /// Logs an event when a telemetry item is sampled out at head. /// @@ -207,6 +207,42 @@ namespace Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation. this.WriteEvent(18, operationId, this.ApplicationName); } + /// + /// Logs an informational event from Hosting listeners. + /// + [Event( + 19, + Message = "Hosting Major Version: '{0}'. Informational Message: '{1}'.", + Level = EventLevel.Informational)] + public void HostingListenerInformational(string hostingVersion, string message, string appDomainName = "Incorrect") + { + this.WriteEvent(19, hostingVersion, message, this.ApplicationName); + } + + /// + /// Logs a verbose event. + /// + [Event( + 20, + Message = "Message: '{0}'.", + Level = EventLevel.Verbose)] + public void HostingListenerVerboe(string message, string appDomainName = "Incorrect") + { + this.WriteEvent(20, message, this.ApplicationName); + } + + /// + /// Logs an event for RequestTelemetry created. + /// + [Event( + 21, + Message = "RequestTelemetry created. CorrelationFormat: '{0}', RequestID: '{1}', OperationId : '{2}' ", + Level = EventLevel.Informational)] + public void RequestTelemetryCreated(string correlationFormat, string requestId, string requestOperationId, string appDomainName = "Incorrect") + { + this.WriteEvent(21, correlationFormat, requestId, requestOperationId, this.ApplicationName); + } + /// /// Keywords for the AspNetEventSource. /// diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/RequestCollectionOptions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/RequestCollectionOptions.cs index 96c6640..dc0c13b 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/RequestCollectionOptions.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/RequestCollectionOptions.cs @@ -6,7 +6,8 @@ public class RequestCollectionOptions { /// - /// Creates new instance of class and fills default values. + /// Initializes a new instance of the class + /// and populates default values. /// public RequestCollectionOptions() { @@ -23,17 +24,19 @@ } /// - /// Get or sets value indicating whether Request-Context header is injected into the response. + /// Gets or sets a value indicating whether Request-Context header is injected into the response. /// public bool InjectResponseHeaders { get; set; } /// - /// Get or sets value indicating whether exceptions are be tracked. + /// Gets or sets a value indicating whether exceptions are be tracked by the RequestCOllectionModule. + /// Exceptions could be tracked by ApplicationInsightsLoggerProvider as well which is not affected by + /// this setting. /// public bool TrackExceptions { get; set; } /// - /// Get or sets value indicating whether W3C distributed tracing standard is enabled. + /// Gets or sets a value indicating whether W3C distributed tracing standard is enabled. /// public bool EnableW3CDistributedTracing { get; set; } } diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/RequestTrackingTelemetryModule.cs b/src/Microsoft.ApplicationInsights.AspNetCore/RequestTrackingTelemetryModule.cs index 1032d0c..5cb5611 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/RequestTrackingTelemetryModule.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/RequestTrackingTelemetryModule.cs @@ -31,7 +31,7 @@ namespace Microsoft.ApplicationInsights.AspNetCore /// /// Initializes a new instance of the class. /// - public RequestTrackingTelemetryModule() + public RequestTrackingTelemetryModule() : this(null) { this.CollectionOptions = new RequestCollectionOptions();