public void CollectAndSerialize() { using (var stream = Stream.Null) { ScrapeHandler.ProcessAsync(_registry, stream).GetAwaiter().GetResult(); } }
public async Task Get() { Response.StatusCode = 200; using (var outputStream = Response.Body) { await ScrapeHandler.ProcessAsync(_registry, outputStream); } }
/// <summary> /// Add PrometheusServer request execution pipeline. /// </summary> public static IApplicationBuilder UsePrometheusServer(this IApplicationBuilder app, Action <PrometheusOptions> setupOptions) { var options = new PrometheusOptions { CollectorRegistryInstance = (ICollectorRegistry)app.ApplicationServices.GetService(typeof(ICollectorRegistry)) ?? Metrics.DefaultCollectorRegistry }; setupOptions?.Invoke(options); if (app == null) { throw new ArgumentNullException(nameof(app)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (!options.MapPath.StartsWith("/")) { throw new ArgumentException($"MapPath '{options.MapPath}' should start with '/'"); } if (options.UseDefaultCollectors) { options.CollectorRegistryInstance.UseDefaultCollectors(options.MetricPrefixName); } var contentType = "text/plain; version=0.0.4"; if (options.ResponseEncoding != null) { contentType += $"; charset={options.ResponseEncoding.BodyName}"; } void AddMetricsHandler(IApplicationBuilder coreapp) { coreapp.Run(async context => { var response = context.Response; response.ContentType = contentType; using var outputStream = response.Body; await ScrapeHandler.ProcessAsync(options.CollectorRegistryInstance, outputStream); }); } if (options.Port == null) { return(app.Map(options.MapPath, AddMetricsHandler)); } bool PortMatches(HttpContext context) => context.Connection.LocalPort == options.Port; return(app.Map(options.MapPath, cfg => cfg.MapWhen(PortMatches, AddMetricsHandler))); }
public void Configure(IApplicationBuilder app) { app.Map(_mapPath, coreapp => { coreapp.Run(async context => { var response = context.Response; response.ContentType = _contentType; using (var outputStream = response.Body) { await ScrapeHandler.ProcessAsync(_registry, outputStream); } }); }); }
private void StartListen() { var cancel = _cancellation.Token; while (!_cancellation.IsCancellationRequested) { try { var getContext = _httpListener.GetContextAsync(); getContext.Wait(cancel); if (cancel.IsCancellationRequested) { return; } var context = getContext.Result; var request = context.Request; var response = context.Response; var rawUrl = request.RawUrl.EndsWith("/") ? request.RawUrl : request.RawUrl + "/"; if (rawUrl == _mapPath) { response.StatusCode = 200; response.ContentType = _contentType; using (var outputStream = response.OutputStream) { ScrapeHandler.ProcessAsync(_registry, outputStream).GetAwaiter().GetResult(); } } else { response.StatusCode = 404; using (var outputStream = response.OutputStream) { } } } catch (Exception ex) { Trace.WriteLine($"Error in MetricServer: {ex}"); } } }
public void Collect() { ScrapeHandler.ProcessAsync(_registry, _stream).GetAwaiter().GetResult(); }
public void Collecting() { using var stream = Stream.Null; ScrapeHandler.ProcessAsync(OurCollectorRegistry, stream).GetAwaiter().GetResult(); }