class Prometheus::Client::Push
Push
implements a simple way to transmit a given registry to a given Pushgateway.
Constants
- DEFAULT_GATEWAY
- INSTANCE_PATH
- PATH
- SUPPORTED_SCHEMES
Attributes
gateway[R]
instance[R]
job[R]
path[R]
Public Class Methods
new(job:, instance: nil, gateway: DEFAULT_GATEWAY, **kwargs)
click to toggle source
# File lib/prometheus/client/push.rb, line 24 def initialize(job:, instance: nil, gateway: DEFAULT_GATEWAY, **kwargs) raise ArgumentError, "job cannot be nil" if job.nil? raise ArgumentError, "job cannot be empty" if job.empty? @mutex = Mutex.new @job = job @instance = instance @gateway = gateway || DEFAULT_GATEWAY @path = build_path(job, instance) @uri = parse("#{@gateway}#{@path}") @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = (@uri.scheme == 'https') @http.open_timeout = kwargs[:open_timeout] if kwargs[:open_timeout] @http.read_timeout = kwargs[:read_timeout] if kwargs[:read_timeout] end
Public Instance Methods
add(registry)
click to toggle source
# File lib/prometheus/client/push.rb, line 41 def add(registry) synchronize do request(Net::HTTP::Post, registry) end end
delete()
click to toggle source
# File lib/prometheus/client/push.rb, line 53 def delete synchronize do request(Net::HTTP::Delete) end end
replace(registry)
click to toggle source
# File lib/prometheus/client/push.rb, line 47 def replace(registry) synchronize do request(Net::HTTP::Put, registry) end end
Private Instance Methods
build_path(job, instance)
click to toggle source
# File lib/prometheus/client/push.rb, line 73 def build_path(job, instance) if instance && !instance.empty? format(INSTANCE_PATH, ERB::Util::url_encode(job), ERB::Util::url_encode(instance)) else format(PATH, ERB::Util::url_encode(job)) end end
parse(url)
click to toggle source
# File lib/prometheus/client/push.rb, line 61 def parse(url) uri = URI.parse(url) unless SUPPORTED_SCHEMES.include?(uri.scheme) raise ArgumentError, 'only HTTP gateway URLs are supported currently.' end uri rescue URI::InvalidURIError => e raise ArgumentError, "#{url} is not a valid URL: #{e}" end
request(req_class, registry = nil)
click to toggle source
# File lib/prometheus/client/push.rb, line 81 def request(req_class, registry = nil) req = req_class.new(@uri) req.content_type = Formats::Text::CONTENT_TYPE req.basic_auth(@uri.user, @uri.password) if @uri.user req.body = Formats::Text.marshal(registry) if registry @http.request(req) end
synchronize() { || ... }
click to toggle source
# File lib/prometheus/client/push.rb, line 90 def synchronize @mutex.synchronize { yield } end