Travix-International / Hystrix.Dotnet

A combination of circuit breaker and timeout. The .net version of the open source Hystrix library built by Netflix.
https://travix-international.github.io/Hystrix.Dotnet/
MIT License
95 stars 16 forks source link

Stream metrics from console/windows application #17

Open kolbis opened 6 years ago

kolbis commented 6 years ago

On our microservices environment, most of the microservices are implemented as console applications which using Topshelf run as windows services. I could not find any "out of the box" solution for streaming commands metrics from a console or windows application. Am I missing something? Have you already implemented this?

markvincze commented 6 years ago

Hi @kolbis,

I don't think you're missing anything. Supporting the Console scenario wasn't part of what we've done so far, so we don't have any streamlined support for that.

If you'd like to get it to work right now, I think you can simply start up the ASP.NET Core stream endpoint programmatically in a Console app, with an approach like this:

using Hystrix.Dotnet;
using Hystrix.Dotnet.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace ConsoleHystrix
{
    public class SelfContainedHystrixStream
    {
        public static void Start(IHystrixCommandFactory commandFactory, HystrixOptions options)
        {
            var startup = new HystrixStreamStartup(commandFactory, options);

            var hostBuilder = new WebHostBuilder()
                .UseKestrel()
                .UseUrls($"http://localhost:8081/")
                .ConfigureServices(startup.ConfigureServices)
                .Configure(startup.Configure);

            webHost = hostBuilder.Build();
            webHost.Start();
        }
    }

    public class HystrixStreamStartup
    {
        private readonly IHystrixCommandFactory commandFactory;
        private readonly HystrixOptions options;

        public HystrixStreamStartup(IHystrixCommandFactory commandFactory, HystrixOptions options)
        {
            this.commandFactory = commandFactory;
            this.options = options;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(options);
            services.AddSingleton<IHystrixCommandFactory>(commandFactory);
            services.AddSingleton<IHystrixMetricsStreamEndpoint>(new HystrixMetricsStreamEndpoint(commandFactory, options));
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseHystrixMetricsEndpoint("hystrix.stream");
        }
    }
}

And then in your Console app call SelfContainedHystrixStream.Start() and pass in your own command factory and options.

I didn't have time yet to test this code, so you might need to adjust it to make it work (or compile :sweat_smile:), but I think it should work.