Sha256: 928fd665b5a967add6c8137c656fc3be09fe495edf7c37cab5c8fd498eeceea6
Contents?: true
Size: 1.7 KB
Versions: 3
Compression:
Stored size: 1.7 KB
Contents
# encoding: UTF-8 require 'net/http' require 'uri' require 'prometheus/client' require 'prometheus/client/formats/text' module Prometheus # Client is a ruby implementation for a Prometheus compatible client. module Client # Push implements a simple way to transmit a given registry to a given # Pushgateway. class Push DEFAULT_GATEWAY = 'http://localhost:9091' PATH = '/metrics/jobs/%s' INSTANCE_PATH = '/metrics/jobs/%s/instances/%s' HEADER = { 'Content-Type' => Formats::Text::CONTENT_TYPE } attr_reader :job, :instance, :gateway, :path def initialize(job, instance = nil, gateway = nil) @job = job @instance = instance @gateway = gateway || DEFAULT_GATEWAY @uri = parse(@gateway) @path = build_path(job, instance) @http = Net::HTTP.new(@uri.host, @uri.port) end def add(registry) request('POST', registry) end def replace(registry) request('PUT', registry) end private def parse(url) uri = URI.parse(url) if uri.scheme == 'http' uri else fail ArgumentError, 'only HTTP gateway URLs are supported currently.' end rescue URI::InvalidURIError => e raise ArgumentError, "#{url} is not a valid URL: #{e}" end def build_path(job, instance) if instance format(INSTANCE_PATH, URI.escape(job), URI.escape(instance)) else format(PATH, URI.escape(job)) end end def request(method, registry) data = Formats::Text.marshal(registry) @http.send_request(method, path, data, HEADER) end end end end
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
prometheus-client-0.5.0 | lib/prometheus/client/push.rb |
custom-prometheus-client-0.4.3 | lib/prometheus/client/push.rb |
prometheus-client-0.4.2 | lib/prometheus/client/push.rb |