prometheusHandler function

Handler prometheusHandler([
  1. CollectorRegistry? registry
])

Create a shelf handler that returns the metrics in the prometheus text representation. If no registry is provided, the CollectorRegistry.defaultRegistry is used.

Implementation

shelf.Handler prometheusHandler([CollectorRegistry? registry]) {
  final reg = registry ?? CollectorRegistry.defaultRegistry;

  return (shelf.Request? request) async {
    // TODO: Instead of using a StringBuffer we could directly stream to network
    final buffer = StringBuffer();
    final metrics = await reg.collectMetricFamilySamples();
    format.write004(buffer, metrics);
    return shelf.Response.ok(
      buffer.toString(),
      headers: {'Content-Type': format.contentType},
    );
  };
}