faraday-cdn-metrics 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +42 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/faraday-cdn-metrics.gemspec +29 -0
- data/lib/faraday_cdn_metrics.rb +74 -0
- data/lib/faraday_cdn_metrics/server_caches/fastly.rb +49 -0
- data/lib/faraday_cdn_metrics/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3c86baef6a57498196107f84d5238eb3f82e0b64
|
|
4
|
+
data.tar.gz: 25826f2b5283099093116ec87b02f61cdbc47335
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3082622a60a1bc7578b05b5d164f1f2f38947b8c1c9999885eb7838944b5a35263524c64ca30e7f259cdd968dd5aadc0cd235263ff07436df8c223bbedee62c0
|
|
7
|
+
data.tar.gz: cdd357e460e675d5a1ef8e74338396a5806a4256802955adf815b8a681750e832f1b5eff2c3be78afca95dc8fac946a2d7e0bd41434a108f80ee0a7667029eb6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Anson Kelly
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Faraday::Cdn::Metrics
|
|
2
|
+
|
|
3
|
+
Adds instrumentation to a faraday stack to capture server cache hits / misses. Currently only supports fastly.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'faraday-cdn-metrics'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install faraday-cdn-metrics
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Initialise the middleware with an instrumentation class. It must respond to :instrument (eg ActiveSupport::Notifications)
|
|
24
|
+
|
|
25
|
+
Use as faraday middleware:
|
|
26
|
+
```
|
|
27
|
+
Faraday.new do |b|
|
|
28
|
+
b.use :cdn_metrics, instrumenter: ActiveSupport::Notifications
|
|
29
|
+
|
|
30
|
+
b.adapter :typhoeus
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Development
|
|
35
|
+
|
|
36
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
37
|
+
|
|
38
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
39
|
+
|
|
40
|
+
## Contributing
|
|
41
|
+
|
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/faraday-cdn-metrics.
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'bundler/gem_tasks'
|
|
2
|
+
require 'rspec/core/rake_task'
|
|
3
|
+
|
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
5
|
+
|
|
6
|
+
task :default => :spec
|
|
7
|
+
|
|
8
|
+
desc "Upload to rubygems"
|
|
9
|
+
task :upload => :build do
|
|
10
|
+
if tag_exists?(current_tag_name)
|
|
11
|
+
puts "Tag exists, did you run rake increase_revision_number after merging with master?"
|
|
12
|
+
exit 1
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
create_tag(current_tag_name)
|
|
16
|
+
Rake::Task[:release].invoke
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
desc "Increase the revision number"
|
|
20
|
+
task :increase_revision_number do
|
|
21
|
+
version_file = "lib/faraday_cdn_metrics/version.rb"
|
|
22
|
+
file_content = File.read(version_file)
|
|
23
|
+
rule = /(\d+\.\d+\.)(\d+)/
|
|
24
|
+
new_revision_number = rule.match(file_content)[2].to_i + 1
|
|
25
|
+
new_file_content = file_content.sub(rule, '\1' + new_revision_number.to_s)
|
|
26
|
+
|
|
27
|
+
File.open(version_file, 'w') { |file| file.write(new_file_content) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def tag_exists?(tag_name)
|
|
31
|
+
result = `git tag | grep #{tag_name}`.strip
|
|
32
|
+
result == tag_name
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def create_tag(tag_name)
|
|
36
|
+
sh "git tag -a #{tag_name} -m \"Released version #{tag_name}\""
|
|
37
|
+
sh "git push origin #{tag_name}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def current_tag_name
|
|
41
|
+
"v#{FaradayCdnMetrics::VERSION}"
|
|
42
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "faraday/cdn/metrics"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "faraday_cdn_metrics/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "faraday-cdn-metrics"
|
|
8
|
+
spec.version = FaradayCdnMetrics::VERSION
|
|
9
|
+
spec.authors = ["Anson Kelly"]
|
|
10
|
+
spec.email = ["anson.kelly@carwow.co.uk"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Metrics requests to a CDN like Fastly}
|
|
13
|
+
spec.description = %q{Metrics requests to a CDN like Fastly}
|
|
14
|
+
spec.homepage = "https://github.com/carwow/faraday-cdn-metrics"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
|
19
|
+
end
|
|
20
|
+
spec.bindir = "exe"
|
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
23
|
+
|
|
24
|
+
spec.add_dependency "faraday", '~> 0.11'
|
|
25
|
+
|
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
29
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require 'faraday'
|
|
2
|
+
require 'faraday_cdn_metrics/version'
|
|
3
|
+
require 'faraday_cdn_metrics/server_caches/fastly'
|
|
4
|
+
|
|
5
|
+
module FaradayCdnMetrics
|
|
6
|
+
class CdnMetrics < Faraday::Middleware
|
|
7
|
+
|
|
8
|
+
# Cache status' shamelessly copied from faraday-http-cache
|
|
9
|
+
CACHE_STATUSES = [
|
|
10
|
+
# The request was not cacheable.
|
|
11
|
+
:unacceptable,
|
|
12
|
+
|
|
13
|
+
# The response was cached and can still be used.
|
|
14
|
+
:fresh,
|
|
15
|
+
|
|
16
|
+
# The response was cached and the server has validated it with a 304 response.
|
|
17
|
+
:valid,
|
|
18
|
+
|
|
19
|
+
# The response was cache but was revalidated by the sserver.
|
|
20
|
+
:invalid,
|
|
21
|
+
|
|
22
|
+
# No response was found in the cache.
|
|
23
|
+
:miss,
|
|
24
|
+
|
|
25
|
+
# The response can't be cached.
|
|
26
|
+
:uncacheable,
|
|
27
|
+
|
|
28
|
+
# The request decided to ignore the cache.
|
|
29
|
+
:bypass
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
32
|
+
EVENT_NAME = 'cdn_metrics.http_cache'.freeze
|
|
33
|
+
|
|
34
|
+
def initialize(app, instrumenter: nil, instrument_name: EVENT_NAME, server_cache: FaradayCdnMetrics::ServerCaches::Fastly)
|
|
35
|
+
super(app)
|
|
36
|
+
|
|
37
|
+
@instrumenter = instrumenter
|
|
38
|
+
@instrument_name = instrument_name
|
|
39
|
+
@server_cache_class = server_cache
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def call(request_env)
|
|
43
|
+
@app.call(request_env).on_complete do |response_env|
|
|
44
|
+
client_cache_status = find_client_cache_status(response_env)
|
|
45
|
+
server_cache_status = find_server_cache_status(response_env)
|
|
46
|
+
|
|
47
|
+
@instrumenter.instrument(@instrument_name, { env: response_env, client_cache_status: client_cache_status, server_cache_status: server_cache_status })
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def find_client_cache_status(env)
|
|
54
|
+
trace = (env[:http_cache_trace] || [])
|
|
55
|
+
CACHE_STATUSES.find { |status| trace.include?(status) } || :unknown
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def find_server_cache_status(env)
|
|
59
|
+
server_cache = @server_cache_class.new(env[:response_headers])
|
|
60
|
+
|
|
61
|
+
return :pass if server_cache.pass?
|
|
62
|
+
return :valid if server_cache.hit_while_revalidate?
|
|
63
|
+
return :fresh if server_cache.hit?
|
|
64
|
+
return :miss if server_cache.miss?
|
|
65
|
+
:unknown
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
if Faraday.respond_to?(:register_middleware)
|
|
71
|
+
Faraday.register_middleware cdn_metrics: FaradayCdnMetrics::CdnMetrics
|
|
72
|
+
elsif Faraday::Middleware.respond_to?(:register_middleware)
|
|
73
|
+
Faraday::Middleware.register_middleware cdn_metrics: FaradayCdnMetrics::CdnMetrics
|
|
74
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module FaradayCdnMetrics
|
|
2
|
+
module ServerCaches
|
|
3
|
+
class Fastly
|
|
4
|
+
|
|
5
|
+
def initialize(response_headers = {})
|
|
6
|
+
@response_headers = response_headers
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def hit?
|
|
10
|
+
x_cache_hit?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def miss?
|
|
14
|
+
x_cache_miss?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def pass?
|
|
18
|
+
x_cache_miss? && fetch_age.nil?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def hit_while_revalidate?
|
|
22
|
+
max_age = fetch_max_age.to_i
|
|
23
|
+
age = fetch_age.to_i
|
|
24
|
+
|
|
25
|
+
x_cache_hit? && max_age > 0 && age > max_age
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
attr_reader :response_headers
|
|
31
|
+
|
|
32
|
+
def x_cache_hit?
|
|
33
|
+
response_headers.fetch('X-Cache', nil) == 'HIT'
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def x_cache_miss?
|
|
37
|
+
response_headers.fetch('X-Cache', nil) == 'MISS'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def fetch_max_age
|
|
41
|
+
response_headers.fetch('Cache-Control', '').match(/max-age=(\d+)/)&.captures&.first
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def fetch_age
|
|
45
|
+
response_headers.fetch('Age', nil)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: faraday-cdn-metrics
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Anson Kelly
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-10-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: faraday
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.11'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.11'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.15'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.15'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
description: Metrics requests to a CDN like Fastly
|
|
70
|
+
email:
|
|
71
|
+
- anson.kelly@carwow.co.uk
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- ".rspec"
|
|
78
|
+
- Gemfile
|
|
79
|
+
- LICENSE.txt
|
|
80
|
+
- README.md
|
|
81
|
+
- Rakefile
|
|
82
|
+
- bin/console
|
|
83
|
+
- bin/setup
|
|
84
|
+
- faraday-cdn-metrics.gemspec
|
|
85
|
+
- lib/faraday_cdn_metrics.rb
|
|
86
|
+
- lib/faraday_cdn_metrics/server_caches/fastly.rb
|
|
87
|
+
- lib/faraday_cdn_metrics/version.rb
|
|
88
|
+
homepage: https://github.com/carwow/faraday-cdn-metrics
|
|
89
|
+
licenses:
|
|
90
|
+
- MIT
|
|
91
|
+
metadata: {}
|
|
92
|
+
post_install_message:
|
|
93
|
+
rdoc_options: []
|
|
94
|
+
require_paths:
|
|
95
|
+
- lib
|
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
|
+
requirements:
|
|
103
|
+
- - ">="
|
|
104
|
+
- !ruby/object:Gem::Version
|
|
105
|
+
version: '0'
|
|
106
|
+
requirements: []
|
|
107
|
+
rubyforge_project:
|
|
108
|
+
rubygems_version: 2.5.2
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: Metrics requests to a CDN like Fastly
|
|
112
|
+
test_files: []
|