1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# encoding: UTF-8
require 'prometheus/client/gauge'
require 'prometheus/client/push'
describe Prometheus::Client::Push do
let(:gateway) { 'http://localhost:9091' }
let(:registry) { Prometheus::Client::Registry.new }
let(:grouping_key) { {} }
let(:push) { Prometheus::Client::Push.new(job: 'test-job', gateway: gateway, grouping_key: grouping_key, open_timeout: 5, read_timeout: 30) }
describe '.new' do
it 'returns a new push instance' do
expect(push).to be_a(Prometheus::Client::Push)
end
it 'uses localhost as default Pushgateway' do
push = Prometheus::Client::Push.new(job: 'test-job')
expect(push.gateway).to eql('http://localhost:9091')
end
it 'allows to specify a custom Pushgateway' do
push = Prometheus::Client::Push.new(job: 'test-job', gateway: 'http://pu.sh:1234')
expect(push.gateway).to eql('http://pu.sh:1234')
end
it 'raises an ArgumentError if the job is nil' do
expect do
Prometheus::Client::Push.new(job: nil)
end.to raise_error ArgumentError
end
it 'raises an ArgumentError if the job is empty' do
expect do
Prometheus::Client::Push.new(job: "")
end.to raise_error ArgumentError
end
it 'raises an ArgumentError if the given gateway URL is invalid' do
['inva.lid:1233', 'http://[invalid]'].each do |url|
expect do
Prometheus::Client::Push.new(job: 'test-job', gateway: url)
end.to raise_error ArgumentError
end
end
it 'raises InvalidLabelError if a grouping key label has an invalid name' do
expect do
Prometheus::Client::Push.new(job: "test-job", grouping_key: { "not_a_symbol" => "foo" })
end.to raise_error Prometheus::Client::LabelSetValidator::InvalidLabelError
end
end
describe '#add' do
it 'sends a given registry to via HTTP POST' do
expect(push).to receive(:request).with(Net::HTTP::Post, registry)
push.add(registry)
end
end
describe '#replace' do
it 'sends a given registry to via HTTP PUT' do
expect(push).to receive(:request).with(Net::HTTP::Put, registry)
push.replace(registry)
end
end
describe '#delete' do
it 'deletes existing metrics with HTTP DELETE' do
expect(push).to receive(:request).with(Net::HTTP::Delete)
push.delete
end
end
describe '#path' do
it 'uses the default metrics path if no grouping key given' do
push = Prometheus::Client::Push.new(job: 'test-job')
expect(push.path).to eql('/metrics/job/test-job')
end
it 'appends additional grouping labels to the path if specified' do
push = Prometheus::Client::Push.new(
job: 'test-job',
grouping_key: { foo: "bar", baz: "qux"},
)
expect(push.path).to eql('/metrics/job/test-job/foo/bar/baz/qux')
end
it 'encodes grouping key label values containing `/` in url-safe base64' do
push = Prometheus::Client::Push.new(
job: 'test-job',
grouping_key: { foo: "bar/baz"},
)
expect(push.path).to eql('/metrics/job/test-job/foo@base64/YmFyL2Jheg==')
end
it 'encodes empty grouping key label values as a single base64 padding character' do
push = Prometheus::Client::Push.new(
job: 'test-job',
grouping_key: { foo: ""},
)
expect(push.path).to eql('/metrics/job/test-job/foo@base64/=')
end
it 'URL-encodes all other non-URL-safe characters' do
push = Prometheus::Client::Push.new(job: '<bar job>', grouping_key: { foo_label: '<bar value>' })
expected = '/metrics/job/%3Cbar%20job%3E/foo_label/%3Cbar%20value%3E'
expect(push.path).to eql(expected)
end
end
describe '#request' do
let(:content_type) { Prometheus::Client::Formats::Text::CONTENT_TYPE }
let(:data) { Prometheus::Client::Formats::Text.marshal(registry) }
let(:uri) { URI.parse("#{gateway}/metrics/job/test-job") }
let(:response) do
double(
:response,
code: '200',
message: 'OK',
body: 'Everything worked'
)
end
it 'sends marshalled registry to the specified gateway' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request).and_return(response)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Post, registry)
end
context 'for a 3xx response' do
let(:response) do
double(
:response,
code: '301',
message: 'Moved Permanently',
body: 'Probably no body, but technically you can return one'
)
end
it 'raises a redirect error' do
request = double(:request)
allow(request).to receive(:content_type=)
allow(request).to receive(:body=)
allow(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
allow(http).to receive(:use_ssl=)
allow(http).to receive(:open_timeout=)
allow(http).to receive(:read_timeout=)
allow(http).to receive(:request).with(request).and_return(response)
allow(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
expect { push.send(:request, Net::HTTP::Post, registry) }.to raise_error(
Prometheus::Client::Push::HttpRedirectError
)
end
end
context 'for a 4xx response' do
let(:response) do
double(
:response,
code: '400',
message: 'Bad Request',
body: 'Info on why the request was bad'
)
end
it 'raises a client error' do
request = double(:request)
allow(request).to receive(:content_type=)
allow(request).to receive(:body=)
allow(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
allow(http).to receive(:use_ssl=)
allow(http).to receive(:open_timeout=)
allow(http).to receive(:read_timeout=)
allow(http).to receive(:request).with(request).and_return(response)
allow(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
expect { push.send(:request, Net::HTTP::Post, registry) }.to raise_error(
Prometheus::Client::Push::HttpClientError
)
end
end
context 'for a 5xx response' do
let(:response) do
double(
:response,
code: '500',
message: 'Internal Server Error',
body: 'Apology for the server code being broken'
)
end
it 'raises a server error' do
request = double(:request)
allow(request).to receive(:content_type=)
allow(request).to receive(:body=)
allow(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
allow(http).to receive(:use_ssl=)
allow(http).to receive(:open_timeout=)
allow(http).to receive(:read_timeout=)
allow(http).to receive(:request).with(request).and_return(response)
allow(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
expect { push.send(:request, Net::HTTP::Post, registry) }.to raise_error(
Prometheus::Client::Push::HttpServerError
)
end
end
it 'deletes data from the registry' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(Net::HTTP::Delete).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(false)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request).and_return(response)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Delete)
end
context 'HTTPS support' do
let(:gateway) { 'https://localhost:9091' }
it 'uses HTTPS when requested' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Post).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request).and_return(response)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.send(:request, Net::HTTP::Post, registry)
end
end
context 'Basic Auth support' do
context 'when credentials are passed in the gateway URL' do
let(:gateway) { 'https://super:secret@localhost:9091' }
it "raises an ArgumentError explaining why we don't support that mechanism" do
expect { push }.to raise_error ArgumentError, /in the gateway URL.*username `super`/m
end
end
context 'when credentials are passed to the separate `basic_auth` method' do
let(:gateway) { 'https://localhost:9091' }
it 'passes the credentials on to the HTTP client' do
request = double(:request)
expect(request).to receive(:content_type=).with(content_type)
expect(request).to receive(:basic_auth).with('super', 'secret')
expect(request).to receive(:body=).with(data)
expect(Net::HTTP::Put).to receive(:new).with(uri).and_return(request)
http = double(:http)
expect(http).to receive(:use_ssl=).with(true)
expect(http).to receive(:open_timeout=).with(5)
expect(http).to receive(:read_timeout=).with(30)
expect(http).to receive(:request).with(request).and_return(response)
expect(Net::HTTP).to receive(:new).with('localhost', 9091).and_return(http)
push.basic_auth("super", "secret")
push.send(:request, Net::HTTP::Put, registry)
end
end
end
context 'with a grouping key that clashes with a metric label' do
let(:grouping_key) { { foo: "bar"} }
before do
gauge = Prometheus::Client::Gauge.new(
:test_gauge,
'test docstring',
foo: nil
)
registry.register(gauge)
gauge.set({ foo: "bar"}, 42)
end
it 'raises an error when grouping key labels conflict with metric labels' do
expect { push.send(:request, Net::HTTP::Post, registry) }.to raise_error(
Prometheus::Client::LabelSetValidator::InvalidLabelSetError
)
end
end
end
end
|