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 = nil) click to toggle source
# File lib/prometheus/client/push.rb, line 23
def initialize(job, instance = nil, gateway = nil)
  @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')
end

Public Instance Methods

add(registry) click to toggle source
# File lib/prometheus/client/push.rb, line 35
def add(registry)
  synchronize do
    request(Net::HTTP::Post, registry)
  end
end
delete() click to toggle source
# File lib/prometheus/client/push.rb, line 47
def delete
  synchronize do
    request(Net::HTTP::Delete)
  end
end
replace(registry) click to toggle source
# File lib/prometheus/client/push.rb, line 41
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 67
def build_path(job, instance)
  if instance
    format(INSTANCE_PATH, URI.escape(job), URI.escape(instance))
  else
    format(PATH, URI.escape(job))
  end
end
parse(url) click to toggle source
# File lib/prometheus/client/push.rb, line 55
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 75
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 84
def synchronize
  @mutex.synchronize { yield }
end