/// <summary> /// Create HTTP endpoint where metrics will be available in various formats: /// GET / => visualization application /// GET /json => metrics serialized as JSON /// GET /text => metrics in human readable text format /// </summary> /// <param name="httpUriPrefix">prefix where to start HTTP endpoint</param> public MetricsConfig WithHttpEndpoint(string httpUriPrefix) { if (this.isDisabled) { return this; } using (this.listener) { } this.listener = new MetricsHttpListener(httpUriPrefix, this.Registry, this.healthStatus); this.listener.Start(); return this; }
public static Task <MetricsHttpListener> StartHttpListenerAsync(string httpUriPrefix, MetricsDataProvider dataProvider, Func <HealthStatus> healthStatus, CancellationToken token, int maxRetries = 1) { return(Task.Factory.StartNew(async() => { MetricsHttpListener listener = null; var remainingRetries = maxRetries; do { try { listener = new MetricsHttpListener(httpUriPrefix, dataProvider, healthStatus, token); listener.Start(); if (remainingRetries != maxRetries) { log.InfoFormat("HttpListener started successfully after {0} retries", maxRetries - remainingRetries); } remainingRetries = 0; } catch (Exception x) { using (listener) { } listener = null; remainingRetries--; if (remainingRetries > 0) { log.WarnException("Unable to start HTTP Listener. Sleeping for {0} sec and retrying {1} more times", x, maxRetries - remainingRetries, remainingRetries); await Task.Delay(1000 * (maxRetries - remainingRetries), token).ConfigureAwait(false); } else { MetricsErrorHandler.Handle(x, $"Unable to start HTTP Listener. Retried {maxRetries} times, giving up..."); } } } while (remainingRetries > 0); return listener; }, token).Unwrap()); }
public void Dispose() { using (this.listener) { } this.listener = null; }