public void DoubleDisposeOfListener() { var listener = new DiagnosticListener("TestingDoubleDisposeOfListener"); int completionCount = 0; IDisposable subscription = listener.Subscribe(MakeObserver <KeyValuePair <string, object> >(_ => { }, () => completionCount++)); listener.Dispose(); listener.Dispose(); subscription.Dispose(); subscription.Dispose(); Assert.Equal(1, completionCount); }
public void GetHeaders_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetHeaders(100, context.Request.Headers); Assert.NotNull(result); Assert.True(result.ContainsKey("header1")); Assert.True(result.ContainsKey("header2")); Assert.True(result.ContainsKey("status")); var header1Val = result["header1"] as string; Assert.Equal("header1Value", header1Val); var header2Val = result["header2"] as string; Assert.Equal("header2Value", header2Val); var statusVal = result["status"] as string; Assert.Equal("100", statusVal); listener.Dispose(); }
public void GetTraces_ReturnsTraces() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); for (int i = 0; i < 200; i++) { long start = Stopwatch.GetTimestamp(); var context = CreateRequest(); KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST, new { httpContext = context, timestamp = start }); obs.OnNext(begin); KeyValuePair <string, object> end = new KeyValuePair <string, object>(TraceObserver.END_REQUEST, new { httpContext = context, timestamp = start + 100000 }); obs.OnNext(end); } Assert.Equal(0, obs._pending.Count); Assert.Equal(option.Capacity, obs._queue.Count); List <Trace> traces = obs.GetTraces(); Assert.Equal(option.Capacity, traces.Count); Assert.Equal(option.Capacity, obs._queue.Count); listener.Dispose(); }
public void AddTraceActuator_AddsCorrectServices() { ServiceCollection services = new ServiceCollection(); var appSettings = new Dictionary <string, string>() { ["management:endpoints:enabled"] = "false", ["management:endpoints:path"] = "/cloudfoundryapplication", ["management:endpoints:trace:enabled"] = "false", }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appSettings); var config = configurationBuilder.Build(); var listener = new DiagnosticListener("Test"); services.AddSingleton(listener); services.AddTraceActuator(config); var serviceProvider = services.BuildServiceProvider(); var options = serviceProvider.GetService <ITraceOptions>(); Assert.NotNull(options); var repo = serviceProvider.GetService <ITraceRepository>(); Assert.NotNull(repo); var ep = serviceProvider.GetService <TraceEndpoint>(); Assert.NotNull(ep); listener.Dispose(); }
public void Completed() { var result = new List <KeyValuePair <string, object> >(); var observer = new ObserverToList <TelemData>(result); var listener = new DiagnosticListener("TestingCompleted"); var subscription = listener.Subscribe(observer); listener.Write("IntPayload", 5); Assert.Equal(1, result.Count); Assert.Equal("IntPayload", result[0].Key); Assert.Equal(5, result[0].Value); Assert.False(observer.Completed); // The listener dies listener.Dispose(); Assert.True(observer.Completed); // confirm that we can unsubscribe without crashing subscription.Dispose(); // If we resubscribe after dispose, but it does not do anything. subscription = listener.Subscribe(observer); listener.Write("IntPayload", 5); Assert.Equal(1, result.Count); }
public void MakeTrace_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); Trace result = obs.MakeTrace(context, 10000000, 20000000); Assert.NotNull(result); Assert.NotNull(result.Info); Assert.NotEqual(0, result.TimeStamp); Assert.True(result.Info.ContainsKey("method")); Assert.True(result.Info.ContainsKey("path")); Assert.True(result.Info.ContainsKey("headers")); Assert.True(result.Info.ContainsKey("timeTaken")); Assert.Equal("GET", result.Info["method"]); Assert.Equal("/myPath", result.Info["path"]); var headers = result.Info["headers"] as Dictionary <string, object>; Assert.NotNull(headers); Assert.True(headers.ContainsKey("request")); Assert.True(headers.ContainsKey("response")); var timeTaken = result.Info["timeTaken"] as string; Assert.NotNull(timeTaken); var expected = ((20000000 - 10000000) / obs.ticksPerMilli).ToString(); Assert.Equal(expected, timeTaken); listener.Dispose(); }
public void GetRequestUri_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetRequestUri(context.Request); Assert.Equal("http://localhost:1111/myPath", result); listener.Dispose(); }
public void GetRemoteAddress_NoConnection_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetRemoteAddress(context); Assert.Null(result); listener.Dispose(); }
public void GetUserPrincipal_NotAuthenticated_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetUserPrincipal(context); Assert.Null(result); listener.Dispose(); }
public void GetProperties_NoProperties_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); long timeStamp = -1; obs.GetProperties(new { foo = "bar" }, out HttpContext context, out timeStamp); Assert.Equal(0, timeStamp); Assert.Null(context); listener.Dispose(); }
public void GetTimeTaken_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetTimeTaken(10000000); var expected = (10000000 / obs.ticksPerMilli).ToString(); Assert.Equal(expected, result); listener.Dispose(); }
public void OnNext_IgnoresUnRecognizedEvents() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); KeyValuePair <string, object> ignore = new KeyValuePair <string, object>("foobar", null); obs.OnNext(ignore); Assert.Equal(0, obs._pending.Count); Assert.Equal(0, obs._queue.Count); listener.Dispose(); }
public void GetUserPrincipal_Authenticated_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); context.User = new ClaimsPrincipal(new MyIdentity()); var result = obs.GetUserPrincipal(context); Assert.Equal("MyTestName", result); listener.Dispose(); }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _cancellationTokenSource.Dispose(); _pollingTask.Dispose(); _diagnostics.Dispose(); } _disposed = true; } }
public void GetProperties_WithProperties_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); long timeStamp = -1; long expectedTime = Stopwatch.GetTimestamp(); var expectedContext = CreateRequest(); obs.GetProperties(new { httpContext = expectedContext, timestamp = expectedTime }, out HttpContext context, out timeStamp); Assert.Equal(expectedTime, timeStamp); Assert.True(object.ReferenceEquals(expectedContext, context)); listener.Dispose(); }
public void OnNext_RemovesPending_AddsToQueue() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); long start = Stopwatch.GetTimestamp(); var context = CreateRequest(); KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST, new { httpContext = context, timestamp = start }); obs.OnNext(begin); KeyValuePair <string, object> end = new KeyValuePair <string, object>(TraceObserver.END_REQUEST, new { httpContext = context, timestamp = start + 100000 }); obs.OnNext(end); Assert.Equal(0, obs._pending.Count); Assert.Equal(1, obs._queue.Count); Trace result = null; Assert.True(obs._queue.TryPeek(out result)); Assert.NotNull(result.Info); Assert.NotEqual(0, result.TimeStamp); Assert.True(result.Info.ContainsKey("method")); Assert.True(result.Info.ContainsKey("path")); Assert.True(result.Info.ContainsKey("headers")); Assert.True(result.Info.ContainsKey("timeTaken")); Assert.Equal("GET", result.Info["method"]); Assert.Equal("/myPath", result.Info["path"]); var headers = result.Info["headers"] as Dictionary <string, object>; Assert.NotNull(headers); Assert.True(headers.ContainsKey("request")); Assert.True(headers.ContainsKey("response")); var timeTaken = result.Info["timeTaken"] as string; Assert.NotNull(timeTaken); var expected = ((100000) / obs.ticksPerMilli).ToString(); Assert.Equal(expected, timeTaken); listener.Dispose(); }
public void Constructor_ThrowsOnNulls() { // Arrange DiagnosticListener listener = null; DiagnosticListener listener2 = new DiagnosticListener("test"); ITraceOptions options = null; // Act and Assert var ex = Assert.Throws <ArgumentNullException>(() => new TraceObserver(listener, options)); Assert.Contains(nameof(listener), ex.Message); var ex2 = Assert.Throws <ArgumentNullException>(() => new TraceObserver(listener2, options)); Assert.Contains(nameof(options), ex2.Message); listener2.Dispose(); }
public async void HandleTraceRequestAsync_ReturnsExpected() { var opts = new TraceOptions(); DiagnosticListener listener = new DiagnosticListener("test"); TraceObserver obs = new TraceObserver(listener, opts); var ep = new TestTraceEndpoint(opts, obs); var middle = new TraceEndpointMiddleware(null, ep); var context = CreateRequest("GET", "/trace"); await middle.HandleTraceRequestAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); StreamReader rdr = new StreamReader(context.Response.Body); string json = await rdr.ReadToEndAsync(); Assert.Equal("[]", json); listener.Dispose(); }
public void AddTraceActuator_AddsCorrectServices() { ServiceCollection services = new ServiceCollection(); var appsettings = @" { 'management': { 'endpoints': { 'enabled': false, 'sensitive': false, 'path': '/cloudfoundryapplication', 'trace' : { 'enabled': true, 'sensitive': false } } } }"; var path = TestHelpers.CreateTempFile(appsettings); string directory = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); var config = configurationBuilder.Build(); var listener = new DiagnosticListener("Test"); services.AddSingleton(listener); services.AddTraceActuator(config); var serviceProvider = services.BuildServiceProvider(); var options = serviceProvider.GetService <ITraceOptions>(); Assert.NotNull(options); var repo = serviceProvider.GetService <ITraceRepository>(); Assert.NotNull(repo); var ep = serviceProvider.GetService <TraceEndpoint>(); Assert.NotNull(ep); listener.Dispose(); }
public void IsTraceRequest_ReturnsExpected() { var opts = new TraceOptions(); DiagnosticListener listener = new DiagnosticListener("test"); TraceObserver obs = new TraceObserver(listener, opts); var ep = new TraceEndpoint(opts, obs); var middle = new TraceEndpointMiddleware(null, ep); var context = CreateRequest("GET", "/trace"); Assert.True(middle.IsTraceRequest(context)); var context2 = CreateRequest("PUT", "/trace"); Assert.False(middle.IsTraceRequest(context2)); var context3 = CreateRequest("GET", "/badpath"); Assert.False(middle.IsTraceRequest(context3)); listener.Dispose(); }
public void Subscribe_Listener_StopActivity_AddsToQueue() { DiagnosticListener listener = new DiagnosticListener("Microsoft.AspNetCore"); var option = new TraceEndpointOptions(); TraceDiagnosticObserver obs = new TraceDiagnosticObserver(option); obs.Subscribe(listener); var context = CreateRequest(); string activityName = "Microsoft.AspNetCore.Hosting.HttpRequestIn"; Activity current = new Activity(activityName); listener.StartActivity(current, new { HttpContext = context }); Thread.Sleep(1000); listener.StopActivity(current, new { HttpContext = context }); Assert.Single(obs._queue); Assert.True(obs._queue.TryPeek(out TraceResult result)); Assert.NotNull(result.Info); Assert.NotEqual(0, result.TimeStamp); Assert.True(result.Info.ContainsKey("method")); Assert.True(result.Info.ContainsKey("path")); Assert.True(result.Info.ContainsKey("headers")); Assert.True(result.Info.ContainsKey("timeTaken")); Assert.Equal("GET", result.Info["method"]); Assert.Equal("/myPath", result.Info["path"]); var headers = result.Info["headers"] as Dictionary <string, object>; Assert.NotNull(headers); Assert.True(headers.ContainsKey("request")); Assert.True(headers.ContainsKey("response")); var timeTaken = result.Info["timeTaken"] as string; Assert.NotNull(timeTaken); Assert.StartsWith("10", timeTaken); obs.Dispose(); listener.Dispose(); }
public void GetSessionId_WithSession_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var session = new TestSession(); ISessionFeature sessFeature = new SessionFeature(); sessFeature.Session = session; context.Features.Set <ISessionFeature>(sessFeature); var result = obs.GetSessionId(context); Assert.Equal("TestSessionId", result); listener.Dispose(); }
public void GetRequestParameters_ReturnsExpected() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); HttpContext context = CreateRequest(); var result = obs.GetRequestParameters(context.Request); Assert.NotNull(result); Assert.True(result.ContainsKey("foo")); Assert.True(result.ContainsKey("bar")); var fooVal = result["foo"]; Assert.Equal(1, fooVal.Length); Assert.Equal("bar", fooVal[0]); var barVal = result["bar"]; Assert.Equal(1, barVal.Length); Assert.Equal("foo", barVal[0]); listener.Dispose(); }
public void OnNext_AddsToPending() { DiagnosticListener listener = new DiagnosticListener("test"); TraceOptions option = new TraceOptions(); TraceObserver obs = new TraceObserver(listener, option); long expectedTime = Stopwatch.GetTimestamp(); var expectedContext = CreateRequest(); KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST, new { httpContext = expectedContext, timestamp = expectedTime }); obs.OnNext(begin); Assert.Equal(1, obs._pending.Count); Assert.True(obs._pending.ContainsKey(expectedContext.TraceIdentifier)); var pending = obs._pending[expectedContext.TraceIdentifier]; Assert.Equal(expectedTime, pending.StartTime); Assert.Equal(0, obs._queue.Count); listener.Dispose(); }
public void GetTraces_ReturnsTraces() { var listener = new DiagnosticListener("test"); var option = new TraceEndpointOptions(); var obs = new TraceDiagnosticObserver(option); var current = new Activity("Microsoft.AspNetCore.Hosting.HttpRequestIn"); current.Start(); for (var i = 0; i < 200; i++) { var context = CreateRequest(); obs.ProcessEvent("Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop", new { HttpContext = context }); } Assert.Equal(option.Capacity, obs._queue.Count); var traces = obs.GetTraces(); Assert.Equal(option.Capacity, traces.Count); Assert.Equal(option.Capacity, obs._queue.Count); listener.Dispose(); }