callcounter 0.0.0 → 0.1.4
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 +4 -4
- data/LICENSE +23 -0
- data/README.md +76 -0
- data/lib/callcounter/capturer.rb +99 -0
- data/lib/callcounter/configuration.rb +18 -0
- data/lib/callcounter/railtie.rb +10 -0
- data/lib/callcounter/version.rb +5 -0
- data/lib/callcounter.rb +20 -1
- metadata +17 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99d0cc0bcdf0500c5203169c4f24174faa1e34bc0d6b51b9caabac4c798916bb
|
4
|
+
data.tar.gz: 90df76c181d3ecbb4520fe277969a1abadaefab1c7f69adc7fe0dbb4e92592c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a41c1476531b297327569a83c1dbbe176a96731671351ac98d23a7755aa0c4b41c0351163eddc88ac20540e7c40593b3529fdd9d3b22230b06e0e0d1ff340481
|
7
|
+
data.tar.gz: 581cae38256f5ec226ddb541cc90463b99f22f96bfea2d14f694050bf34da2321c6227de2f5b5d1e896faddaa5954267d4bd564d7df502b3da1fd53a2ab1c39c
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2021, Lint Ltd
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
20
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
21
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
22
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
23
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Callcounter Ruby integration gem
|
2
|
+
|
3
|
+
This gem can be used to gather API request & response data from Rack based applications and send it to Callcounter.
|
4
|
+
|
5
|
+
Callcounter is an API analytics platform that collect information about requests (calls) to your API using so-called integrations. Integrations come in the form of a Ruby gem, a Nuget package, a Pip module, etc. The integrations
|
6
|
+
can send the data to Callcounter using an API, which is described at: https://callcounter.io/docs/api
|
7
|
+
|
8
|
+
After collection data, the web interface can then be used to view all kinds of metrics, giving you insight in the
|
9
|
+
(mis)usage of your API.
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
### Bundler
|
14
|
+
|
15
|
+
When using bundler, simply add the following line to your Gemfile and run `bundle`:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'callcounter'
|
19
|
+
```
|
20
|
+
|
21
|
+
### Other
|
22
|
+
|
23
|
+
Install the gem with: `gem install callcounter`. Next, add the following call to your entry file in order to activate
|
24
|
+
the Rack middleware:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
use Callcounter::Capturer
|
28
|
+
```
|
29
|
+
|
30
|
+
## Configure what to capture
|
31
|
+
|
32
|
+
Configure callcounter with the following code, this can be placed in a Rails initializer when using Ruby on Rails,
|
33
|
+
for example `config/initializers/callcounter.rb`, or somewhere in your entry point file when using Sinatra or other Rack based frameworks:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Callcounter.configure do |config|
|
37
|
+
config.app_token = '' # TODO: fill with your unique app token
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
This will capture any requests to the `api` subdomain and any request that has a path which starts with `/api`.
|
42
|
+
After deploying you should start seeing data in Callcounter. Note that this might take some time because this gems
|
43
|
+
only sends data every few requests or every few minutes.
|
44
|
+
|
45
|
+
If you API doesn't match with the default matching rules, you can add a lambda that will be called for every request
|
46
|
+
to determine whether it was a request to your API. For example, you can customise the default lambda shown below:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Callcounter.configure do |config|
|
50
|
+
config.app_token = '' # TODO: fill with your unique app token
|
51
|
+
config.track = lambda { |request|
|
52
|
+
request.hostname.start_with?('api') || request.path.start_with?('/api')
|
53
|
+
}
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Bug reporting
|
58
|
+
|
59
|
+
Bugs can be reported through the Github issues.
|
60
|
+
If you don't want to sign up for an account, you can also contact us through https://callcounter.io/contact
|
61
|
+
|
62
|
+
## Releasing
|
63
|
+
|
64
|
+
- Verify tests pass.
|
65
|
+
- Increment version number in: `lib/callcounter/version.rb`
|
66
|
+
- Run `bundle install`
|
67
|
+
- Commit all changes
|
68
|
+
- Create a git tag for the release.
|
69
|
+
- Push the git tag.
|
70
|
+
- Build the gem: `gem build callcounter.gemspec`
|
71
|
+
- Push the gem to rubygems.org: `gem push callcounter-?.?.?.gem`
|
72
|
+
|
73
|
+
## About Callcounter
|
74
|
+
|
75
|
+
[Callcounter](https://callcounter.io) is a service, developed and maintained by Lint Ltd, that
|
76
|
+
helps API providers with debugging and optimising the usage of their APIs.
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module Callcounter
|
6
|
+
# class that captures the rack requests and sends them to Callcounter
|
7
|
+
class Capturer
|
8
|
+
attr_accessor :buffer
|
9
|
+
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
@buffer = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
start = Time.now
|
17
|
+
result = @app.call(env)
|
18
|
+
finish = Time.now
|
19
|
+
|
20
|
+
return result if app_token.nil?
|
21
|
+
|
22
|
+
threading do
|
23
|
+
background_work(start, finish, env, result)
|
24
|
+
end
|
25
|
+
|
26
|
+
result
|
27
|
+
end
|
28
|
+
|
29
|
+
def debug?
|
30
|
+
Callcounter.configuration.debug
|
31
|
+
end
|
32
|
+
|
33
|
+
def track?(request)
|
34
|
+
Callcounter.configuration.track.call(request)
|
35
|
+
end
|
36
|
+
|
37
|
+
def project_token
|
38
|
+
Callcounter.configuration.project_token
|
39
|
+
end
|
40
|
+
|
41
|
+
def app_token
|
42
|
+
Callcounter.configuration.app_token || project_token
|
43
|
+
end
|
44
|
+
|
45
|
+
def async?
|
46
|
+
Callcounter.configuration.async
|
47
|
+
end
|
48
|
+
|
49
|
+
def threading(&block)
|
50
|
+
if async?
|
51
|
+
Thread.new(&block)
|
52
|
+
else
|
53
|
+
yield
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def send_buffer
|
58
|
+
http = Net::HTTP.new('api.callcounter.io', 443)
|
59
|
+
http.use_ssl = true
|
60
|
+
track = Net::HTTP::Post.new('/api/v1/events/batch')
|
61
|
+
track['content-type'] = 'application/json'
|
62
|
+
track['user-agent'] = "callcounter-gem (#{Callcounter::VERSION})"
|
63
|
+
track.body = { batch: { project_token: app_token, events: @buffer } }.to_json
|
64
|
+
|
65
|
+
http.request(track)
|
66
|
+
|
67
|
+
puts 'sent request' if debug?
|
68
|
+
rescue StandardError
|
69
|
+
puts 'failed to send request' if debug?
|
70
|
+
end
|
71
|
+
|
72
|
+
def should_send_buffer?
|
73
|
+
@buffer.first[:created_at] < Time.now - Random.rand(300..359) || @buffer.size > 25
|
74
|
+
end
|
75
|
+
|
76
|
+
def event_attributes(start, finish, request, result)
|
77
|
+
{
|
78
|
+
created_at: start,
|
79
|
+
elapsed_time: ((finish - start) * 1000).round,
|
80
|
+
method: request.request_method,
|
81
|
+
path: request.path,
|
82
|
+
user_agent: request.user_agent,
|
83
|
+
status: result.first
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
def background_work(start, finish, env, result)
|
88
|
+
request = Rack::Request.new(env)
|
89
|
+
return unless track?(request)
|
90
|
+
|
91
|
+
@buffer << event_attributes(start, finish, request, result)
|
92
|
+
|
93
|
+
return unless should_send_buffer?
|
94
|
+
|
95
|
+
send_buffer
|
96
|
+
@buffer = []
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Callcounter
|
4
|
+
# configuration class for Callcounter middleware
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :debug, :project_token, :track, :async, :app_token
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@debug = false
|
10
|
+
@project_token = nil
|
11
|
+
@app_token = nil
|
12
|
+
@track = lambda { |request|
|
13
|
+
return request.hostname.start_with?('api') || request.path.start_with?('/api')
|
14
|
+
}
|
15
|
+
@async = true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Callcounter
|
4
|
+
# enable the Callcounter rack middleware automatically in rails
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'callcounter.middleware' do |app|
|
7
|
+
app.middleware.use(Callcounter::Capturer)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/callcounter.rb
CHANGED
@@ -1,2 +1,21 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'callcounter/capturer'
|
4
|
+
require 'callcounter/configuration'
|
5
|
+
require 'callcounter/railtie' if defined?(Rails::Railtie)
|
6
|
+
require 'callcounter/version'
|
7
|
+
|
8
|
+
# rack middleware that captures incoming api requests and sends them to Callcounter
|
9
|
+
module Callcounter
|
10
|
+
class << self
|
11
|
+
attr_writer :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Callcounter::Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
yield(configuration)
|
20
|
+
end
|
2
21
|
end
|
metadata
CHANGED
@@ -1,25 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callcounter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- George Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: Callcounter is a service that helps API providers with debugging and
|
14
|
+
optimising the usage of their APIs.
|
15
|
+
email:
|
16
|
+
- george@lint.com
|
15
17
|
executables: []
|
16
18
|
extensions: []
|
17
19
|
extra_rdoc_files: []
|
18
20
|
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
19
23
|
- lib/callcounter.rb
|
20
|
-
|
21
|
-
|
22
|
-
-
|
24
|
+
- lib/callcounter/capturer.rb
|
25
|
+
- lib/callcounter/configuration.rb
|
26
|
+
- lib/callcounter/railtie.rb
|
27
|
+
- lib/callcounter/version.rb
|
28
|
+
homepage: https://github.com/Callcounter/callcounter-ruby
|
29
|
+
licenses: []
|
23
30
|
metadata: {}
|
24
31
|
post_install_message:
|
25
32
|
rdoc_options: []
|
@@ -36,8 +43,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
43
|
- !ruby/object:Gem::Version
|
37
44
|
version: '0'
|
38
45
|
requirements: []
|
39
|
-
rubygems_version: 3.
|
46
|
+
rubygems_version: 3.2.32
|
40
47
|
signing_key:
|
41
48
|
specification_version: 4
|
42
|
-
summary:
|
49
|
+
summary: Callcounter integration gem to gather API request statistics
|
43
50
|
test_files: []
|