Skip to content

Commit

Permalink
Add possibility to talk HTTPS in push client
Browse files Browse the repository at this point in the history
Fixes #38.
  • Loading branch information
colszowka authored and grobie committed Feb 14, 2017
1 parent 1b458a0 commit b055be7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/prometheus/client/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Push
PATH = '/metrics/jobs/%s'.freeze
INSTANCE_PATH = '/metrics/jobs/%s/instances/%s'.freeze
HEADER = { 'Content-Type' => Formats::Text::CONTENT_TYPE }.freeze
SUPPORTED_SCHEMES = %w(http https).freeze

attr_reader :job, :instance, :gateway, :path

Expand All @@ -26,6 +27,7 @@ def initialize(job, instance = nil, gateway = nil)
@uri = parse(@gateway)
@path = build_path(job, instance)
@http = Net::HTTP.new(@uri.host, @uri.port)
@http.use_ssl = @uri.scheme == 'https'
end

def add(registry)
Expand All @@ -45,7 +47,7 @@ def delete
def parse(url)
uri = URI.parse(url)

if uri.scheme != 'http'
unless SUPPORTED_SCHEMES.include?(uri.scheme)
raise ArgumentError, 'only HTTP gateway URLs are supported currently.'
end

Expand Down
19 changes: 18 additions & 1 deletion spec/prometheus/client/push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,33 @@
end

describe '#add' do
it 'pushes a given registry to the configured Pushgateway' do
it 'pushes a given registry to the configured Pushgateway via HTTP' do
http = double(:http)
expect(http).to receive(:send_request).with(
'POST',
'/metrics/jobs/foo/instances/bar',
Prometheus::Client::Formats::Text.marshal(registry),
'Content-Type' => Prometheus::Client::Formats::Text::CONTENT_TYPE,
)
expect(http).to receive(:use_ssl=).with(false)
expect(Net::HTTP).to receive(:new).with('pu.sh', 9091).and_return(http)

described_class.new('foo', 'bar', 'http://pu.sh:9091').add(registry)
end

it 'pushes a given registry to the configured Pushgateway via HTTPS' do
http = double(:http)
expect(http).to receive(:send_request).with(
'POST',
'/metrics/jobs/foo/instances/bar',
Prometheus::Client::Formats::Text.marshal(registry),
'Content-Type' => Prometheus::Client::Formats::Text::CONTENT_TYPE,
)
expect(http).to receive(:use_ssl=).with(true)
expect(Net::HTTP).to receive(:new).with('pu.sh', 9091).and_return(http)

described_class.new('foo', 'bar', 'https://pu.sh:9091').add(registry)
end
end

describe '#replace' do
Expand All @@ -75,6 +90,7 @@
Prometheus::Client::Formats::Text.marshal(registry),
'Content-Type' => Prometheus::Client::Formats::Text::CONTENT_TYPE,
)
expect(http).to receive(:use_ssl=).with(false)
expect(Net::HTTP).to receive(:new).with('pu.sh', 9091).and_return(http)

described_class.new('foo', 'bar', 'http://pu.sh:9091').replace(registry)
Expand All @@ -88,6 +104,7 @@
'DELETE',
'/metrics/jobs/foo/instances/bar',
)
expect(http).to receive(:use_ssl=).with(false)
expect(Net::HTTP).to receive(:new).with('pu.sh', 9091).and_return(http)

described_class.new('foo', 'bar', 'http://pu.sh:9091').delete
Expand Down

0 comments on commit b055be7

Please sign in to comment.