public async Task ActivityIsCreatedForRequest() { Activity activity = null; (string Key, object Value, DiagnosticListener)startEvent = default; using var testListener = new TestDiagnosticListener("Azure.Core"); MockTransport mockTransport = CreateMockTransport(_ => { activity = Activity.Current; startEvent = testListener.Events.Dequeue(); MockResponse mockResponse = new MockResponse(201); mockResponse.AddHeader(new HttpHeader("x-ms-request-id", "server request id")); return(mockResponse); }); string clientRequestId = null; Task <Response> requestTask = SendRequestAsync(mockTransport, request => { request.Method = RequestMethod.Get; request.Uri.Reset(new Uri("http://example.com")); request.Headers.Add("User-Agent", "agent"); clientRequestId = request.ClientRequestId; }, s_enabledPolicy); await requestTask; (string Key, object Value, DiagnosticListener)stopEvent = testListener.Events.Dequeue(); Assert.AreEqual("Azure.Core.Http.Request.Start", startEvent.Key); Assert.AreEqual("Azure.Core.Http.Request.Stop", stopEvent.Key); Assert.AreEqual("Azure.Core.Http.Request", activity.OperationName); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.status_code", "201")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.url", "http://example.com/")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.method", "GET")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.user_agent", "agent")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("requestId", clientRequestId)); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("serviceRequestId", "server request id")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("kind", "client")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("az.namespace", "Microsoft.Azure.Core.Cool.Tests")); CollectionAssert.DoesNotContain(activity.Tags.Select(t => t.Key), "otel.status_code"); }
public void AddLinkCallsPassesLinksAsPartOfStartPayload() { using var testListener = new TestDiagnosticListener("Azure.Clients"); DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false); DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName"); scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", "foo=bar"); scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", null); scope.Start(); (string Key, object Value, DiagnosticListener) startEvent = testListener.Events.Dequeue(); Activity activity = Activity.Current; scope.Dispose(); (string Key, object Value, DiagnosticListener) stopEvent = testListener.Events.Dequeue(); var isEnabledCall = testListener.IsEnabledCalls.Dequeue(); Assert.NotNull(activity); Assert.Null(Activity.Current); Assert.AreEqual("ActivityName.Start", startEvent.Key); Assert.AreEqual("ActivityName.Stop", stopEvent.Key); Assert.AreEqual("ActivityName", isEnabledCall.Name); var activities = (IEnumerable<Activity>)startEvent.Value.GetType().GetTypeInfo().GetDeclaredProperty("Links").GetValue(startEvent.Value); Activity[] activitiesArray = activities.ToArray(); Assert.AreEqual(activitiesArray.Length, 2); Activity linkedActivity1 = activitiesArray[0]; Activity linkedActivity2 = activitiesArray[1]; Assert.AreEqual(ActivityIdFormat.W3C, linkedActivity1.IdFormat); Assert.AreEqual("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", linkedActivity1.ParentId); Assert.AreEqual("foo=bar", linkedActivity1.TraceStateString); Assert.AreEqual(ActivityIdFormat.W3C, linkedActivity2.IdFormat); Assert.AreEqual("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", linkedActivity2.ParentId); Assert.Null(linkedActivity2.TraceStateString); Assert.AreEqual(0, testListener.Events.Count); }
public void ActivityDurationIsNotZeroWhenStoping() { TimeSpan?duration = null; using var testListener = new TestDiagnosticListener("Azure.Clients"); testListener.EventCallback = _ => { duration = Activity.Current?.Duration; }; DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true); DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName"); scope.Start(); Thread.Sleep(50); scope.Dispose(); Assert.True(duration > TimeSpan.Zero); }
public async Task ActivityMarkedAsErrorForErrorResponse() { Activity activity = null; using var testListener = new TestDiagnosticListener("Azure.Core"); MockTransport mockTransport = CreateMockTransport(_ => { activity = Activity.Current; MockResponse mockResponse = new MockResponse(500); return(mockResponse); }); Task <Response> requestTask = SendGetRequest(mockTransport, s_enabledPolicy); await requestTask; CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("otel.status_code", "ERROR")); }
public void ActivityStartTimeCanBeSet() { DateTime?actualStartTimeUtc = null; using var testListener = new TestDiagnosticListener("Azure.Clients"); testListener.EventCallback = _ => { actualStartTimeUtc = Activity.Current?.StartTimeUtc; }; DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false); DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName"); DateTime expectedStartTimeUtc = DateTime.UtcNow - TimeSpan.FromSeconds(10); scope.SetStartTime(expectedStartTimeUtc); scope.Start(); scope.Dispose(); Assert.AreEqual(expectedStartTimeUtc, actualStartTimeUtc); }
public void NestedActivitiesNoSuppressionDifferentSourcesServerClient() { using var testListener = new TestDiagnosticListener("Azure.Clients"); DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false); ; DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", DiagnosticScope.ActivityKind.Server); scope.Start(); using var activityListener2 = new TestDiagnosticListener("Azure.Clients2"); DiagnosticScopeFactory clientDiagnostics2 = new DiagnosticScopeFactory("Azure.Clients2", "Microsoft.Azure.Core.Cool.Tests", true, false); DiagnosticScope nestedScope = clientDiagnostics2.CreateScope("ClientName.NestedActivityName"); nestedScope.Start(); Assert.IsTrue(nestedScope.IsEnabled); Assert.AreEqual("ClientName.NestedActivityName", Activity.Current.OperationName); nestedScope.Dispose(); Assert.AreEqual("ClientName.ActivityName", Activity.Current.OperationName); scope.Dispose(); }
public async Task PassesMessageIntoIsEnabledStartAndStopEvents() { using var testListener = new TestDiagnosticListener("Azure.Core"); var transport = new MockTransport(new MockResponse(200)); await SendGetRequest(transport, s_enabledPolicy); (string Key, object Value, DiagnosticListener)startEvent = testListener.Events.Dequeue(); (string Key, object Value, DiagnosticListener)stopEvent = testListener.Events.Dequeue(); (string, object, object)isEnabledCall = testListener.IsEnabledCalls.Dequeue(); Assert.AreEqual("Azure.Core.Http.Request.Start", startEvent.Key); Assert.IsInstanceOf <HttpMessage>(startEvent.Value); Assert.AreEqual("Azure.Core.Http.Request.Stop", stopEvent.Key); Assert.IsInstanceOf <HttpMessage>(stopEvent.Value); Assert.AreEqual("Azure.Core.Http.Request", isEnabledCall.Item1); Assert.IsInstanceOf <HttpMessage>(isEnabledCall.Item2); }
public async Task PassesMessageIntoIsEnabledStartAndStopEvents() { using var testListener = new TestDiagnosticListener("Azure.Pipeline"); var transport = new MockTransport(new MockResponse(200)); await SendGetRequest(transport, RequestActivityPolicy.Shared); KeyValuePair <string, object> startEvent = testListener.Events.Dequeue(); KeyValuePair <string, object> stopEvent = testListener.Events.Dequeue(); var isEnabledCall = testListener.IsEnabledCalls.Dequeue(); Assert.AreEqual("Azure.Core.Http.Request.Start", startEvent.Key); Assert.IsInstanceOf <HttpPipelineMessage>(startEvent.Value); Assert.AreEqual("Azure.Core.Http.Request.Stop", stopEvent.Key); Assert.IsInstanceOf <HttpPipelineMessage>(stopEvent.Value); Assert.AreEqual("Azure.Core.Http.Request", isEnabledCall.Item1); Assert.IsInstanceOf <HttpPipelineMessage>(isEnabledCall.Item2); }
public void AddLinkCallsPassesLinksAsPartOfStartPayload() { using var testListener = new TestDiagnosticListener("Azure.Clients"); DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true); DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName"); scope.AddLink("id"); scope.AddLink("id2"); scope.Start(); (string Key, object Value, DiagnosticListener)startEvent = testListener.Events.Dequeue(); Activity activity = Activity.Current; scope.Dispose(); (string Key, object Value, DiagnosticListener)stopEvent = testListener.Events.Dequeue(); Assert.Null(Activity.Current); Assert.AreEqual("ActivityName.Start", startEvent.Key); Assert.AreEqual("ActivityName.Stop", stopEvent.Key); var activities = (IEnumerable <Activity>)startEvent.Value.GetType().GetTypeInfo().GetDeclaredProperty("Links").GetValue(startEvent.Value); Activity[] activitiesArray = activities.ToArray(); Assert.AreEqual(activitiesArray.Length, 2); Activity linkedActivity1 = activitiesArray[0]; Activity linkedActivity2 = activitiesArray[1]; Assert.AreEqual(ActivityIdFormat.W3C, linkedActivity1.IdFormat); Assert.AreEqual("id", linkedActivity1.ParentId); Assert.AreEqual(ActivityIdFormat.W3C, linkedActivity2.IdFormat); Assert.AreEqual("id2", linkedActivity2.ParentId); Assert.AreEqual(0, testListener.Events.Count); }
public void ResourceNameIsOptional() { using var testListener = new TestDiagnosticListener("Azure.Clients"); DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", null, true, false); DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName"); scope.Start(); (string Key, object Value, DiagnosticListener) startEvent = testListener.Events.Dequeue(); Activity activity = Activity.Current; scope.Dispose(); (string Key, object Value, DiagnosticListener) stopEvent = testListener.Events.Dequeue(); Assert.Null(Activity.Current); Assert.AreEqual("ActivityName.Start", startEvent.Key); Assert.AreEqual("ActivityName.Stop", stopEvent.Key); Assert.AreEqual(ActivityIdFormat.W3C, activity.IdFormat); }
public async Task ActivityIsCreatedForRequest() { Activity activity = null; KeyValuePair <string, object> startEvent = default; using var testListener = new TestDiagnosticListener("Azure.Pipeline"); MockTransport mockTransport = CreateMockTransport(_ => { activity = Activity.Current; startEvent = testListener.Events.Dequeue(); MockResponse mockResponse = new MockResponse(201); mockResponse.AddHeader(new HttpHeader("x-ms-request-id", "server request id")); return(mockResponse); }); using Request request = mockTransport.CreateRequest(); request.Method = RequestMethod.Get; request.UriBuilder.Uri = new Uri("http://example.com"); request.Headers.Add("User-Agent", "agent"); Task <Response> requestTask = SendRequestAsync(mockTransport, request, s_enabledPolicy); await requestTask; KeyValuePair <string, object> stopEvent = testListener.Events.Dequeue(); Assert.AreEqual("Azure.Core.Http.Request.Start", startEvent.Key); Assert.AreEqual("Azure.Core.Http.Request.Stop", stopEvent.Key); Assert.AreEqual("Azure.Core.Http.Request", activity.OperationName); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.status_code", "201")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.url", "http://example.com/")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.method", "GET")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("http.user_agent", "agent")); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("requestId", request.ClientRequestId)); CollectionAssert.Contains(activity.Tags, new KeyValuePair <string, string>("serviceRequestId", "server request id")); }
public void NestedClientActivitiesSuppressed(int kind, bool expectSuppression) { using var testListener = new TestDiagnosticListener("Azure.Clients"); DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, true); using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (DiagnosticScope.ActivityKind)kind); scope.Start(); DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (DiagnosticScope.ActivityKind)kind); nestedScope.Start(); if (expectSuppression) { Assert.IsFalse(nestedScope.IsEnabled); Assert.AreEqual("ClientName.ActivityName", Activity.Current.OperationName); } else { Assert.IsTrue(nestedScope.IsEnabled); Assert.AreEqual("ClientName.NestedActivityName", Activity.Current.OperationName); } nestedScope.Dispose(); Assert.AreEqual("ClientName.ActivityName", Activity.Current.OperationName); }