public void VerifyOnRequestWithDuplicateRequestCreatesValidTelemetry() { Guid loggingRequestId = Guid.NewGuid(); var requestMsg = new HttpRequestMessage(HttpMethod.Get, RequestUrlWithScheme); using (var listener = new HttpCoreDiagnosticSourceListener( this.configuration, setComponentCorrelationHttpHeaders: true, correlationDomainExclusionList: new string[] { "excluded.host.com" }, injectLegacyHeaders: false, injectRequestIdInW3CMode: true, HttpInstrumentationVersion.V1)) { // first request, expected to fail listener.OnRequest(requestMsg, loggingRequestId); // second request (BEFORE HANDLING FIRST RESPONSE), expected to pass listener.OnRequest(requestMsg, loggingRequestId); Assert.IsTrue(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out _)); Assert.AreEqual(0, this.sentTelemetry.Count); // first request fails HttpResponseMessage responseMsg1 = new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = requestMsg }; listener.OnResponse(responseMsg1, loggingRequestId); Assert.IsFalse(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out _)); Assert.AreEqual(1, this.sentTelemetry.Count); Assert.AreEqual(false, ((DependencyTelemetry)this.sentTelemetry.Last()).Success); // first request fails Assert.Inconclusive(); // THE FOLLOWING ASSERTS ARE EXPECTED TO PASS, BUT CURRENTLY FAIL DUE TO A KNOWN ISSUE: #724 // Because two identical requests were sent, whichever completes first will remove the request from pending telemetry. // second request is success HttpResponseMessage responseMsg2 = new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = requestMsg }; listener.OnResponse(responseMsg2, loggingRequestId); Assert.IsFalse(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out _)); Assert.AreEqual(2, this.sentTelemetry.Count); Assert.AreEqual(true, ((DependencyTelemetry)this.sentTelemetry.Last()).Success); // second request is success } }
public void OnResponseWithParentActivity() { var parentActivity = new Activity("incoming_request"); parentActivity.AddBaggage("k1", "v1"); parentActivity.AddBaggage("k2", "v2"); parentActivity.Start(); parentActivity.TraceStateString = "state=some"; Guid loggingRequestId = Guid.NewGuid(); HttpRequestMessage requestMsg = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme); using (var listener = new HttpCoreDiagnosticSourceListener( this.configuration, setComponentCorrelationHttpHeaders: true, correlationDomainExclusionList: new string[] { "excluded.host.com" }, injectLegacyHeaders: false, injectRequestIdInW3CMode: true, HttpInstrumentationVersion.V1)) { listener.OnRequest(requestMsg, loggingRequestId); Assert.IsNotNull(Activity.Current); var requestId = requestMsg.Headers.GetValues(RequestResponseHeaders.RequestIdHeader).Single(); var traceparent = requestMsg.Headers.GetValues("traceparent").Single(); var tracestate = requestMsg.Headers.GetValues("tracestate").Single(); Assert.AreEqual($"|{Activity.Current.TraceId.ToHexString()}.{Activity.Current.SpanId.ToHexString()}.", requestId); Assert.AreEqual(Activity.Current.Id, traceparent); Assert.AreEqual("state=some", tracestate); var correlationContextHeader = requestMsg.Headers.GetValues(RequestResponseHeaders.CorrelationContextHeader).ToArray(); Assert.AreEqual(2, correlationContextHeader.Length); Assert.IsTrue(correlationContextHeader.Contains("k1=v1")); Assert.IsTrue(correlationContextHeader.Contains("k2=v2")); Assert.IsTrue(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out var dependency)); DependencyTelemetry telemetry = dependency.Telemetry; HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = requestMsg }; listener.OnResponse(responseMsg, loggingRequestId); Assert.AreEqual(parentActivity, Activity.Current); Assert.AreEqual(requestId, telemetry.Id); Assert.AreEqual(parentActivity.RootId, telemetry.Context.Operation.Id); Assert.AreEqual($"|{parentActivity.TraceId.ToHexString()}.{parentActivity.SpanId.ToHexString()}.", telemetry.Context.Operation.ParentId); Assert.AreEqual("state=some", telemetry.Properties["tracestate"]); // Check the operation details this.operationDetailsInitializer.ValidateOperationDetailsCore(telemetry); } parentActivity.Stop(); }
public void OnResponseWithSuccessfulResponseEventWithMatchingRequestAndNoTargetInstrumentationKeyHasHeader() { Guid loggingRequestId = Guid.NewGuid(); HttpRequestMessage requestMsg = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme); using (var listener = new HttpCoreDiagnosticSourceListener( this.configuration, setComponentCorrelationHttpHeaders: true, correlationDomainExclusionList: new string[] { "excluded.host.com" }, injectLegacyHeaders: false, injectRequestIdInW3CMode: true, HttpInstrumentationVersion.V1)) { listener.OnRequest(requestMsg, loggingRequestId); Assert.IsTrue(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out var dependency)); Assert.AreEqual(0, this.sentTelemetry.Count); DependencyTelemetry telemetry = dependency.Telemetry; Assert.AreEqual(string.Empty, telemetry.ResultCode); Assert.AreEqual(true, telemetry.Success); HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = requestMsg }; listener.OnResponse(responseMsg, loggingRequestId); Assert.IsFalse(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out dependency)); Assert.AreEqual(1, this.sentTelemetry.Count); Assert.AreSame(telemetry, this.sentTelemetry.Single()); Assert.AreEqual(RemoteDependencyConstants.HTTP, telemetry.Type); Assert.AreEqual(RequestUrl, telemetry.Target); Assert.AreEqual(HttpOkResultCode, telemetry.ResultCode); Assert.AreEqual(true, telemetry.Success); string expectedVersion = SdkVersionHelper.GetExpectedSdkVersion(typeof(DependencyTrackingTelemetryModule), prefix: "rdddsc:"); Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion); // Check the operation details this.operationDetailsInitializer.ValidateOperationDetailsCore(telemetry); } }
public void OnResponseWithFailedResponseEventWithMatchingRequestAndDifferentTargetInstrumentationKeyHashHeader() { Guid loggingRequestId = Guid.NewGuid(); HttpRequestMessage requestMsg = new HttpRequestMessage(HttpMethod.Post, RequestUrlWithScheme); using (var listener = new HttpCoreDiagnosticSourceListener( this.configuration, setComponentCorrelationHttpHeaders: true, correlationDomainExclusionList: new string[] { "excluded.host.com" }, injectLegacyHeaders: false, injectRequestIdInW3CMode: true, HttpInstrumentationVersion.V1)) { listener.OnRequest(requestMsg, loggingRequestId); Assert.IsTrue(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out var dependency)); Assert.AreEqual(0, this.sentTelemetry.Count); DependencyTelemetry telemetry = dependency.Telemetry; Assert.AreEqual(string.Empty, telemetry.ResultCode); Assert.AreEqual(true, telemetry.Success); HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = requestMsg }; string targetApplicationId = this.testApplicationId2; HttpHeadersUtilities.SetRequestContextKeyValue(responseMsg.Headers, RequestResponseHeaders.RequestContextCorrelationTargetKey, targetApplicationId); listener.OnResponse(responseMsg, loggingRequestId); Assert.IsFalse(listener.PendingDependencyTelemetry.TryGetValue(requestMsg, out dependency)); Assert.AreEqual(1, this.sentTelemetry.Count); Assert.AreSame(telemetry, this.sentTelemetry.Single()); Assert.AreEqual(RemoteDependencyConstants.AI, telemetry.Type); Assert.AreEqual(GetApplicationInsightsTarget(targetApplicationId), telemetry.Target); Assert.AreEqual(NotFoundResultCode, telemetry.ResultCode); Assert.AreEqual(false, telemetry.Success); // Check the operation details this.operationDetailsInitializer.ValidateOperationDetailsCore(telemetry); } }
public void OnResponseWithResponseEventButNoMatchingRequest() { var responseMsg = new HttpResponseMessage { RequestMessage = new HttpRequestMessage(HttpMethod.Get, RequestUrlWithScheme) }; using (var listener = new HttpCoreDiagnosticSourceListener( this.configuration, setComponentCorrelationHttpHeaders: true, correlationDomainExclusionList: new string[] { "excluded.host.com" }, injectLegacyHeaders: false, injectRequestIdInW3CMode: true, HttpInstrumentationVersion.V1)) { listener.OnResponse(responseMsg, Guid.NewGuid()); Assert.AreEqual(0, this.sentTelemetry.Count); } }