Class: Lens::Rails::MetricsExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lens/rails/metrics_exporter.rb

Overview

Rack middleware that tracks request counts and durations, flushing them to Lens every 30 s via POST /v1/metrics.

Constant Summary collapse

RESERVOIR_SIZE =
1024

Instance Method Summary collapse

Constructor Details

#initialize(app, url:, token:, service_name:, open_timeout: 2, read_timeout: 2) ⇒ MetricsExporter

Returns a new instance of MetricsExporter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lens/rails/metrics_exporter.rb', line 13

def initialize(app, url:, token:, service_name:,
  open_timeout: 2,
  read_timeout: 2)
  @app = app
  @token = token
  @service_name = service_name
  @uri = URI("#{url}/v1/metrics")
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @mutex = Mutex.new
  reset_window
  restart_flush_thread
  Lens::Rails.register_flushable(self)
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/lens/rails/metrics_exporter.rb', line 28

def call(env)
  Thread.current[:lens_request_id] = env["action_dispatch.request_id"]
  wall_start = Time.now
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  status, headers, body = @app.call(env)
  duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000.0
  queue_time = parse_queue_time(env, wall_start)

  @mutex.synchronize do
    @request_count += 1
    @error_count += 1 if status >= 500
    @duration_sum += duration
    @duration_min = duration if @duration_min.nil? || duration < @duration_min
    @duration_max = duration if @duration_max.nil? || duration > @duration_max
    if @samples.size < RESERVOIR_SIZE
      @samples << duration
    else
      idx = rand(@request_count)
      @samples[idx] = duration if idx < RESERVOIR_SIZE
    end
    if queue_time
      @queue_samples << queue_time
      @queue_sum += queue_time
    end
  end

  [status, headers, body]
ensure
  Thread.current[:lens_request_id] = nil
end

#flushObject



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lens/rails/metrics_exporter.rb', line 59

def flush
  snapshot = @mutex.synchronize do
    s = {count: @request_count, errors: @error_count,
         sum: @duration_sum, min: @duration_min, max: @duration_max,
         samples: @samples, queue_samples: @queue_samples, queue_sum: @queue_sum}
    reset_window
    s
  end

  return if snapshot[:count].zero?

  now = Time.now.iso8601(3)
  avg = snapshot[:sum] / snapshot[:count]
  p50 = percentile(snapshot[:samples], 0.50)
  p95 = percentile(snapshot[:samples], 0.95)

  metrics = [
    {name: "http.server.request_count", unit: "1", value: snapshot[:count].to_f, timestamp: now},
    {name: "http.server.error_count", unit: "1", value: snapshot[:errors].to_f, timestamp: now},
    {name: "http.server.duration", unit: "ms", value: avg, timestamp: now},
    {name: "http.server.duration.p50", unit: "ms", value: p50, timestamp: now},
    {name: "http.server.duration.p95", unit: "ms", value: p95, timestamp: now}
  ]

  if snapshot[:queue_samples].any?
    q_avg = snapshot[:queue_sum] / snapshot[:queue_samples].size
    q_p50 = percentile(snapshot[:queue_samples], 0.50)
    q_p95 = percentile(snapshot[:queue_samples], 0.95)
    metrics += [
      {name: "http.server.queue_time", unit: "ms", value: q_avg, timestamp: now},
      {name: "http.server.queue_time.p50", unit: "ms", value: q_p50, timestamp: now},
      {name: "http.server.queue_time.p95", unit: "ms", value: q_p95, timestamp: now}
    ]
  end

  body = JSON.generate(metrics: metrics, service: @service_name, version: Lens::Rails::VERSION)

  req = Net::HTTP::Post.new(@uri)
  req["Content-Type"] = "application/octet-stream"
  req["Authorization"] = "Bearer #{@token}"
  req.body = Zlib.gzip(body)

  Thread.current[:lens_in_export] = true
  begin
    Net::HTTP.start(@uri.host, @uri.port,
      use_ssl: @uri.scheme == "https",
      open_timeout: @open_timeout,
      read_timeout: @read_timeout) { |http| http.request(req) }
  ensure
    Thread.current[:lens_in_export] = false
  end
end

#restart_flush_threadObject



117
118
119
# File 'lib/lens/rails/metrics_exporter.rb', line 117

def restart_flush_thread
  BackoffLoop.new(base: 30, max: 120, name: "lens-metrics-flush").start { flush }
end

#shutdown(timeout:) ⇒ Object



112
113
114
115
# File 'lib/lens/rails/metrics_exporter.rb', line 112

def shutdown(timeout:)
  Thread.new { flush }.join(timeout)
rescue
end