public async void HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                var           opts          = new HeapDumpEndpointOptions();
                var           mopts         = TestHelpers.GetManagementOptions(opts);
                LoggerFactory loggerFactory = new LoggerFactory();
                loggerFactory.AddConsole(minLevel: LogLevel.Debug);
                var logger1 = loggerFactory.CreateLogger <HeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();

                HeapDumper obs     = new HeapDumper(opts, logger: logger1);
                var        ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var        middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var        context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                byte[] buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
        }
Example #2
0
        public async void HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                var opts  = new HeapDumpEndpointOptions();
                var mopts = TestHelper.GetManagementOptions(opts);

                IServiceCollection serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
                var loggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var logger1 = loggerFactory.CreateLogger <HeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();

                var obs     = new HeapDumper(opts, logger: logger1);
                var ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
        }
Example #3
0
        public void Invoke_CreatesDump()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                var loggerFactory = TestHelpers.GetLoggerFactory();
                var logger1       = loggerFactory.CreateLogger <WindowsHeapDumper>();
                var logger2       = loggerFactory.CreateLogger <HeapDumpEndpoint>();

                var dumper = new WindowsHeapDumper(new HeapDumpEndpointOptions(), logger: logger1);
                var ep     = new HeapDumpEndpoint(new HeapDumpEndpointOptions(), dumper, logger2);

                var result = ep.Invoke();
                Assert.NotNull(result);
                Assert.True(File.Exists(result));
                File.Delete(result);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                if (typeof(object).Assembly.GetType("System.Index") != null)
                {
                    var loggerFactory = TestHelpers.GetLoggerFactory();
                    var logger1       = loggerFactory.CreateLogger <LinuxHeapDumper>();
                    var logger2       = loggerFactory.CreateLogger <HeapDumpEndpoint>();

                    var dumper = new LinuxHeapDumper(new HeapDumpEndpointOptions(), logger: logger1);
                    var ep     = new HeapDumpEndpoint(new HeapDumpEndpointOptions(), dumper, logger2);

                    var result = ep.Invoke();
                    Assert.NotNull(result);
                    Assert.True(File.Exists(result));
                    File.Delete(result);
                }
            }
        }
Example #4
0
        public void HeapDumpEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var         opts  = new HeapDumpEndpointOptions();
            var         mopts = TestHelper.GetManagementOptions(opts);
            IHeapDumper obs;

            if (Platform.IsWindows)
            {
                obs = new WindowsHeapDumper(opts);
            }
            else if (Platform.IsLinux)
            {
                obs = new LinuxHeapDumper(opts);
            }
            else
            {
                return;
            }

            var ep     = new HeapDumpEndpoint(opts, obs);
            var middle = new HeapDumpEndpointMiddleware(null, ep, mopts);

            Assert.True(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("PUT", "/cloudfoundryapplication/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/badpath"));
        }
Example #5
0
        public static void UseHeapDumpActuator(IConfiguration configuration, IHeapDumper heapDumper = null, ILoggerFactory loggerFactory = null)
        {
            var options = new HeapDumpOptions(configuration);

            heapDumper = heapDumper ?? new HeapDumper(options);
            var ep      = new HeapDumpEndpoint(options, heapDumper, CreateLogger <HeapDumpEndpoint>(loggerFactory));
            var handler = new HeapDumpHandler(ep, SecurityService, CreateLogger <HeapDumpHandler>(loggerFactory));

            ConfiguredHandlers.Add(handler);
        }
Example #6
0
        public void HeapDumpEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var        opts   = new HeapDumpOptions();
            HeapDumper obs    = new HeapDumper(opts);
            var        ep     = new HeapDumpEndpoint(opts, obs);
            var        middle = new HeapDumpEndpointMiddleware(null, ep);

            Assert.True(middle.RequestVerbAndPathMatch("GET", "/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("PUT", "/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("GET", "/badpath"));
        }
        public void HeapDumpEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var        opts   = new HeapDumpEndpointOptions();
            var        mopts  = TestHelpers.GetManagementOptions(opts);
            HeapDumper obs    = new HeapDumper(opts);
            var        ep     = new HeapDumpEndpoint(opts, obs);
            var        middle = new HeapDumpEndpointMiddleware(null, ep, mopts);

            Assert.True(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("PUT", "/cloudfoundryapplication/heapdump"));
            Assert.False(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/badpath"));
        }
