nexogen-international / Nexogen.Libraries.Metrics

Library for collecting application metrics in .NET and exporting them to Prometheus
MIT License
61 stars 9 forks source link

Nexogen.Libraries.Metrics.Prometheus.Standalone package missing #43

Closed bastianeicher closed 4 years ago

bastianeicher commented 4 years ago

The readme says to install Nexogen.Libraries.Metrics.Prometheus.Standalone for a standalone server but there is no release of this package for 3.1.0 (the last is 3.1.0-rc.1).

Commit 5c4e3cc seems to have removed the standalone server code. Does this mean that this feature is no longer supported?

Perhaps it could be reimplemented using .NET Core Hosted Services and HttpListener, for example like this:

public class PrometheusServer : IHostedService
{
    private readonly HttpListener _listener;
    private readonly IExposable _metrics;

    public PrometheusServer(IOptions<PrometheusServerOptions> options, IExposable metrics)
    {
        _listener = new HttpListener {Prefixes = {$"http://*:{options.Value.Port}/"}};
        _metrics = metrics;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _listener.Start();
        BeginContext();
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        _listener.Abort();
        return Task.CompletedTask;
    }

    private void BeginContext()
    {
        _listener.BeginGetContext(ListenerCallback, _listener);
    }

    private async void ListenerCallback(IAsyncResult result)
    {
        var context = _listener.EndGetContext(result);
        context.Response.StatusCode = 200;
        context.Response.Headers.Add("Content-Type", "text/plain");
        await _metrics.Expose(context.Response.OutputStream, ExposeOptions.Default);
        context.Response.Close();
    }
}

I could create a Pull Request if you're interested.

ahoka commented 4 years ago

Hello @bastianeicher

We are certainly interested. It was removed due to the lack of time, but this is something we would like to support again in the future. Please do send a pull request!