b4n1-telemetry 0.1.1
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/EULA.txt +71 -0
- data/LICENSE +96 -0
- data/README.md +49 -0
- data/lib/b4n1_telemetry/client.rb +189 -0
- data/lib/b4n1_telemetry/envelope.rb +68 -0
- data/lib/b4n1_telemetry/transport.rb +60 -0
- data/lib/b4n1_telemetry/version.rb +5 -0
- data/lib/b4n1_telemetry.rb +83 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8f0dc1bdc9c7ad3113c5ec91f9ed6bbd7f37a7dd75cba219a8d847e75eb0962c
|
|
4
|
+
data.tar.gz: 7551b4d66e5d37478bff6d60174ffa07750b8eca326c8d04da291d592e0a2e13
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c7a1e33ca40ead77f8d257f5f9bd7cd7f5ee1decac528da9a55010e1757b68cf3ce2c60074bc5d8083ec2ad54e531c9129e46d13d56b7934820cbaa1fe587040
|
|
7
|
+
data.tar.gz: 29ba86133ce97429aa37098b074de9a256c1d1533bf3d12558979c13fc39f5e8a06752d57c876324867316a17c881bbf0218d15c559db78774339ffe127a087d
|
data/EULA.txt
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
B4N1-TELEMETRY — END USER LICENSE AGREEMENT
|
|
2
|
+
===========================================
|
|
3
|
+
|
|
4
|
+
Version: 1.0 | Effective: 2026-11-21 | Applies to: b4n1-telemetry 0.18.1+ and its SDKs 0.1.0+
|
|
5
|
+
|
|
6
|
+
This End User License Agreement ("EULA") governs your use of the B4N1
|
|
7
|
+
ARTIFACTS of b4n1-telemetry — the collector binary (`telemetry-server`),
|
|
8
|
+
the packaged per-language SDK distributions (`*.whl`, `*.tgz`, `*.nupkg`,
|
|
9
|
+
`*.gem`, `*.jar`, `*.phar`, Go modules) and any bundled documentation.
|
|
10
|
+
|
|
11
|
+
The SDK wrapper SOURCE files are licensed separately under the Business
|
|
12
|
+
Source License 1.1 — see the `LICENSE` file that travels with this one and
|
|
13
|
+
each registry's license metadata.
|
|
14
|
+
|
|
15
|
+
1. DEFINITIONS
|
|
16
|
+
"Artifacts" means the collector binary and the packaged SDK distributions
|
|
17
|
+
described above.
|
|
18
|
+
"SDK" means the BUSL-1.1 licensed wrapper source distributed per language.
|
|
19
|
+
"Source" means the Rust implementation of the collector, its build
|
|
20
|
+
scripts, internal tests and internal documentation. Source is NOT
|
|
21
|
+
distributed under this EULA and is not distributed to you at all.
|
|
22
|
+
|
|
23
|
+
2. LICENSE GRANT
|
|
24
|
+
Subject to your compliance with this EULA and with any commercial
|
|
25
|
+
agreement between you and the Licensor, B4N1 grants you a non-exclusive,
|
|
26
|
+
non-transferable, revocable licence to install and use the Artifacts,
|
|
27
|
+
in object form, for your own internal business purposes, including
|
|
28
|
+
running the collector inside your own infrastructure.
|
|
29
|
+
|
|
30
|
+
3. RESTRICTIONS
|
|
31
|
+
You may not:
|
|
32
|
+
(a) reverse engineer, decompile, disassemble or otherwise attempt to
|
|
33
|
+
derive the Source or the algorithms of the Artifacts, except to the
|
|
34
|
+
extent such restriction is prohibited by applicable law;
|
|
35
|
+
(b) redistribute, sublicense, sell, rent, lease or lend the Artifacts,
|
|
36
|
+
or provide them as a managed service to third parties, without a
|
|
37
|
+
written commercial agreement with the Licensor;
|
|
38
|
+
(c) remove or alter any copyright, licence or proprietary notice in the
|
|
39
|
+
Artifacts;
|
|
40
|
+
(d) use the Artifacts to build a competing product.
|
|
41
|
+
|
|
42
|
+
4. DATA AND PRIVACY
|
|
43
|
+
The collector stores what your applications send it, on infrastructure you
|
|
44
|
+
control. It does not transmit your events to B4N1. You are the controller
|
|
45
|
+
of that data and are responsible for what you collect and for how long you
|
|
46
|
+
retain it: the collector ships retention controls precisely so you can
|
|
47
|
+
delete what you must.
|
|
48
|
+
|
|
49
|
+
5. NO WARRANTY
|
|
50
|
+
THE ARTIFACTS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
51
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
52
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
|
53
|
+
|
|
54
|
+
6. LIMITATION OF LIABILITY
|
|
55
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
56
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
|
|
57
|
+
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ARTIFACTS OR
|
|
58
|
+
THE USE OR OTHER DEALINGS IN THE ARTIFACTS.
|
|
59
|
+
|
|
60
|
+
7. TERMINATION
|
|
61
|
+
This EULA terminates automatically if you breach it. On termination you
|
|
62
|
+
must stop using the Artifacts and delete all copies.
|
|
63
|
+
|
|
64
|
+
8. THIRD-PARTY COMPONENTS
|
|
65
|
+
The Artifacts include third-party components licensed under their own
|
|
66
|
+
terms. The full list, with copyright notices and licence texts, is in
|
|
67
|
+
`THIRD-PARTY-NOTICES.txt`, which may be printed from the binary itself
|
|
68
|
+
with `--licenses`.
|
|
69
|
+
|
|
70
|
+
9. CONTACT
|
|
71
|
+
B4N1 — b4n1@b4n1.com — https://b4n1.com
|
data/LICENSE
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
4
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
5
|
+
|
|
6
|
+
-----------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
Parameters
|
|
9
|
+
|
|
10
|
+
Licensor: B4N1 (b4n1.com)
|
|
11
|
+
Licensed Work: b4n1-telemetry SDK — the per-language wrapper source
|
|
12
|
+
(Python, JavaScript, Go, Ruby, PHP, Java, C#) that sends
|
|
13
|
+
events to a b4n1-telemetry collector.
|
|
14
|
+
Additional Use Grant: You may use the Licensed Work in production, free of
|
|
15
|
+
charge, provided that your organization's total annual
|
|
16
|
+
gross revenue does not exceed USD $100,000. Organizations
|
|
17
|
+
whose annual gross revenue exceeds USD $100,000 must
|
|
18
|
+
obtain a commercial license from the Licensor before any
|
|
19
|
+
production use. You may not resell or provide the
|
|
20
|
+
Licensed Work as a managed commercial service without
|
|
21
|
+
authorization from the Licensor.
|
|
22
|
+
Change Date: Four years after the first publicly available
|
|
23
|
+
distribution of the Licensed Work under this License.
|
|
24
|
+
Change License: Apache License, Version 2.0
|
|
25
|
+
|
|
26
|
+
For information about alternative licensing arrangements for the Licensed
|
|
27
|
+
Work, please contact b4n1@b4n1.com.
|
|
28
|
+
|
|
29
|
+
-----------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
Terms
|
|
32
|
+
|
|
33
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
34
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
35
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
36
|
+
production use.
|
|
37
|
+
|
|
38
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
39
|
+
available distribution of a specific version of the Licensed Work under this
|
|
40
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
41
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
42
|
+
above terminate.
|
|
43
|
+
|
|
44
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
45
|
+
currently in effect as described in this License, you must purchase a
|
|
46
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
47
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
48
|
+
|
|
49
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
50
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
51
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
52
|
+
for each version of the Licensed Work released by Licensor.
|
|
53
|
+
|
|
54
|
+
You must conspicuously display this License on each original or modified copy
|
|
55
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
56
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
57
|
+
License apply to your use of that work.
|
|
58
|
+
|
|
59
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
60
|
+
terminate your rights under this License for the current and all other
|
|
61
|
+
versions of the Licensed Work.
|
|
62
|
+
|
|
63
|
+
This License does not grant you any right in any trademark or logo of
|
|
64
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
65
|
+
Licensor as expressly required by this License).
|
|
66
|
+
|
|
67
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
68
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
69
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
70
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
71
|
+
TITLE. MariaDB hereby grants you permission to use this License's text to
|
|
72
|
+
license your works, and to refer to it using the trademark "Business Source
|
|
73
|
+
License", as long as you comply with the Covenants of Licensor below.
|
|
74
|
+
|
|
75
|
+
Covenants of Licensor
|
|
76
|
+
|
|
77
|
+
In consideration of the right to use this License's text and the "Business
|
|
78
|
+
Source License" name and trademark, Licensor covenants to MariaDB, and to all
|
|
79
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
80
|
+
|
|
81
|
+
To specify as the Change License a license that is compatible with GPL
|
|
82
|
+
Version 2.0 or a later version, where "compatible" means that software
|
|
83
|
+
provided under the Change License can be included in a program with software
|
|
84
|
+
provided under GPL Version 2.0 or a later version. Licensor may specify
|
|
85
|
+
additional Change Licenses without limitation.
|
|
86
|
+
|
|
87
|
+
To either: (a) specify an additional grant of rights to use that does not
|
|
88
|
+
impose any additional restriction on the right granted in this License, as
|
|
89
|
+
the Additional Use Grant; or (b) insert the text "None" to specify a Change
|
|
90
|
+
License. Not to modify this License in any other way.
|
|
91
|
+
|
|
92
|
+
Notice
|
|
93
|
+
|
|
94
|
+
The Business Source License (this document, or the "License") is not an Open
|
|
95
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
96
|
+
under an Open Source License, as stated in this License.
|
data/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# b4n1-telemetry for Ruby
|
|
2
|
+
|
|
3
|
+
Drop-in error reporting to your own collector. No gems, no native extensions:
|
|
4
|
+
the SDK is Ruby's standard library.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
gem install b4n1-telemetry
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
require "b4n1_telemetry"
|
|
14
|
+
|
|
15
|
+
B4N1Telemetry.init(app: "api")
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
That is the whole integration — an `at_exit` hook flushes whatever is still
|
|
19
|
+
queued, so a process that ends normally does not lose its last events.
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
B4N1Telemetry.capture("payment failed", level: "error", component: "billing", data: { "order" => "42" })
|
|
23
|
+
B4N1Telemetry.capture_exception(error, component: "billing")
|
|
24
|
+
B4N1Telemetry.correlation_id = "req-7" # stamped on every event of the request
|
|
25
|
+
B4N1Telemetry.flush(2.0)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`component:` is the wire's `module` field. Ruby's `module` is a keyword, so the
|
|
29
|
+
option cannot be called that without losing the ability to pass it as a hash
|
|
30
|
+
key — the mapping is done in `envelope.rb`.
|
|
31
|
+
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
| Variable | Meaning |
|
|
35
|
+
|---|---|
|
|
36
|
+
| `B4N1_TELEMETRY_URL` | Collector base URL (default `http://127.0.0.1:9450`) |
|
|
37
|
+
| `B4N1_TELEMETRY_TOKEN` | Bearer token, when the collector requires one |
|
|
38
|
+
| `B4N1_TELEMETRY_APP` | Application name (default `ruby`) |
|
|
39
|
+
| `B4N1_TELEMETRY_RELEASE` | Deploy id, stamped on every event |
|
|
40
|
+
|
|
41
|
+
## What it does not do
|
|
42
|
+
|
|
43
|
+
It never raises into your application, never blocks a request on the network
|
|
44
|
+
for longer than the given timeout, and never hides a loss: a full queue drops
|
|
45
|
+
and counts, a `429` pauses for `Retry-After` and keeps the batch.
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
Business Source License 1.1 — see `LICENSE`.
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
require_relative "envelope"
|
|
7
|
+
require_relative "transport"
|
|
8
|
+
|
|
9
|
+
module B4N1Telemetry
|
|
10
|
+
# Bounded queue drained by a background sender.
|
|
11
|
+
#
|
|
12
|
+
# Sending is best-effort by design: an observability client that raises or
|
|
13
|
+
# blocks inside the application turns a bug report into an outage, so every
|
|
14
|
+
# failure lands in #stats instead. The one failure that is not merely counted
|
|
15
|
+
# is a 429: the batch goes back to the head of the queue and sending pauses
|
|
16
|
+
# for Retry-After, because retrying at once would earn another 429 and
|
|
17
|
+
# dropping it would make the collector's quota look like lost telemetry.
|
|
18
|
+
class Client
|
|
19
|
+
attr_reader :url, :app, :release
|
|
20
|
+
|
|
21
|
+
def initialize(url: nil, token: nil, app: nil, release: nil, queue_size: 2048,
|
|
22
|
+
batch_size: 100, flush_interval: 0.25, timeout: 3.0, on_error: nil)
|
|
23
|
+
@url = (url || ENV["B4N1_TELEMETRY_URL"] || Envelope::DEFAULT_URL).sub(%r{/+\z}, "")
|
|
24
|
+
@token = token || ENV["B4N1_TELEMETRY_TOKEN"]
|
|
25
|
+
@app = app || ENV.fetch("B4N1_TELEMETRY_APP", "ruby")
|
|
26
|
+
@release = release || ENV["B4N1_TELEMETRY_RELEASE"]
|
|
27
|
+
@queue_size = queue_size
|
|
28
|
+
@batch_size = [batch_size, 1].max
|
|
29
|
+
@flush_interval = [flush_interval, 0.01].max
|
|
30
|
+
@timeout = timeout
|
|
31
|
+
@on_error = on_error
|
|
32
|
+
|
|
33
|
+
@queue = []
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@sent = 0
|
|
36
|
+
@failed = 0
|
|
37
|
+
@dropped = 0
|
|
38
|
+
@throttled = 0
|
|
39
|
+
@inflight = 0
|
|
40
|
+
@last_error = nil
|
|
41
|
+
@paused_until = 0.0
|
|
42
|
+
@closed = false
|
|
43
|
+
@transport = Transport.new(@url, @token, @timeout)
|
|
44
|
+
@sender = Thread.new { sender_loop }
|
|
45
|
+
@sender.abort_on_exception = false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Enqueue one event. Returns false when the queue is full (counted).
|
|
49
|
+
def send_event(level: "error", message: "", component: "", data: nil,
|
|
50
|
+
backtrace: nil, correlation_id: nil, breadcrumbs: nil)
|
|
51
|
+
enqueue(Envelope.build(app: @app, level: level, message: message, component: component,
|
|
52
|
+
data: data, backtrace: backtrace,
|
|
53
|
+
correlation_id: correlation_id,
|
|
54
|
+
breadcrumbs: breadcrumbs, release: @release))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Report an exception at level "error", with its real backtrace.
|
|
58
|
+
def capture_exception(error, component: "")
|
|
59
|
+
return false if error.nil?
|
|
60
|
+
|
|
61
|
+
enqueue(Envelope.build(app: @app, level: "error",
|
|
62
|
+
message: Envelope.describe(error), component: component,
|
|
63
|
+
data: { "error_type" => error.class.to_s },
|
|
64
|
+
backtrace: Envelope.backtrace_lines(error),
|
|
65
|
+
release: @release))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def enqueue(envelope)
|
|
69
|
+
should_flush = false
|
|
70
|
+
@mutex.synchronize do
|
|
71
|
+
return false if @closed
|
|
72
|
+
if @queue.length >= @queue_size
|
|
73
|
+
@dropped += 1
|
|
74
|
+
return false
|
|
75
|
+
end
|
|
76
|
+
@queue << envelope
|
|
77
|
+
should_flush = @queue.length >= @batch_size
|
|
78
|
+
end
|
|
79
|
+
flush(2.0) if should_flush
|
|
80
|
+
true
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Resolves true when everything queued has been sent (or dropped) within
|
|
84
|
+
# `timeout`. An event already in flight counts as pending: reporting
|
|
85
|
+
# "flushed" while the POST is open is how a caller loses data.
|
|
86
|
+
def flush(timeout = 2.0)
|
|
87
|
+
deadline = monotonic + timeout
|
|
88
|
+
loop do
|
|
89
|
+
batch = take
|
|
90
|
+
post(batch) if batch
|
|
91
|
+
return true if pending.zero?
|
|
92
|
+
|
|
93
|
+
remaining = deadline - monotonic
|
|
94
|
+
return false if remaining <= 0
|
|
95
|
+
|
|
96
|
+
pause = @paused_until - monotonic
|
|
97
|
+
sleep([pause.positive? ? pause : 0.01, remaining].min)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def shutdown(timeout = 2.0)
|
|
102
|
+
flush(timeout)
|
|
103
|
+
@mutex.synchronize { @closed = true }
|
|
104
|
+
@sender.kill
|
|
105
|
+
@sender.join(1)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def stats
|
|
109
|
+
@mutex.synchronize do
|
|
110
|
+
{ url: @url, sent: @sent, failed: @failed, dropped: @dropped,
|
|
111
|
+
throttled: @throttled, queued: @queue.length, inflight: @inflight,
|
|
112
|
+
last_error: @last_error }
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
def monotonic
|
|
119
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def pending
|
|
123
|
+
@mutex.synchronize { @queue.length + @inflight }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def sender_loop
|
|
127
|
+
loop do
|
|
128
|
+
sleep @flush_interval
|
|
129
|
+
flush(2.0) unless @closed
|
|
130
|
+
end
|
|
131
|
+
rescue StandardError
|
|
132
|
+
nil # the sender thread must never take the application down
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def take
|
|
136
|
+
@mutex.synchronize do
|
|
137
|
+
return nil if @queue.empty? || @closed || monotonic < @paused_until
|
|
138
|
+
|
|
139
|
+
batch = @queue.shift(@batch_size)
|
|
140
|
+
@inflight += batch.length
|
|
141
|
+
batch
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def post(batch)
|
|
146
|
+
settle(batch, @transport.post(batch))
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def settle(batch, result)
|
|
150
|
+
status = result.status
|
|
151
|
+
@mutex.synchronize do
|
|
152
|
+
@inflight -= batch.length
|
|
153
|
+
if result.error
|
|
154
|
+
@failed += 1
|
|
155
|
+
@last_error = result.error
|
|
156
|
+
elsif status.between?(200, 299)
|
|
157
|
+
@sent += 1
|
|
158
|
+
elsif status == 429
|
|
159
|
+
@throttled += 1
|
|
160
|
+
seconds = (result.retry_after || "1").to_i
|
|
161
|
+
seconds = 1 unless seconds.positive?
|
|
162
|
+
@paused_until = monotonic + seconds
|
|
163
|
+
@last_error = "HTTP 429 (retry in #{seconds}s)"
|
|
164
|
+
requeue(batch)
|
|
165
|
+
else
|
|
166
|
+
@failed += 1
|
|
167
|
+
@last_error = "HTTP #{status}"
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
notify(@last_error) if result.error || !status.between?(200, 299)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# The refused batch is the oldest, so it goes back FIRST, then whatever
|
|
174
|
+
# still fits. What does not fit is counted, never hidden.
|
|
175
|
+
def requeue(batch)
|
|
176
|
+
room = [@queue_size - batch.length, 0].max
|
|
177
|
+
kept = @queue.slice!(0, room)
|
|
178
|
+
@dropped += @queue.length
|
|
179
|
+
@queue.clear
|
|
180
|
+
@queue.concat(batch).concat(kept)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def notify(message)
|
|
184
|
+
@on_error&.call(message)
|
|
185
|
+
rescue StandardError
|
|
186
|
+
nil # an error hook must never break the sender
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module B4N1Telemetry
|
|
7
|
+
# Wire envelope, field for field the collector's TelemetryEvent.
|
|
8
|
+
#
|
|
9
|
+
# A half-populated event must never reach the wire, so every optional field
|
|
10
|
+
# gets its defined zero value here (nil correlation_id, empty lists) instead
|
|
11
|
+
# of being left out.
|
|
12
|
+
module Envelope
|
|
13
|
+
VERSION = 1
|
|
14
|
+
DEFAULT_URL = "http://127.0.0.1:9450"
|
|
15
|
+
LEVELS = %w[debug info warning error fatal].freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# RFC 3339 UTC with milliseconds — the form the collector parses.
|
|
20
|
+
def now_iso
|
|
21
|
+
Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# `component` is the collector's `module` field. It is named differently
|
|
25
|
+
# here because `module` is a Ruby keyword and cannot be a parameter name;
|
|
26
|
+
# the wire keeps the collector's spelling so every language agrees.
|
|
27
|
+
def build(app:, level: "error", message: "", component: "", data: nil,
|
|
28
|
+
backtrace: nil, correlation_id: nil, breadcrumbs: nil,
|
|
29
|
+
timestamp: nil, release: nil)
|
|
30
|
+
level = level.to_s
|
|
31
|
+
raise ArgumentError, "unknown level #{level.inspect}" unless LEVELS.include?(level)
|
|
32
|
+
|
|
33
|
+
event = {
|
|
34
|
+
"timestamp" => timestamp || now_iso,
|
|
35
|
+
"app" => app.to_s,
|
|
36
|
+
"level" => level,
|
|
37
|
+
"message" => message.to_s,
|
|
38
|
+
"module" => component.to_s,
|
|
39
|
+
"data" => stringify(data),
|
|
40
|
+
"backtrace" => Array(backtrace),
|
|
41
|
+
"correlation_id" => correlation_id,
|
|
42
|
+
"breadcrumbs" => Array(breadcrumbs)
|
|
43
|
+
}
|
|
44
|
+
# Omitted when unset so an event from an older producer stays valid.
|
|
45
|
+
event["release"] = release unless release.nil? || release.to_s.empty?
|
|
46
|
+
{ "v" => VERSION, "event" => event }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The wire carries strings only: a numeric 2 and a string "2" must group
|
|
50
|
+
# together on the dashboard instead of splitting one issue in two.
|
|
51
|
+
def stringify(data)
|
|
52
|
+
(data || {}).each_with_object({}) do |(key, value), out|
|
|
53
|
+
out[key.to_s] = value.to_s
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# `"RuntimeError: boom"` — the message every other SDK uses for the same
|
|
58
|
+
# error, so one collector groups them together.
|
|
59
|
+
def describe(error)
|
|
60
|
+
"#{error.class}: #{error.message}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A Ruby backtrace as one string per frame, like a Python traceback.
|
|
64
|
+
def backtrace_lines(error)
|
|
65
|
+
Array(error.backtrace)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module B4N1Telemetry
|
|
8
|
+
# The POST itself: the only place that knows HTTP exists, and the only place
|
|
9
|
+
# that retries.
|
|
10
|
+
#
|
|
11
|
+
# A missing answer is not a refusal. A timeout against a healthy collector
|
|
12
|
+
# means the collector is fsyncing, and dropping the batch there is silent
|
|
13
|
+
# loss, so the request is attempted once more. A 4xx is a bug in the payload
|
|
14
|
+
# and retrying repeats it; a 429 is the collector asking for time, which the
|
|
15
|
+
# client answers with its requeue path.
|
|
16
|
+
class Transport
|
|
17
|
+
RETRY_PAUSE = 0.1
|
|
18
|
+
|
|
19
|
+
# status is nil when no answer arrived at all; error says why.
|
|
20
|
+
Result = Struct.new(:status, :retry_after, :error, keyword_init: true)
|
|
21
|
+
|
|
22
|
+
def initialize(url, token, timeout)
|
|
23
|
+
@url = url
|
|
24
|
+
@token = token
|
|
25
|
+
@timeout = timeout
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def post(batch)
|
|
29
|
+
attempt = 0
|
|
30
|
+
begin
|
|
31
|
+
attempt += 1
|
|
32
|
+
response = deliver(batch)
|
|
33
|
+
Result.new(status: response.code.to_i,
|
|
34
|
+
retry_after: response["Retry-After"],
|
|
35
|
+
error: nil)
|
|
36
|
+
rescue StandardError => e
|
|
37
|
+
if attempt == 1
|
|
38
|
+
sleep RETRY_PAUSE
|
|
39
|
+
retry
|
|
40
|
+
end
|
|
41
|
+
Result.new(status: nil, retry_after: nil, error: "#{e.class}: #{e.message}")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def deliver(batch)
|
|
48
|
+
uri = URI.parse("#{@url}/api/v1/events")
|
|
49
|
+
request = Net::HTTP::Post.new(uri)
|
|
50
|
+
request["Content-Type"] = "application/json"
|
|
51
|
+
request["Authorization"] = "Bearer #{@token}" if @token
|
|
52
|
+
request.body = JSON.generate(batch)
|
|
53
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
54
|
+
open_timeout: @timeout,
|
|
55
|
+
read_timeout: @timeout) do |http|
|
|
56
|
+
http.request(request)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "b4n1_telemetry/envelope"
|
|
4
|
+
require_relative "b4n1_telemetry/client"
|
|
5
|
+
|
|
6
|
+
# b4n1-telemetry for Ruby: drop-in error reporting to your own collector.
|
|
7
|
+
#
|
|
8
|
+
# require "b4n1_telemetry"
|
|
9
|
+
# B4N1Telemetry.init(app: "api")
|
|
10
|
+
#
|
|
11
|
+
# After that, `capture` and `capture_exception` report without any per-call-site
|
|
12
|
+
# configuration, and an `at_exit` hook flushes whatever is still queued so a
|
|
13
|
+
# process that ends normally does not lose its last events.
|
|
14
|
+
#
|
|
15
|
+
# Standard library only: the SDK adds no dependency to the application.
|
|
16
|
+
# Environment: B4N1_TELEMETRY_URL, B4N1_TELEMETRY_TOKEN, B4N1_TELEMETRY_APP,
|
|
17
|
+
# B4N1_TELEMETRY_RELEASE.
|
|
18
|
+
module B4N1Telemetry
|
|
19
|
+
CORRELATION_KEY = :b4n1_correlation_id
|
|
20
|
+
class << self
|
|
21
|
+
attr_reader :client
|
|
22
|
+
|
|
23
|
+
# Install the package-level client. Idempotent: a second call keeps the
|
|
24
|
+
# first client instead of sending every event twice.
|
|
25
|
+
def init(**options)
|
|
26
|
+
return @client if @client
|
|
27
|
+
|
|
28
|
+
@client = Client.new(**options)
|
|
29
|
+
install_at_exit
|
|
30
|
+
@client
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def capture(message, level: "error", component: "", data: nil, backtrace: nil)
|
|
34
|
+
return false unless @client
|
|
35
|
+
|
|
36
|
+
@client.send_event(level: level, message: message, component: component, data: data,
|
|
37
|
+
backtrace: backtrace, correlation_id: correlation_id)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def capture_exception(error, component: "")
|
|
41
|
+
return false unless @client
|
|
42
|
+
|
|
43
|
+
@client.capture_exception(error, component: component)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def flush(timeout = 2.0)
|
|
47
|
+
return true unless @client
|
|
48
|
+
|
|
49
|
+
@client.flush(timeout)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def shutdown(timeout = 2.0)
|
|
53
|
+
return unless @client
|
|
54
|
+
|
|
55
|
+
@client.shutdown(timeout)
|
|
56
|
+
@client = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Correlation lives on the thread, which is the unit a web worker handles
|
|
60
|
+
# one request in. A module-level variable would leak one request's id into
|
|
61
|
+
# every concurrent request — worse than no correlation at all.
|
|
62
|
+
def with_correlation(id)
|
|
63
|
+
previous = Thread.current[CORRELATION_KEY]
|
|
64
|
+
Thread.current[CORRELATION_KEY] = id
|
|
65
|
+
yield
|
|
66
|
+
ensure
|
|
67
|
+
Thread.current[CORRELATION_KEY] = previous
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def correlation_id
|
|
71
|
+
Thread.current[CORRELATION_KEY]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def install_at_exit
|
|
77
|
+
return if @at_exit_installed
|
|
78
|
+
|
|
79
|
+
@at_exit_installed = true
|
|
80
|
+
at_exit { shutdown }
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: b4n1-telemetry
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- B4N1
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Uncaught exceptions, explicit captures, batching and correlation for
|
|
14
|
+
a self-hosted b4n1-telemetry collector. Standard library only.
|
|
15
|
+
email:
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- EULA.txt
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/b4n1_telemetry.rb
|
|
24
|
+
- lib/b4n1_telemetry/client.rb
|
|
25
|
+
- lib/b4n1_telemetry/envelope.rb
|
|
26
|
+
- lib/b4n1_telemetry/transport.rb
|
|
27
|
+
- lib/b4n1_telemetry/version.rb
|
|
28
|
+
homepage:
|
|
29
|
+
licenses:
|
|
30
|
+
- BUSL-1.1
|
|
31
|
+
metadata:
|
|
32
|
+
rubygems_mfa_required: 'true'
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '2.7'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.3.5
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Drop-in error reporting to your own b4n1-telemetry collector.
|
|
52
|
+
test_files: []
|