Class: FaradayCdnMetrics::CdnMetrics

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_cdn_metrics.rb

Constant Summary collapse

CACHE_STATUSES =

Cache status’ shamelessly copied from faraday-http-cache

[
  # The request was not cacheable.
  :unacceptable,

  # The response was cached and can still be used.
  :fresh,

  # The response was cached and the server has validated it with a 304 response.
  :valid,

  # The response was cache but was revalidated by the sserver.
  :invalid,

  # No response was found in the cache.
  :miss,

  # The response can't be cached.
  :uncacheable,

  # The request decided to ignore the cache.
  :bypass
].freeze
EVENT_NAME =
'cdn_metrics.http_cache'

Instance Method Summary collapse

Constructor Details

#initialize(app, instrumenter: nil, instrument_name: EVENT_NAME, server_cache: FaradayCdnMetrics::ServerCaches::Fastly) ⇒ CdnMetrics

Returns a new instance of CdnMetrics.



35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#call(request_env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# 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