Class: Prometheus::AlertBufferClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus/alert_buffer_client/client.rb

Overview

Client contains the implementation for a Prometheus-alert-buffer client.

Defined Under Namespace

Classes: RequestError

Constant Summary collapse

DEFAULT_ARGS =

Default parameters for creating default client

{
  path: '/topics/alerts',
  credentials: {},
  options: {
    open_timeout: 2,
    timeout: 5,
  },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a Prometheus Alert client:

A default client is created if options is omitted.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)

    server base URL.

  • :credentials (Hash)

    Authentication credentials.

  • :options (Hash)

    Options used to define connection.

  • :params (Hash)

    URI query unencoded key/value pairs.

  • :headers (Hash)

    Unencoded HTTP header key/value pairs.

  • :request (Hash)

    Request options.

  • :ssl (Hash)

    SSL options.

  • :proxy (Hash)

    Proxy options.



37
38
39
40
41
42
43
44
45
46
# File 'lib/prometheus/alert_buffer_client/client.rb', line 37

def initialize(options = {})
  options = DEFAULT_ARGS.merge(options)

  @client = Faraday.new(
    faraday_options(options),
  ) do |conn|
    conn.response(:json)
    conn.adapter(Faraday.default_adapter)
  end
end

Instance Method Details

#faraday_headers(options) ⇒ Object

Helper function to evalueate the low level headers option



95
96
97
98
99
100
101
102
103
104
# File 'lib/prometheus/alert_buffer_client/client.rb', line 95

def faraday_headers(options)
  return options[:headers] if options[:headers]

  headers = options[:credentials]
  return unless headers && headers[:token]

  {
    Authorization: "Bearer #{headers[:token]}",
  }
end

#faraday_options(options) ⇒ Object

Helper function to create the args for the low level client



120
121
122
123
124
125
126
127
128
# File 'lib/prometheus/alert_buffer_client/client.rb', line 120

def faraday_options(options)
  {
    url: options[:url] + options[:path],
    proxy: faraday_proxy(options),
    ssl: faraday_ssl(options),
    headers: faraday_headers(options),
    request: faraday_request(options),
  }
end

#faraday_proxy(options) ⇒ Object

Helper function to evalueate the low level proxy option



74
75
76
77
78
79
# File 'lib/prometheus/alert_buffer_client/client.rb', line 74

def faraday_proxy(options)
  return options[:proxy] if options[:proxy]

  proxy = options[:options]
  proxy[:http_proxy_uri] if proxy[:http_proxy_uri]
end

#faraday_request(options) ⇒ Object

Helper function to evalueate the low level headers option



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/prometheus/alert_buffer_client/client.rb', line 107

def faraday_request(options)
  return options[:request] if options[:request]

  request = options[:options]
  return unless request[:open_timeout] || request[:timeout]

  {
    open_timeout: request[:open_timeout],
    timeout: request[:timeout],
  }
end

#faraday_ssl(options) ⇒ Object

Helper function to evalueate the low level ssl option



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prometheus/alert_buffer_client/client.rb', line 82

def faraday_ssl(options)
  return options[:ssl] if options[:ssl]

  ssl = options[:options]
  return unless ssl[:verify_ssl] || ssl[:ssl_cert_store]

  {
    verify: ssl[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
    cert_store: ssl[:ssl_cert_store],
  }
end

#get(options = {}) ⇒ Hash

Get alerts:

All alerts will be fetched if options are omitted.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :generation_id (String)

    Database generation Id.

  • :from_index (Integer)

    Minimal index of alerts to fetch.

Returns:

  • (Hash)

    response with keys: generationID, messages



56
57
58
59
60
61
62
63
# File 'lib/prometheus/alert_buffer_client/client.rb', line 56

def get(options = {})
  response = @client.get do |req|
    req.params['generationID'] = options[:generation_id]
    req.params['fromIndex'] = options[:from_index]
  end

  response.body
end

#post(alert) ⇒ Object

post alert:

Parameters:

  • alert (String)

    Alert to post



67
68
69
70
71
# File 'lib/prometheus/alert_buffer_client/client.rb', line 67

def post(alert)
  @client.post do |req|
    req.body = alert
  end
end