Example #8
0
        public void Invoke_CreatesDump()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                var loggerFactory = TestHelpers.GetLoggerFactory();
                var logger1       = loggerFactory.CreateLogger <HeapDumper>();
                var logger2       = loggerFactory.CreateLogger <HeapDumpEndpoint>();

                var dumper = new HeapDumper(new HeapDumpEndpointOptions(), logger: logger1);
                var ep     = new HeapDumpEndpoint(new HeapDumpEndpointOptions(), dumper, logger2);

                var result = ep.Invoke();
                Assert.NotNull(result);
                Assert.True(File.Exists(result));
                File.Delete(result);
            }
        }
        /// <summary>
        /// Adds OWIN Middleware for providing Heap Dumps to OWIN pipeline
        /// </summary>
        /// <param name="builder">Your <see cref="IAppBuilder"/></param>
        /// <param name="options"><see cref="IHeapDumpOptions"/> to configure the endpoint</param>
        /// <param name="heapDumper"><see cref="HeapDumper"/> or other implementer of <see cref="IHeapDumper"/> for retrieving a heap dump</param>
        /// <param name="loggerFactory"><see cref="ILoggerFactory"/> for logging inside the middleware and its components</param>
        /// <returns>Your <see cref="IAppBuilder"/> with Heap Dump middleware attached</returns>
        public static IAppBuilder UseHeapDumpActuator(this IAppBuilder builder, IHeapDumpOptions options, IHeapDumper heapDumper, ILoggerFactory loggerFactory = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (heapDumper == null)
            {
                throw new ArgumentNullException(nameof(heapDumper));
            }

            var endpoint = new HeapDumpEndpoint(options, heapDumper, loggerFactory?.CreateLogger <HeapDumpEndpoint>());
            var logger   = loggerFactory?.CreateLogger <HeapDumpEndpointOwinMiddleware>();

            return(builder.Use <HeapDumpEndpointOwinMiddleware>(endpoint, ManagementOptions.Get(), logger));
        }
        public void IsHeapDumpRequest_ReturnsExpected()
        {
            var opts = new HeapDumpOptions();

            HeapDumper obs     = new HeapDumper(opts);
            var        ep      = new HeapDumpEndpoint(opts, obs);
            var        middle  = new HeapDumpEndpointMiddleware(null, ep);
            var        context = CreateRequest("GET", "/heapdump");

            Assert.True(middle.IsHeapDumpRequest(context));
            var context2 = CreateRequest("PUT", "/heapdump");

            Assert.False(middle.IsHeapDumpRequest(context2));
            var context3 = CreateRequest("GET", "/badpath");

            Assert.False(middle.IsHeapDumpRequest(context3));
        }
        public void Invoke_CreatesDump()
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                LoggerFactory loggerFactory = new LoggerFactory();
                loggerFactory.AddConsole(minLevel: LogLevel.Debug);
                var logger1 = loggerFactory.CreateLogger <HeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();

                var dumper = new HeapDumper(new HeapDumpOptions(), logger: logger1);
                var ep     = new HeapDumpEndpoint(new HeapDumpOptions(), dumper, logger2);

                var result = ep.Invoke();
                Assert.NotNull(result);
                Assert.True(File.Exists(result));
                File.Delete(result);
            }
        }
        public async Task HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var opts  = new HeapDumpEndpointOptions();
                var mopts = new ActuatorManagementOptions();
                mopts.EndpointOptions.Add(opts);

                IServiceCollection serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
                var loggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var logger1 = loggerFactory.CreateLogger <WindowsHeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();
                var logger4 = loggerFactory.CreateLogger <HeapDumper>();

                // WindowsHeapDumper should be used with .NET Core 3.1 on Windows. HeapDumper should be used with Linux and .NET 5 on Windows
                var obs = (Platform.IsWindows && RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.InvariantCultureIgnoreCase))
                            ? new WindowsHeapDumper(opts, logger: logger1)
                            : !Platform.IsOSX
                                ? (IHeapDumper) new HeapDumper(opts, logger: logger4)
                                : throw new InvalidOperationException("Unsupported Platfornm");

                var ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
            else
            {
                return;
            }
        }
        public async void HandleHeapDumpRequestAsync_ReturnsExpected()
        {
            if (EndpointServiceCollectionExtensions.IsHeapDumpSupported())
            {
                var opts  = new HeapDumpEndpointOptions();
                var mopts = new ActuatorManagementOptions();
                mopts.EndpointOptions.Add(opts);

                IServiceCollection serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
                var loggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();

                var logger1 = loggerFactory.CreateLogger <WindowsHeapDumper>();
                var logger2 = loggerFactory.CreateLogger <HeapDumpEndpoint>();
                var logger3 = loggerFactory.CreateLogger <HeapDumpEndpointMiddleware>();
                var logger4 = loggerFactory.CreateLogger <LinuxHeapDumper>();

                var obs = Platform.IsWindows ? (IHeapDumper) new WindowsHeapDumper(opts, logger: logger1)
                                : Platform.IsLinux ? (IHeapDumper) new LinuxHeapDumper(opts, logger: logger4)
                                : throw new InvalidOperationException("Unsupported Platfornm");

                var ep      = new HeapDumpEndpoint(opts, obs, logger2);
                var middle  = new HeapDumpEndpointMiddleware(null, ep, mopts, logger3);
                var context = CreateRequest("GET", "/heapdump");
                await middle.HandleHeapDumpRequestAsync(context);

                context.Response.Body.Seek(0, SeekOrigin.Begin);
                var buffer = new byte[1024];
                await context.Response.Body.ReadAsync(buffer, 0, 1024);

                Assert.NotEqual(0, buffer[0]);
            }
            else
            {
                return;
            }
        }
Example #14
0
 public HeapDumpEndpointOwinMiddleware(OwinMiddleware next, HeapDumpEndpoint endpoint, ILogger <HeapDumpEndpointOwinMiddleware> logger = null)
     : base(next, endpoint, logger: logger)
 {
     _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
 }
Example #15
0
 public HeapDumpEndpointOwinMiddleware(OwinMiddleware next, HeapDumpEndpoint endpoint, IEnumerable <IManagementOptions> mgmtOptions, ILogger <HeapDumpEndpointOwinMiddleware> logger = null)
     : base(next, endpoint, mgmtOptions, logger: logger)
 {
     _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
 }