Skip to content

Commit e87f709

Browse files
authored
Merge pull request #61 from rubyists/serialize-errors
feat: serialize error payloads
2 parents 7b08dfa + 0af4b0b commit e87f709

7 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ Gemfile.lock
5656
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
5757
.rubocop_cache
5858
# .rubocop-https?--*
59+
.idea

‎.release-please-manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.2.10"
2+
".": "0.2.11"
33
}

‎.version.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.10
1+
0.2.11

‎Gemfile.lock‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
leopard (0.2.9)
4+
leopard (0.2.11)
55
concurrent-ruby (~> 1.1)
66
dry-configurable (~> 1.3)
77
dry-monads (~> 1.9)

‎lib/leopard/metrics_server.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'socket'
44
require 'erb'
5+
require 'json'
56

67
module Rubyists
78
module Leopard
@@ -62,8 +63,11 @@ def close_client(client)
6263
def write_metrics_response(client, request_line, workers)
6364
if request_line&.start_with?('GET /metrics')
6465
body = prometheus_metrics(workers)
66+
content_type = body.is_a?(String) ? 'text/plain; version=0.0.4' : 'application/json'
67+
body = JSON.generate(body) unless body.is_a?(String)
68+
6569
client.write "HTTP/1.1 200 OK\r\n" \
66-
"Content-Type: text/plain; version=0.0.4\r\n" \
70+
"Content-Type: #{content_type}\r\n" \
6771
"Content-Length: #{body.bytesize}\r\n\r\n#{body}"
6872
else
6973
client.write "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"

‎lib/leopard/version.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Rubyists
44
module Leopard
55
# x-release-please-start-version
6-
VERSION = '0.2.10'
6+
VERSION = '0.2.11'
77
# x-release-please-end
88
end
99
end

‎test/lib/nats_api_server.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,22 @@ def instance_variable_get(name)
322322

323323
assert_equal expected_metrics, @klass.send(:prometheus_metrics, workers)
324324
end
325+
326+
it 'serializes hash metrics payloads before writing the response' do
327+
client = Object.new
328+
response = nil
329+
client.define_singleton_method(:write) { |payload| response = payload }
330+
331+
@klass.stub(:prometheus_metrics, { error: 'boom' }) do
332+
@klass.send(:write_metrics_response, client, 'GET /metrics HTTP/1.1', [])
333+
end
334+
335+
expected_body = '{"error":"boom"}'
336+
expected_response = "HTTP/1.1 200 OK\r\n" \
337+
"Content-Type: application/json\r\n" \
338+
"Content-Length: #{expected_body.bytesize}\r\n\r\n#{expected_body}"
339+
340+
assert_equal expected_response, response
341+
end
325342
end
326343
end

0 commit comments

Comments
 (0)