class FaradayCdnMetrics::CdnMetrics

Constants

CACHE_STATUSES

Cache status' shamelessly copied from faraday-http-cache

EVENT_NAME

Public Class Methods

new(app, instrumenter: nil, instrument_name: EVENT_NAME, server_cache: FaradayCdnMetrics::ServerCaches::Fastly) click to toggle source
Calls superclass method
# File lib/faraday_cdn_metrics.rb, line 35
def initialize(app,
               instrumenter: nil,
               instrument_name: EVENT_NAME,
               server_cache: FaradayCdnMetrics::ServerCaches::Fastly)
  super(app)

  @instrumenter = instrumenter
  @instrument_name = instrument_name
  @server_cache_class = server_cache
end

Public Instance Methods

call(request_env) click to toggle source
# File lib/faraday_cdn_metrics.rb, line 46
def call(request_env)
  @app.call(request_env).on_complete do |response_env|
    client_cache_status = find_client_cache_status(response_env)
    server_cache_status = find_server_cache_status(response_env)

    @instrumenter.instrument(@instrument_name, {
                               env: response_env,
                               client_cache_status: client_cache_status,
                               server_cache_status: server_cache_status
                             })
  end
end

Private Instance Methods

find_client_cache_status(env) click to toggle source
# File lib/faraday_cdn_metrics.rb, line 61
def find_client_cache_status(env)
  trace = (env[:http_cache_trace] || [])
  CACHE_STATUSES.find { |status| trace.include?(status) } || :unknown
end
find_server_cache_status(env) click to toggle source
# File lib/faraday_cdn_metrics.rb, line 66
def find_server_cache_status(env)
  server_cache = @server_cache_class.new(env[:response_headers])

  return :pass if server_cache.pass?
  return :valid if server_cache.hit_while_revalidate?
  return :fresh if server_cache.hit?
  return :miss if server_cache.miss?

  :unknown
end