Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ Gemfile.lock
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
.rubocop_cache
# .rubocop-https?--*
.idea
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.2.10"
".": "0.2.11"
}
2 changes: 1 addition & 1 deletion .version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.10
0.2.11
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
leopard (0.2.9)
leopard (0.2.11)
concurrent-ruby (~> 1.1)
dry-configurable (~> 1.3)
dry-monads (~> 1.9)
Expand Down
6 changes: 5 additions & 1 deletion lib/leopard/metrics_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'socket'
require 'erb'
require 'json'

module Rubyists
module Leopard
Expand Down Expand Up @@ -62,8 +63,11 @@ def close_client(client)
def write_metrics_response(client, request_line, workers)
if request_line&.start_with?('GET /metrics')
body = prometheus_metrics(workers)
content_type = body.is_a?(String) ? 'text/plain; version=0.0.4' : 'application/json'
body = JSON.generate(body) unless body.is_a?(String)

client.write "HTTP/1.1 200 OK\r\n" \
"Content-Type: text/plain; version=0.0.4\r\n" \
"Content-Type: #{content_type}\r\n" \
"Content-Length: #{body.bytesize}\r\n\r\n#{body}"
else
client.write "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"
Expand Down
2 changes: 1 addition & 1 deletion lib/leopard/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Rubyists
module Leopard
# x-release-please-start-version
VERSION = '0.2.10'
VERSION = '0.2.11'
Comment thread
gabeodess marked this conversation as resolved.
# x-release-please-end
end
end
17 changes: 17 additions & 0 deletions test/lib/nats_api_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,22 @@ def instance_variable_get(name)

assert_equal expected_metrics, @klass.send(:prometheus_metrics, workers)
end

it 'serializes hash metrics payloads before writing the response' do
client = Object.new
response = nil
client.define_singleton_method(:write) { |payload| response = payload }

@klass.stub(:prometheus_metrics, { error: 'boom' }) do
@klass.send(:write_metrics_response, client, 'GET /metrics HTTP/1.1', [])
end

expected_body = '{"error":"boom"}'
expected_response = "HTTP/1.1 200 OK\r\n" \
"Content-Type: application/json\r\n" \
"Content-Length: #{expected_body.bytesize}\r\n\r\n#{expected_body}"

assert_equal expected_response, response
end
end
end
Loading