Sha256: 7c5972e1599583952124590f705fa9f3359c09c3183e7e312939ae9dda62249a

Contents?: true

Size: 1.8 KB

Versions: 71

Compression:

Stored size: 1.8 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'.freeze
      PATH            = '/metrics/jobs/%s'.freeze
      INSTANCE_PATH   = '/metrics/jobs/%s/instances/%s'.freeze
      HEADER          = { 'Content-Type' => Formats::Text::CONTENT_TYPE }.freeze

      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

      def delete
        @http.send_request('DELETE', path)
      end

      private

      def parse(url)
        uri = URI.parse(url)

        if uri.scheme == 'http'
          uri
        else
          raise 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

71 entries across 71 versions & 1 rubygems

Version Path
prometheus-client-mmap-0.12.0 lib/prometheus/client/push.rb
prometheus-client-mmap-0.11.0 lib/prometheus/client/push.rb
prometheus-client-mmap-0.10.0 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.10 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.9 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.8 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.7 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.6 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.5 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.4 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.3 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.2 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.1 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.1.pre.rc.2 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.1.pre.rc.1 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.0.pre.rc.1 lib/prometheus/client/push.rb
prometheus-client-mmap-0.9.0 lib/prometheus/client/push.rb
prometheus-client-mmap-0.7.0.beta45.10 lib/prometheus/client/push.rb
prometheus-client-mmap-0.7.0.beta45.9 lib/prometheus/client/push.rb
prometheus-client-mmap-0.7.0.beta45.8 lib/prometheus/client/push.rb