forked from prometheus/client_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.rb
92 lines (74 loc) · 2.39 KB
/
exporter.rb
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
# encoding: UTF-8
require 'prometheus/client'
require 'prometheus/client/formats/json'
require 'prometheus/client/formats/text'
module Prometheus
module Client
module Rack
# Exporter is a Rack middleware that provides a sample implementation of
# a Prometheus HTTP client API.
class Exporter
attr_reader :app, :registry, :path
FORMATS = [Formats::Text, Formats::JSON]
FALLBACK = Formats::JSON
def initialize(app, options = {})
@app = app
@registry = options[:registry] || Client.registry
@path = options[:path] || '/metrics'
@acceptable = build_dictionary(FORMATS, FALLBACK)
end
def call(env)
if env['PATH_INFO'] == @path
format = negotiate(env['HTTP_ACCEPT'], @acceptable)
format ? respond_with(format) : not_acceptable(FORMATS)
else
@app.call(env)
end
end
private
def negotiate(accept, formats)
accept = '*/*' if accept.to_s.empty?
parse(accept).each do |content_type, _|
return formats[content_type] if formats.key?(content_type)
end
nil
end
def parse(header)
header.to_s.split(/\s*,\s*/).map do |type|
attributes = type.split(/\s*;\s*/)
quality = extract_quality(attributes)
[attributes.join('; '), quality]
end.sort_by(&:last).reverse
end
def extract_quality(attributes, default = 1.0)
quality = default
attributes.delete_if do |attr|
quality = attr.split('q=').last.to_f if attr.start_with?('q=')
end
quality
end
def respond_with(format)
[
200,
{ 'Content-Type' => format::CONTENT_TYPE },
[format.marshal(@registry)],
]
end
def not_acceptable(formats)
types = formats.map { |format| format::MEDIA_TYPE }
[
406,
{ 'Content-Type' => 'text/plain' },
["Supported media types: #{types.join(', ')}"],
]
end
def build_dictionary(formats, fallback)
formats.each_with_object('*/*' => fallback) do |format, memo|
memo[format::CONTENT_TYPE] = format
memo[format::MEDIA_TYPE] = format
end
end
end
end
end
end