tentaclelabs / prometheus_client

Dart implementation of the Prometheus client library
https://pub.dev/packages/prometheus_client
MIT License
19 stars 4 forks source link

inc() seems to go up by 2 #34

Closed berkobob closed 1 year ago

berkobob commented 1 year ago

I'm just writing my first exporter. I'm sure I'm just doing something stupid but everytime I make a request my Counter goes up by 2!

import 'package:prometheus_client/format.dart';
import 'package:prometheus_client/prometheus_client.dart';

class NutshellExporter {
  final Counter metricRequestCounter = Counter(
      name: 'my_new_counter', help: 'The total number of times I call inc');

  NutshellExporter() {
    metricRequestCounter.register();
  }

  Future<void> start() async {
    final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
    print('Listening on port 8080');

    await for (HttpRequest request in server) {
      metricRequestCounter.inc();
      request.response.headers.add('content-type', contentType);
      final metrics = await metricRequestCounter.collect();
      write004(request.response, metrics);
      await request.response.close();
    }
  }
}

1st response:

# HELP my_new_counter The total number of times I call inc
# TYPE my_new_counter counter
my_new_counter 1.0

2nd response:

# HELP my_new_counter The total number of times I call inc
# TYPE my_new_counter counter
my_new_counter 3.0
Fox32 commented 1 year ago

I haven't looked into your code, but one idea that I have: Are you using a browser and maybe the browser is performing two calls? Favicon? Options request? But I can try to have a look later today.

berkobob commented 1 year ago

You are quite right. When I do a curl from the command line the counter increments by 1! How do I avoid this problem in production, or does prometheus sort that out?

Fox32 commented 1 year ago

It depends, you will probably not have a counter for the metrics endpoint and the metrics endpoint would be called/scraped by prometheus.

For other endpoints you could make sure to exclude favicons / OPTIONS requests. But it depends if you want to know about them or not. They can also generate load / errors.