apirelio 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/CHANGELOG.md +7 -0
- data/LICENSE +21 -0
- data/README.md +27 -0
- data/lib/apirelio/client.rb +130 -0
- data/lib/apirelio/event.rb +61 -0
- data/lib/apirelio/http_batch_transport.rb +58 -0
- data/lib/apirelio/metadata.rb +44 -0
- data/lib/apirelio/rack/context.rb +25 -0
- data/lib/apirelio/rack/middleware.rb +126 -0
- data/lib/apirelio/route.rb +49 -0
- data/lib/apirelio/version.rb +5 -0
- data/lib/apirelio.rb +13 -0
- metadata +117 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fce9ddd2aa09cd6234e1b0c7b2fb7df415d107ff534a9472dfd7883f06e1c116
|
|
4
|
+
data.tar.gz: b27c9db466b05fb1a54fcd3efdb9f2ddf5850bfd2c52417e2c1a0871dc30d06c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 75345de07ce8a68b25779dc450fa9619f4b238780d46a1e9a206c3c13b7e9e33e8a46fee331e380a0362b01c9c2b046931c8826e5abfcb052f0909aa76892e25
|
|
7
|
+
data.tar.gz: 8a37deb8eb1a66c2fb0e2d0078253f37e670797ed8c683fbb9cd1d5b56b660bfb9a0436abe5af8bd3e82bce8ae883e49fffbb284e23f95f35db1beca05c708cc
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Apirelio
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Apirelio Ruby
|
|
2
|
+
|
|
3
|
+
[Documentation](https://apirelio.com/docs/ruby) · [RubyGems](https://rubygems.org/gems/apirelio) · [Apirelio](https://apirelio.com)
|
|
4
|
+
|
|
5
|
+
Privacy-safe customer-aware API analytics for Ruby and Rack applications.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bundle add apirelio
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
client = Apirelio::Client.new(
|
|
13
|
+
api_key: ENV.fetch("APIRELIO_API_KEY", ""),
|
|
14
|
+
service: "billing-api"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
use Apirelio::Rack::Middleware,
|
|
18
|
+
client: client,
|
|
19
|
+
customer_resolver: ->(env) {
|
|
20
|
+
account = env["current_account"]
|
|
21
|
+
account && { id: account.id.to_s, name: account.name, plan: account.plan }
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The middleware records the final status, duration and normalized route without collecting request or response bodies, query strings, credentials, cookies or client IP addresses. Delivery uses a bounded background queue and never changes the observed response or exception.
|
|
26
|
+
|
|
27
|
+
Use [`apirelio-rails`](https://github.com/pryznar/apirelio-rails) for automatic Rails registration.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apirelio
|
|
4
|
+
class Client
|
|
5
|
+
attr_reader :dropped_count
|
|
6
|
+
|
|
7
|
+
def initialize(api_key:, service:, endpoint: "https://apirelio.com", environment: "production", release: nil,
|
|
8
|
+
enabled: true, batch_size: 100, flush_interval: 5.0, max_queue_size: 10_000,
|
|
9
|
+
timeout: 2.0, max_retries: 2, metadata_keys: [], transport: nil,
|
|
10
|
+
on_error: nil, on_dropped: nil)
|
|
11
|
+
@api_key = api_key.to_s
|
|
12
|
+
@service = service.to_s
|
|
13
|
+
@environment = environment.to_s
|
|
14
|
+
@release = release
|
|
15
|
+
@enabled = enabled
|
|
16
|
+
@batch_size = [[batch_size.to_i, 1].max, 500].min
|
|
17
|
+
@flush_interval = [flush_interval.to_f, 0.1].max
|
|
18
|
+
@max_queue_size = [max_queue_size.to_i, 1].max
|
|
19
|
+
@metadata_keys = metadata_keys.map(&:to_s).freeze
|
|
20
|
+
@transport = transport || HttpBatchTransport.new(endpoint: endpoint, api_key: @api_key, timeout: timeout, max_retries: max_retries)
|
|
21
|
+
@on_error = on_error
|
|
22
|
+
@on_dropped = on_dropped
|
|
23
|
+
@queue = []
|
|
24
|
+
@mutex = Mutex.new
|
|
25
|
+
@flush_mutex = Mutex.new
|
|
26
|
+
@condition = ConditionVariable.new
|
|
27
|
+
@closed = false
|
|
28
|
+
@dropped_count = 0
|
|
29
|
+
@worker = Thread.new { worker_loop }
|
|
30
|
+
@worker.name = "apirelio" if @worker.respond_to?(:name=)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def capture(context)
|
|
34
|
+
return false if @closed || !@enabled || @api_key.empty?
|
|
35
|
+
|
|
36
|
+
event = Event.build(context, service: @service, environment: @environment, release: @release, metadata_keys: @metadata_keys)
|
|
37
|
+
dropped = false
|
|
38
|
+
@mutex.synchronize do
|
|
39
|
+
if @queue.length >= @max_queue_size
|
|
40
|
+
@dropped_count += 1
|
|
41
|
+
dropped = true
|
|
42
|
+
else
|
|
43
|
+
@queue << event
|
|
44
|
+
@condition.signal if @queue.length >= @batch_size
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
if dropped
|
|
48
|
+
report_dropped(event)
|
|
49
|
+
return false
|
|
50
|
+
end
|
|
51
|
+
true
|
|
52
|
+
rescue StandardError => error
|
|
53
|
+
report(error)
|
|
54
|
+
false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def pending_count
|
|
58
|
+
@mutex.synchronize { @queue.length }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def flush
|
|
62
|
+
@flush_mutex.synchronize do
|
|
63
|
+
loop do
|
|
64
|
+
batch = @mutex.synchronize { @queue.shift(@batch_size) }
|
|
65
|
+
return true if batch.empty?
|
|
66
|
+
|
|
67
|
+
begin
|
|
68
|
+
@transport.send(batch)
|
|
69
|
+
rescue StandardError
|
|
70
|
+
dropped_events = []
|
|
71
|
+
@mutex.synchronize do
|
|
72
|
+
@queue.unshift(*batch)
|
|
73
|
+
dropped_events = trim_queue
|
|
74
|
+
end
|
|
75
|
+
dropped_events.each { |event| report_dropped(event) }
|
|
76
|
+
raise
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def shutdown(timeout: 5.0)
|
|
83
|
+
@mutex.synchronize do
|
|
84
|
+
@closed = true
|
|
85
|
+
@condition.broadcast
|
|
86
|
+
end
|
|
87
|
+
@worker.join([timeout.to_f, 0].max)
|
|
88
|
+
flush
|
|
89
|
+
rescue StandardError => error
|
|
90
|
+
report(error)
|
|
91
|
+
false
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def worker_loop
|
|
97
|
+
loop do
|
|
98
|
+
closed = @mutex.synchronize do
|
|
99
|
+
@condition.wait(@mutex, @flush_interval) unless @closed
|
|
100
|
+
@closed
|
|
101
|
+
end
|
|
102
|
+
break if closed
|
|
103
|
+
flush if pending_count.positive?
|
|
104
|
+
rescue StandardError => error
|
|
105
|
+
report(error)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def trim_queue
|
|
110
|
+
dropped_events = []
|
|
111
|
+
while @queue.length > @max_queue_size
|
|
112
|
+
dropped_events << @queue.pop
|
|
113
|
+
@dropped_count += 1
|
|
114
|
+
end
|
|
115
|
+
dropped_events
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def report(error)
|
|
119
|
+
@on_error&.call(error)
|
|
120
|
+
rescue StandardError
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def report_dropped(event)
|
|
125
|
+
@on_dropped&.call(event, @dropped_count)
|
|
126
|
+
rescue StandardError
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Apirelio
|
|
7
|
+
module Event
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def build(context, service:, environment:, release:, metadata_keys:)
|
|
11
|
+
customer = context[:customer] || {}
|
|
12
|
+
application = context[:application] || {}
|
|
13
|
+
{
|
|
14
|
+
event_id: SecureRandom.uuid,
|
|
15
|
+
occurred_at: Time.now.utc.iso8601(3),
|
|
16
|
+
service: text(service, 120) || "ruby-api",
|
|
17
|
+
environment: environment.to_s,
|
|
18
|
+
method: (text(context[:method], 16) || "GET").upcase,
|
|
19
|
+
route: Route.normalize(context[:route]),
|
|
20
|
+
route_name: text(context[:route_name], 255),
|
|
21
|
+
status: integer(context.fetch(:status, 200), 100, 599),
|
|
22
|
+
duration_ms: integer(context.fetch(:duration_ms, 0), 0, 4_294_967_295),
|
|
23
|
+
request_bytes: optional_integer(context[:request_bytes]),
|
|
24
|
+
response_bytes: optional_integer(context[:response_bytes]),
|
|
25
|
+
customer_id: text(value(customer, :id), 255),
|
|
26
|
+
customer_name: text(value(customer, :name), 255),
|
|
27
|
+
customer_plan: text(value(customer, :plan), 120),
|
|
28
|
+
application_id: text(value(application, :id), 255),
|
|
29
|
+
application_name: text(value(application, :name), 255),
|
|
30
|
+
api_version: text(context[:api_version], 120),
|
|
31
|
+
sdk: text(context[:sdk], 120) || "ruby",
|
|
32
|
+
sdk_version: text(context[:sdk_version], 120) || "0.0.0",
|
|
33
|
+
release: text(release, 255),
|
|
34
|
+
error_code: text(context[:error_code], 255),
|
|
35
|
+
metadata: Metadata.sanitize(context[:metadata] || {}, allowed_keys: metadata_keys)
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def value(hash, key)
|
|
40
|
+
hash[key] || hash[key.to_s]
|
|
41
|
+
end
|
|
42
|
+
private_class_method :value
|
|
43
|
+
|
|
44
|
+
def text(value, length)
|
|
45
|
+
string = value.is_a?(String) ? value : value&.to_s
|
|
46
|
+
string && !string.empty? ? string[0, length] : nil
|
|
47
|
+
end
|
|
48
|
+
private_class_method :text
|
|
49
|
+
|
|
50
|
+
def integer(value, minimum, maximum)
|
|
51
|
+
numeric = value.is_a?(Numeric) && (!value.respond_to?(:finite?) || value.finite?) ? value.round : minimum
|
|
52
|
+
[[numeric, minimum].max, maximum].min
|
|
53
|
+
end
|
|
54
|
+
private_class_method :integer
|
|
55
|
+
|
|
56
|
+
def optional_integer(value)
|
|
57
|
+
value.nil? ? nil : integer(value, 0, 4_294_967_295)
|
|
58
|
+
end
|
|
59
|
+
private_class_method :optional_integer
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Apirelio
|
|
8
|
+
class IngestionError < StandardError
|
|
9
|
+
attr_reader :status
|
|
10
|
+
|
|
11
|
+
def initialize(message, status = 0)
|
|
12
|
+
super(message)
|
|
13
|
+
@status = status
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class HttpBatchTransport
|
|
18
|
+
def initialize(endpoint:, api_key:, timeout: 2.0, max_retries: 2)
|
|
19
|
+
@uri = URI.join("#{endpoint.to_s.sub(%r{/+\z}, "")}/", "ingest/v1/events/batch")
|
|
20
|
+
@api_key = api_key
|
|
21
|
+
@timeout = [timeout.to_f, 0.1].max
|
|
22
|
+
@max_retries = [[max_retries.to_i, 0].max, 10].min
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def send(events)
|
|
26
|
+
return if events.empty?
|
|
27
|
+
|
|
28
|
+
request = Net::HTTP::Post.new(@uri)
|
|
29
|
+
request["Accept"] = "application/json"
|
|
30
|
+
request["Authorization"] = "Bearer #{@api_key}"
|
|
31
|
+
request["Content-Type"] = "application/json"
|
|
32
|
+
request["User-Agent"] = "apirelio-ruby/#{VERSION}"
|
|
33
|
+
request.body = JSON.generate(events: events)
|
|
34
|
+
|
|
35
|
+
attempts = 0
|
|
36
|
+
begin
|
|
37
|
+
response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == "https", open_timeout: @timeout, read_timeout: @timeout) do |http|
|
|
38
|
+
http.request(request)
|
|
39
|
+
end
|
|
40
|
+
status = response.code.to_i
|
|
41
|
+
return if status.between?(200, 299)
|
|
42
|
+
|
|
43
|
+
error = IngestionError.new("Apirelio ingestion returned HTTP #{status}.", status)
|
|
44
|
+
raise error if status < 500 && status != 429
|
|
45
|
+
raise error
|
|
46
|
+
rescue StandardError => error
|
|
47
|
+
if error.is_a?(IngestionError) && error.status < 500 && error.status != 429
|
|
48
|
+
raise error
|
|
49
|
+
end
|
|
50
|
+
attempts += 1
|
|
51
|
+
raise error if attempts > @max_retries
|
|
52
|
+
|
|
53
|
+
sleep(0.1 * (2**(attempts - 1)))
|
|
54
|
+
retry
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Apirelio
|
|
6
|
+
module Metadata
|
|
7
|
+
MAX_ITEMS = 20
|
|
8
|
+
MAX_BYTES = 4096
|
|
9
|
+
SENSITIVE = %w[authorization cookie password token secret email ip].freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def sanitize(values, allowed_keys: [])
|
|
14
|
+
safe = {}
|
|
15
|
+
values.to_h.each do |raw_key, raw_value|
|
|
16
|
+
key = raw_key.to_s
|
|
17
|
+
next unless scalar?(raw_value)
|
|
18
|
+
next if sensitive?(key)
|
|
19
|
+
next unless key.start_with?("header.") || key == "exception" || allowed_keys.map(&:to_s).include?(key)
|
|
20
|
+
|
|
21
|
+
key = key[0, 120]
|
|
22
|
+
value = raw_value.is_a?(String) ? raw_value[0, 1000] : raw_value
|
|
23
|
+
candidate = safe.merge(key => value)
|
|
24
|
+
next if JSON.generate(candidate).bytesize > MAX_BYTES
|
|
25
|
+
|
|
26
|
+
safe[key] = value
|
|
27
|
+
break if safe.length >= MAX_ITEMS
|
|
28
|
+
end
|
|
29
|
+
safe
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def scalar?(value)
|
|
33
|
+
value.nil? || value == true || value == false || value.is_a?(String) ||
|
|
34
|
+
(value.is_a?(Numeric) && (!value.respond_to?(:finite?) || value.finite?))
|
|
35
|
+
end
|
|
36
|
+
private_class_method :scalar?
|
|
37
|
+
|
|
38
|
+
def sensitive?(key)
|
|
39
|
+
normalized = key.downcase
|
|
40
|
+
SENSITIVE.any? { |fragment| normalized.include?(fragment) }
|
|
41
|
+
end
|
|
42
|
+
private_class_method :sensitive?
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apirelio
|
|
4
|
+
module Rack
|
|
5
|
+
class Context
|
|
6
|
+
attr_reader :metadata
|
|
7
|
+
attr_accessor :error_code
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@metadata = {}
|
|
11
|
+
@error_code = nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def add_metadata(values)
|
|
15
|
+
@metadata.merge!(values)
|
|
16
|
+
self
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def set_error_code(value)
|
|
20
|
+
@error_code = value&.to_s&.slice(0, 255)
|
|
21
|
+
self
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apirelio
|
|
4
|
+
module Rack
|
|
5
|
+
CONTEXT_KEY = "apirelio.context"
|
|
6
|
+
|
|
7
|
+
class Middleware
|
|
8
|
+
def initialize(app, client:, include_routes: [], exclude_routes: [], customer_resolver: nil,
|
|
9
|
+
application_resolver: nil, error_code_resolver: nil, metadata_resolver: nil,
|
|
10
|
+
route_resolver: nil, api_version_header: "HTTP_X_API_VERSION",
|
|
11
|
+
capture_headers: %w[HTTP_X_SDK_VERSION HTTP_USER_AGENT], sdk: "rack",
|
|
12
|
+
sdk_version: VERSION)
|
|
13
|
+
@app = app
|
|
14
|
+
@client = client
|
|
15
|
+
@include_routes = include_routes
|
|
16
|
+
@exclude_routes = exclude_routes
|
|
17
|
+
@customer_resolver = customer_resolver
|
|
18
|
+
@application_resolver = application_resolver
|
|
19
|
+
@error_code_resolver = error_code_resolver
|
|
20
|
+
@metadata_resolver = metadata_resolver
|
|
21
|
+
@route_resolver = route_resolver
|
|
22
|
+
@api_version_header = api_version_header
|
|
23
|
+
@capture_headers = capture_headers
|
|
24
|
+
@sdk = sdk
|
|
25
|
+
@sdk_version = sdk_version
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call(env)
|
|
29
|
+
started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
30
|
+
env[CONTEXT_KEY] = Context.new
|
|
31
|
+
status, headers, body = @app.call(env)
|
|
32
|
+
capture(env, status, headers, nil, started_at)
|
|
33
|
+
[status, headers, body]
|
|
34
|
+
rescue StandardError => error
|
|
35
|
+
capture(env, 500, {}, error, started_at)
|
|
36
|
+
raise
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def capture(env, status, headers, error, started_at)
|
|
42
|
+
route = resolved_route(env)
|
|
43
|
+
return unless Route.capture?(route, include_routes: @include_routes, exclude_routes: @exclude_routes)
|
|
44
|
+
|
|
45
|
+
context = env[CONTEXT_KEY]
|
|
46
|
+
metadata = captured_headers(env)
|
|
47
|
+
metadata.merge!(resolve(@metadata_resolver, env) || {})
|
|
48
|
+
metadata.merge!(context.metadata) if context.is_a?(Context)
|
|
49
|
+
@client.capture(
|
|
50
|
+
method: env["REQUEST_METHOD"] || "UNKNOWN",
|
|
51
|
+
route: route,
|
|
52
|
+
route_name: route_name(env),
|
|
53
|
+
status: status.to_i,
|
|
54
|
+
duration_ms: (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000,
|
|
55
|
+
request_bytes: integer(env["CONTENT_LENGTH"]),
|
|
56
|
+
response_bytes: integer(header(headers, "content-length")),
|
|
57
|
+
customer: resolve(@customer_resolver, env),
|
|
58
|
+
application: resolve(@application_resolver, env),
|
|
59
|
+
api_version: env[@api_version_header],
|
|
60
|
+
sdk: @sdk,
|
|
61
|
+
sdk_version: @sdk_version,
|
|
62
|
+
error_code: error_code(env, error, status, context),
|
|
63
|
+
metadata: metadata
|
|
64
|
+
)
|
|
65
|
+
rescue StandardError
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def resolved_route(env)
|
|
70
|
+
value = resolve(@route_resolver, env) || env["action_dispatch.route_uri_pattern"] || env["sinatra.route"] || env["PATH_INFO"] || "/"
|
|
71
|
+
template = value.to_s.sub(/\(\.:format\)\z/, "").gsub(/:([a-zA-Z_][a-zA-Z0-9_]*)/, "{\\1}")
|
|
72
|
+
Route.normalize(template)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def route_name(env)
|
|
76
|
+
parameters = env["action_dispatch.request.path_parameters"]
|
|
77
|
+
return nil unless parameters.respond_to?(:[])
|
|
78
|
+
|
|
79
|
+
controller = parameters[:controller] || parameters["controller"]
|
|
80
|
+
action = parameters[:action] || parameters["action"]
|
|
81
|
+
[controller, action].compact.join("#").then { |value| value.empty? ? nil : value }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def error_code(env, error, status, context)
|
|
85
|
+
return context.error_code if context.is_a?(Context) && context.error_code
|
|
86
|
+
|
|
87
|
+
resolved = @error_code_resolver&.call(env, error, status)
|
|
88
|
+
resolved ? resolved.to_s[0, 255] : error&.class&.name&.slice(0, 255)
|
|
89
|
+
rescue StandardError
|
|
90
|
+
error&.class&.name&.slice(0, 255)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def resolve(resolver, env)
|
|
94
|
+
resolver&.call(env)
|
|
95
|
+
rescue StandardError
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def captured_headers(env)
|
|
100
|
+
@capture_headers.each_with_object({}) do |name, values|
|
|
101
|
+
value = env[name]
|
|
102
|
+
next if value.nil?
|
|
103
|
+
|
|
104
|
+
label = name.to_s.sub(/\AHTTP_/, "").downcase.tr("_", "-")
|
|
105
|
+
values["header.#{label}"] = value.to_s
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def header(headers, name)
|
|
110
|
+
pair = headers.to_h.find { |key, _| key.to_s.downcase == name }
|
|
111
|
+
pair&.last
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def integer(value)
|
|
115
|
+
Integer(value, exception: false)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
module_function
|
|
120
|
+
|
|
121
|
+
def context(env)
|
|
122
|
+
value = env[CONTEXT_KEY]
|
|
123
|
+
value if value.is_a?(Context)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apirelio
|
|
4
|
+
module Route
|
|
5
|
+
UUID = /\A[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i
|
|
6
|
+
ULID = /\A[0-9A-HJKMNP-TV-Z]{26}\z/i
|
|
7
|
+
INTEGER = /\A\d+\z/
|
|
8
|
+
HEX_IDENTIFIER = /\A[0-9a-f]{16,}\z/i
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def normalize(value)
|
|
13
|
+
path = value.to_s.split(/[?#]/, 2).first.to_s.strip
|
|
14
|
+
path = "/" if path.empty?
|
|
15
|
+
path = "/#{path}" unless path.start_with?("/")
|
|
16
|
+
normalized = path.split("/", -1).map { |segment| identifier?(segment) ? "{id}" : segment }.join("/")
|
|
17
|
+
normalized[0, 500]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def capture?(value, include_routes: [], exclude_routes: [])
|
|
21
|
+
included = include_routes.empty? || include_routes.any? { |pattern| match?(value, pattern) }
|
|
22
|
+
excluded = exclude_routes.any? { |pattern| match?(value, pattern) }
|
|
23
|
+
included && !excluded
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def match?(value, pattern)
|
|
27
|
+
route = normalize(value)
|
|
28
|
+
expected = normalized_pattern(pattern)
|
|
29
|
+
return route == expected unless expected.end_with?("/**")
|
|
30
|
+
|
|
31
|
+
prefix = expected[0...-3]
|
|
32
|
+
prefix = "/" if prefix.empty?
|
|
33
|
+
route == prefix || route.start_with?(prefix == "/" ? "/" : "#{prefix}/")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def identifier?(value)
|
|
37
|
+
INTEGER.match?(value) || UUID.match?(value) || ULID.match?(value) || HEX_IDENTIFIER.match?(value)
|
|
38
|
+
end
|
|
39
|
+
private_class_method :identifier?
|
|
40
|
+
|
|
41
|
+
def normalized_pattern(value)
|
|
42
|
+
path = value.to_s.split(/[?#]/, 2).first.to_s.strip
|
|
43
|
+
path = "/" if path.empty?
|
|
44
|
+
path = "/#{path}" unless path.start_with?("/")
|
|
45
|
+
path.length > 1 && path.end_with?("/") ? path[0...-1] : path
|
|
46
|
+
end
|
|
47
|
+
private_class_method :normalized_pattern
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/apirelio.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "apirelio/version"
|
|
4
|
+
require_relative "apirelio/route"
|
|
5
|
+
require_relative "apirelio/metadata"
|
|
6
|
+
require_relative "apirelio/event"
|
|
7
|
+
require_relative "apirelio/http_batch_transport"
|
|
8
|
+
require_relative "apirelio/client"
|
|
9
|
+
require_relative "apirelio/rack/context"
|
|
10
|
+
require_relative "apirelio/rack/middleware"
|
|
11
|
+
|
|
12
|
+
module Apirelio
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: apirelio
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Apirelio
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rack
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.2'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '4'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '2.2'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '4'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: minitest
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '5.20'
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '6'
|
|
42
|
+
type: :development
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '5.20'
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '6'
|
|
52
|
+
- !ruby/object:Gem::Dependency
|
|
53
|
+
name: rake
|
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '13.1'
|
|
59
|
+
- - "<"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '14'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '13.1'
|
|
69
|
+
- - "<"
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '14'
|
|
72
|
+
description: Fail-safe, privacy-safe event delivery and Rack middleware for Apirelio.
|
|
73
|
+
email:
|
|
74
|
+
- info@apirelio.com
|
|
75
|
+
executables: []
|
|
76
|
+
extensions: []
|
|
77
|
+
extra_rdoc_files: []
|
|
78
|
+
files:
|
|
79
|
+
- CHANGELOG.md
|
|
80
|
+
- LICENSE
|
|
81
|
+
- README.md
|
|
82
|
+
- lib/apirelio.rb
|
|
83
|
+
- lib/apirelio/client.rb
|
|
84
|
+
- lib/apirelio/event.rb
|
|
85
|
+
- lib/apirelio/http_batch_transport.rb
|
|
86
|
+
- lib/apirelio/metadata.rb
|
|
87
|
+
- lib/apirelio/rack/context.rb
|
|
88
|
+
- lib/apirelio/rack/middleware.rb
|
|
89
|
+
- lib/apirelio/route.rb
|
|
90
|
+
- lib/apirelio/version.rb
|
|
91
|
+
homepage: https://apirelio.com
|
|
92
|
+
licenses:
|
|
93
|
+
- MIT
|
|
94
|
+
metadata:
|
|
95
|
+
homepage_uri: https://apirelio.com
|
|
96
|
+
source_code_uri: https://github.com/pryznar/apirelio-ruby
|
|
97
|
+
changelog_uri: https://github.com/pryznar/apirelio-ruby/blob/main/CHANGELOG.md
|
|
98
|
+
documentation_uri: https://apirelio.com/docs/ruby
|
|
99
|
+
rubygems_mfa_required: 'true'
|
|
100
|
+
rdoc_options: []
|
|
101
|
+
require_paths:
|
|
102
|
+
- lib
|
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '3.1'
|
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: '0'
|
|
113
|
+
requirements: []
|
|
114
|
+
rubygems_version: 4.0.16
|
|
115
|
+
specification_version: 4
|
|
116
|
+
summary: Customer-aware API analytics for Ruby and Rack applications.
|
|
117
|
+
test_files: []
|