callcounter 0.0.0 → 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 +4 -4
- data/LICENSE +23 -0
- data/README.md +44 -0
- data/lib/callcounter.rb +20 -1
- data/lib/callcounter/capturer.rb +93 -0
- data/lib/callcounter/configuration.rb +17 -0
- data/lib/callcounter/railtie.rb +10 -0
- data/lib/callcounter/version.rb +5 -0
- metadata +17 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c162b867a3b3a0050d698462603b089142c29460c49254bae7e78705eef1b0ff
|
4
|
+
data.tar.gz: 8613f4d81bf8e88f3bf083dd7360ab8e7255ecacc1fac0e4d74ff555fadfb579
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0513ed8e497d09a4c5c14735415b5b054546cf0bccbfa86e6e006420852f1c1107a9cc50b7bdc6781dfcd4e9f892c66f36ec2f801fc36e9ead53b8ed4416b3c
|
7
|
+
data.tar.gz: 7f0d38080e754246ca32f73f2cd2ff7a37e1bd9ca5737fdb8a21143d8effd8dca48a79c0a396a00b53b8c229e076f51c1eea125ea7f6bce0f6f0f5fdb640f97d
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2021, Webindie
|
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,44 @@
|
|
1
|
+
[](https://builds.sr.ht/~webindie/callcounter-gem?)
|
2
|
+
|
3
|
+
# Callcounter Ruby integration gem
|
4
|
+
|
5
|
+
This gem can be used to gather API request & response data from Rack based applications.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
When using bundler, simply add the following line to your Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'callcounter'
|
13
|
+
```
|
14
|
+
|
15
|
+
Run `bundle install` in your project directory and create an initializer
|
16
|
+
`config/initializers/callcounter.rb` with the following content:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
Callcounter.configure do |config|
|
20
|
+
config.project_token = '' # TODO: fill with your unique project token
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
After deploying you should start seeing data in Callcounter. Note that this might take some time
|
25
|
+
because this gems only sends data every few requests or every few minutes.
|
26
|
+
|
27
|
+
## Configure what to capture
|
28
|
+
|
29
|
+
You can add a lambda that will be called for every request to determine whether it was a request to your API.
|
30
|
+
For example, the default lambda is shown below:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Callcounter.configure do |config|
|
34
|
+
config.project_token = '' # TODO: fill with your unique project token
|
35
|
+
config.track = lambda { |request|
|
36
|
+
return request.hostname.start_with?('api') || request.path.start_with?('/api')
|
37
|
+
}
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## About Callcounter
|
42
|
+
|
43
|
+
[Callcounter](https://callcounter.eu) is a service built by [Webindie](https://webindie.nl) that
|
44
|
+
helps API providers with debugging and optimising the usage of their APIs.
|
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.eu
|
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
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Callcounter
|
4
|
+
# class that captures the rack requests and sends them to callcounter.eu
|
5
|
+
class Capturer
|
6
|
+
attr_accessor :buffer
|
7
|
+
|
8
|
+
def initialize(app)
|
9
|
+
@app = app
|
10
|
+
@buffer = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
start = Time.now
|
15
|
+
result = @app.call(env)
|
16
|
+
finish = Time.now
|
17
|
+
|
18
|
+
return result if project_token.nil?
|
19
|
+
|
20
|
+
threading do
|
21
|
+
background_work(start, finish, env, result)
|
22
|
+
end
|
23
|
+
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
def debug?
|
28
|
+
Callcounter.configuration.debug
|
29
|
+
end
|
30
|
+
|
31
|
+
def track?(request)
|
32
|
+
Callcounter.configuration.track.call(request)
|
33
|
+
end
|
34
|
+
|
35
|
+
def project_token
|
36
|
+
Callcounter.configuration.project_token
|
37
|
+
end
|
38
|
+
|
39
|
+
def async?
|
40
|
+
Callcounter.configuration.async
|
41
|
+
end
|
42
|
+
|
43
|
+
def threading(&block)
|
44
|
+
if async?
|
45
|
+
Thread.new(&block)
|
46
|
+
else
|
47
|
+
yield
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def send_buffer
|
52
|
+
http = Net::HTTP.new('api.callcounter.eu', 443)
|
53
|
+
http.use_ssl = true
|
54
|
+
track = Net::HTTP::Post.new('/api/v1/events')
|
55
|
+
track['content-type'] = 'application/json'
|
56
|
+
track['user-agent'] = "callcounter-gem (#{Callcounter::VERSION})"
|
57
|
+
track.body = { project_token: project_token, events: @buffer }.to_json
|
58
|
+
|
59
|
+
http.request(track)
|
60
|
+
|
61
|
+
puts 'sent request' if debug?
|
62
|
+
rescue StandardError
|
63
|
+
puts 'failed to send request' if debug?
|
64
|
+
end
|
65
|
+
|
66
|
+
def should_send_buffer?
|
67
|
+
@buffer.first[:created_at] < Time.now - Random.rand(300..359) || @buffer.size > 25
|
68
|
+
end
|
69
|
+
|
70
|
+
def event_attributes(start, finish, request, result)
|
71
|
+
{
|
72
|
+
created_at: start,
|
73
|
+
elapsed_time: ((finish - start) * 1000).round,
|
74
|
+
method: request.request_method,
|
75
|
+
path: request.path,
|
76
|
+
user_agent: request.user_agent,
|
77
|
+
status: result.first
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def background_work(start, finish, env, result)
|
82
|
+
request = Rack::Request.new(env)
|
83
|
+
return unless track?(request)
|
84
|
+
|
85
|
+
@buffer << event_attributes(start, finish, request, result)
|
86
|
+
|
87
|
+
return unless should_send_buffer?
|
88
|
+
|
89
|
+
send_buffer
|
90
|
+
@buffer = []
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,17 @@
|
|
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
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@debug = false
|
10
|
+
@project_token = nil
|
11
|
+
@track = lambda { |request|
|
12
|
+
return request.hostname.start_with?('api') || request.path.start_with?('/api')
|
13
|
+
}
|
14
|
+
@async = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
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
|
metadata
CHANGED
@@ -1,43 +1,51 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callcounter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Webindie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Callcounter is a service that helps API providers with debugging and
|
14
|
+
optimising the usage of their APIs.
|
14
15
|
email: frank@webindie.nl
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
18
19
|
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
19
22
|
- lib/callcounter.rb
|
20
|
-
|
23
|
+
- lib/callcounter/capturer.rb
|
24
|
+
- lib/callcounter/configuration.rb
|
25
|
+
- lib/callcounter/railtie.rb
|
26
|
+
- lib/callcounter/version.rb
|
27
|
+
homepage: https://callcounter.eu
|
21
28
|
licenses:
|
22
29
|
- BSD-2-Clause
|
23
|
-
metadata:
|
30
|
+
metadata:
|
31
|
+
source_code_uri: https://git.sr.ht/~webindie/callcounter-gem
|
24
32
|
post_install_message:
|
25
33
|
rdoc_options: []
|
26
34
|
require_paths:
|
27
35
|
- lib
|
28
36
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
37
|
requirements:
|
30
|
-
- - "
|
38
|
+
- - ">"
|
31
39
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
40
|
+
version: 2.4.0
|
33
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
42
|
requirements:
|
35
43
|
- - ">="
|
36
44
|
- !ruby/object:Gem::Version
|
37
45
|
version: '0'
|
38
46
|
requirements: []
|
39
|
-
rubygems_version: 3.
|
47
|
+
rubygems_version: 3.2.15
|
40
48
|
signing_key:
|
41
49
|
specification_version: 4
|
42
|
-
summary:
|
50
|
+
summary: Callcounter integration gem to gather API request statistics
|
43
51
|
test_files: []
|