discourse / prometheus_exporter

A framework for collecting and aggregating prometheus metrics
MIT License
532 stars 154 forks source link

Feature request: no-op `PrometheusExporter::Client` Implementation for test environment #322

Open viralpraxis opened 2 months ago

viralpraxis commented 2 months ago

Hey!

I believe it would be beneficial to have a no-op implementation of PrometheusExporter::Client for use in test environments. This would allow us to avoid sending metrics during tests while maintaining the same interface.

I believe we could implement a stub client like the following:

class StubClient < Client
  def send(json); end

  # Additional stub methods can be added here if necessary
end

If this idea is deemed appropriate, I'd be happy to create a pull request with the implementation.

SamSaffron commented 2 months ago

not sure about this, I guess if it simplifies some of the test suite then fine

viralpraxis commented 2 months ago

The following client stub

# frozen_string_literal: true

require "prometheus_exporter/client"

module PrometheusExporter
  class ClientStub < PrometheusExporter::Client
    def send(_json); end
    def process_queue; end
    def stop(wait_timeout_seconds: 0); end
  end
end

seems to work fine -- in our case, in dev/test environments prometheus_exporter is loaded and PrometheusExprorter::Client.default is set to PrometheusExporter::ClientStub.new. We also don't see any significant test suite slow down. In fact, it's OK for us to have this stub in our codebase (not in the gem itself). Should I open PR with these changes? If not, feel free to close this issue. I hope if someone tries to solve the same problem this snippet will be helpful.

SamSaffron commented 2 months ago

you can send a PR I guess.

viralpraxis commented 1 month ago

Could someone take a look, please?