diff --git a/.gitignore b/.gitignore index 219c2b6..8fc4f3b 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,4 @@ Gemfile.lock # Used by RuboCop. Remote config files pulled in from inherit_from directive. .rubocop_cache # .rubocop-https?--* +.idea diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 033e2d4..0ed71f7 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.2.10" + ".": "0.2.11" } diff --git a/.version.txt b/.version.txt index 13dead7..d3b5ba4 100644 --- a/.version.txt +++ b/.version.txt @@ -1 +1 @@ -0.2.10 +0.2.11 diff --git a/Gemfile.lock b/Gemfile.lock index 717b384..9e8a069 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/lib/leopard/metrics_server.rb b/lib/leopard/metrics_server.rb index 185c19f..0b89965 100644 --- a/lib/leopard/metrics_server.rb +++ b/lib/leopard/metrics_server.rb @@ -2,6 +2,7 @@ require 'socket' require 'erb' +require 'json' module Rubyists module Leopard @@ -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" diff --git a/lib/leopard/version.rb b/lib/leopard/version.rb index b33a0bd..dee514e 100644 --- a/lib/leopard/version.rb +++ b/lib/leopard/version.rb @@ -3,7 +3,7 @@ module Rubyists module Leopard # x-release-please-start-version - VERSION = '0.2.10' + VERSION = '0.2.11' # x-release-please-end end end diff --git a/test/lib/nats_api_server.rb b/test/lib/nats_api_server.rb index 180dca9..34e931b 100755 --- a/test/lib/nats_api_server.rb +++ b/test/lib/nats_api_server.rb @@ -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