instatus-otel 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/LICENSE +21 -0
- data/README.md +257 -0
- data/lib/instatus/otel/attributes.rb +155 -0
- data/lib/instatus/otel/client.rb +108 -0
- data/lib/instatus/otel/configuration.rb +32 -0
- data/lib/instatus/otel/dedupe.rb +41 -0
- data/lib/instatus/otel/event.rb +51 -0
- data/lib/instatus/otel/exports.rb +59 -0
- data/lib/instatus/otel/logger.rb +77 -0
- data/lib/instatus/otel/providers.rb +135 -0
- data/lib/instatus/otel/rack.rb +43 -0
- data/lib/instatus/otel/rails.rb +15 -0
- data/lib/instatus/otel/rails_error_subscriber.rb +21 -0
- data/lib/instatus/otel/rate_cap.rb +30 -0
- data/lib/instatus/otel/resource.rb +50 -0
- data/lib/instatus/otel/scope.rb +92 -0
- data/lib/instatus/otel/scrub.rb +85 -0
- data/lib/instatus/otel/version.rb +8 -0
- data/lib/instatus/otel.rb +184 -0
- metadata +177 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7365979821078cf9991d59ecf8a3687d3bae4bad7d19c9ebde92cde52fbacaaa
|
|
4
|
+
data.tar.gz: cb610cc2f30215d38fdd6fe92d60ea76eb89871a26c36ad0219cf9b2e4c2efe0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 75cdcffd57314f6a256ed4b402e77f10b968ed01e080114e7c9e6cf02303603f93de0c3983a0c32360ed7996e98c8e8f34a1a5a43ec7b1c5a494b7d9cc7d0302
|
|
7
|
+
data.tar.gz: dfc8af7da2679c1fca6c4fb623ffb7d97e8fea0d64dd19d80c59a9040f2c896959a96528f683c3ba034dca4a9dc7c070f1f01bdb9edf0559592530bed830b9ef
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Instatus
|
|
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,257 @@
|
|
|
1
|
+
# instatus-otel
|
|
2
|
+
|
|
3
|
+
Error tracking for Ruby, built on OpenTelemetry, with the Sentry SDK's API.
|
|
4
|
+
Errors show up in Instatus Errors with their backtrace, user and release, and
|
|
5
|
+
your traces, logs and metrics go to Instatus alongside them.
|
|
6
|
+
|
|
7
|
+
- **OpenTelemetry underneath.** Errors are OTel log records and span events
|
|
8
|
+
in the semantic conventions (`exception.type`, `exception.message`,
|
|
9
|
+
`exception.stacktrace`). Any OTel instrumentation you add flows through
|
|
10
|
+
it, and the `OTEL_*` environment variables work as usual.
|
|
11
|
+
- **Sentry on top.** `init`, `capture_exception`, `set_user`, `with_scope`
|
|
12
|
+
and the rest take the same arguments as `sentry-ruby`'s, so moving over is
|
|
13
|
+
mostly a rename.
|
|
14
|
+
- **Rails and Rack.** In a Rails app the gem subscribes to `Rails.error` and
|
|
15
|
+
gives each request its own scope. Any other Rack app adds one middleware.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
# Gemfile
|
|
21
|
+
gem "instatus-otel"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Ruby 3.1 or later.
|
|
25
|
+
|
|
26
|
+
## Start
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require "instatus/otel"
|
|
30
|
+
|
|
31
|
+
Instatus.init do |config|
|
|
32
|
+
config.api_key = ENV["INSTATUS_API_KEY"]
|
|
33
|
+
config.site_id = ENV["INSTATUS_SITE_ID"]
|
|
34
|
+
config.release = ENV["GIT_SHA"]
|
|
35
|
+
config.environment = "production"
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
With an API key, errors, traces, logs and metrics go to
|
|
40
|
+
`https://otel.instatus.com` over OTLP/HTTP. `api_key` and `site_id` fall back
|
|
41
|
+
to `INSTATUS_API_KEY` and `INSTATUS_SITE_ID`, so with those set,
|
|
42
|
+
`Instatus.init` alone is enough.
|
|
43
|
+
|
|
44
|
+
Point it anywhere else with `endpoint`, or with the standard
|
|
45
|
+
`OTEL_EXPORTER_OTLP_ENDPOINT` / `OTEL_EXPORTER_OTLP_HEADERS` variables.
|
|
46
|
+
`OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` are read too.
|
|
47
|
+
|
|
48
|
+
An exception that ends the process is reported at `fatal` level, flushed,
|
|
49
|
+
and then Ruby prints it and exits as usual. `exit` and signals such as
|
|
50
|
+
`SIGTERM` aren't reported.
|
|
51
|
+
|
|
52
|
+
### Rails
|
|
53
|
+
|
|
54
|
+
Add the gem and put `Instatus.init` in `config/initializers/instatus.rb`.
|
|
55
|
+
Errors Rails reports, unhandled request and job errors and those you pass to
|
|
56
|
+
`Rails.error.report` or `Rails.error.handle`, reach Instatus with
|
|
57
|
+
`exception.handled` set as Rails says. Unhandled controller errors are
|
|
58
|
+
reported this way from Rails 7.1.
|
|
59
|
+
|
|
60
|
+
Each request runs in its own scope, so `Instatus.set_user` in a controller
|
|
61
|
+
never reaches the next request on the same thread.
|
|
62
|
+
|
|
63
|
+
To send `Rails.logger` to Logs too (Rails 7.1 or later):
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
Rails.logger.broadcast_to(Instatus.logger)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Rack and Sinatra
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
use Instatus::Otel::RackMiddleware
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The middleware gives each request its own scope, adds its URL (with
|
|
76
|
+
secrets filtered out) and reports what the app raises.
|
|
77
|
+
|
|
78
|
+
### Instrumentations
|
|
79
|
+
|
|
80
|
+
Install the OTel instrumentation gems you want, require them, and name them
|
|
81
|
+
in `instrumentations`:
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
require "opentelemetry/instrumentation/all"
|
|
85
|
+
|
|
86
|
+
Instatus.init do |config|
|
|
87
|
+
config.instrumentations = :all
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`instrumentations` takes `:all`, a list of names such as
|
|
92
|
+
`["OpenTelemetry::Instrumentation::Rack"]`, or a hash of names to their
|
|
93
|
+
options, as `OpenTelemetry::SDK.configure`'s `use` does.
|
|
94
|
+
|
|
95
|
+
## Moving from Sentry
|
|
96
|
+
|
|
97
|
+
```diff
|
|
98
|
+
- gem "sentry-ruby"
|
|
99
|
+
- gem "sentry-rails"
|
|
100
|
+
+ gem "instatus-otel"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```diff
|
|
104
|
+
- Sentry.init do |config|
|
|
105
|
+
- config.dsn = ENV["SENTRY_DSN"]
|
|
106
|
+
+ Instatus.init do |config|
|
|
107
|
+
+ config.api_key = ENV["INSTATUS_API_KEY"]
|
|
108
|
+
+ config.site_id = ENV["INSTATUS_SITE_ID"]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then `Sentry.` becomes `Instatus.`. The differences:
|
|
112
|
+
|
|
113
|
+
- `event.exception` in `before_send` is one value with `type`, `value`,
|
|
114
|
+
`stacktrace` (Ruby's backtrace text) and `handled`, not Sentry's list of
|
|
115
|
+
parsed frames. The raised exception is in `hint[:exception]`.
|
|
116
|
+
- Breadcrumbs, session tracking and profiling aren't included.
|
|
117
|
+
- Tags, contexts and extras become attributes: `set_tag("plan", "pro")` is
|
|
118
|
+
`plan`, `set_context("cart", { items: 3 })` is `cart.items`, and
|
|
119
|
+
`set_extra("id", 1)` is `extra.id`.
|
|
120
|
+
- `release` is `service.version`, `environment` is
|
|
121
|
+
`deployment.environment.name`, and the user's id (or email, or username)
|
|
122
|
+
is `enduser.id`.
|
|
123
|
+
|
|
124
|
+
## Options
|
|
125
|
+
|
|
126
|
+
| Option | What it does | Default |
|
|
127
|
+
| ------------------------- | ------------------------------------------------------ | ------------------- |
|
|
128
|
+
| `api_key`, `site_id` | Send every signal to the Instatus collector | `INSTATUS_*` env |
|
|
129
|
+
| `endpoint` | Another OTLP/HTTP endpoint (`/v1/<signal>` is added) | `otel.instatus.com` |
|
|
130
|
+
| `service_name` | `service.name` | `OTEL_SERVICE_NAME`, then `ruby` |
|
|
131
|
+
| `release` | `service.version` | |
|
|
132
|
+
| `environment` | `deployment.environment.name` | |
|
|
133
|
+
| `sample_rate` | Share of errors to send, 0 to 1 | `1` |
|
|
134
|
+
| `traces_sample_rate` | Share of traces to keep, 0 to 1 | `1` |
|
|
135
|
+
| `before_send` | Change an event or return `nil` to drop it | |
|
|
136
|
+
| `excluded_exceptions` | Exception classes (or their names) never reported | `[]` |
|
|
137
|
+
| `max_events_per_minute` | Errors sent per minute | `300` |
|
|
138
|
+
| `instrumentations` | OpenTelemetry instrumentations to install | |
|
|
139
|
+
| `metrics_export_interval` | Seconds between metric exports | `60` |
|
|
140
|
+
| `shutdown_timeout` | Seconds to flush at exit | `2` |
|
|
141
|
+
| `debug` | Print why events are dropped | `false` |
|
|
142
|
+
| `enabled` | `false` keeps the SDK quiet, for tests and local dev | `true` |
|
|
143
|
+
|
|
144
|
+
Every option can also be passed to `init` as a keyword:
|
|
145
|
+
`Instatus.init(release: "1.4.0")`.
|
|
146
|
+
|
|
147
|
+
Every event is checked before it is sent: the same error twice in a row is
|
|
148
|
+
sent once, values under keys like `password`, `token` or `authorization` are
|
|
149
|
+
replaced with `[Filtered]` (in nested hashes and arrays too), card numbers
|
|
150
|
+
are filtered out of messages and backtraces, and URLs lose their
|
|
151
|
+
credentials and secret query parameters. `before_send` runs before these
|
|
152
|
+
filters, so it sees the original values.
|
|
153
|
+
|
|
154
|
+
## Functions
|
|
155
|
+
|
|
156
|
+
### `Instatus.init { |config| }`
|
|
157
|
+
|
|
158
|
+
Starts the SDK. Call it once, as early as you can.
|
|
159
|
+
|
|
160
|
+
### `Instatus.capture_exception(error, **context)`
|
|
161
|
+
|
|
162
|
+
Reports an error you rescued. Returns the event id.
|
|
163
|
+
|
|
164
|
+
```ruby
|
|
165
|
+
begin
|
|
166
|
+
charge_card(order)
|
|
167
|
+
rescue Stripe::CardError => e
|
|
168
|
+
Instatus.capture_exception(e, tags: { step: "charge" })
|
|
169
|
+
end
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`context` takes `tags:`, `extra:`, `user:`, `contexts:` and `level:`, and a
|
|
173
|
+
block gets a copy of the scope for this event only.
|
|
174
|
+
|
|
175
|
+
### `Instatus.capture_message(message, level: :info)`
|
|
176
|
+
|
|
177
|
+
Reports a message. `:error` and `:fatal` messages are issues in Errors;
|
|
178
|
+
lower levels are plain logs.
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
Instatus.capture_message("Stripe webhook signature mismatch", level: :error)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `Instatus.set_user(user)`
|
|
185
|
+
|
|
186
|
+
Sets who is affected. Pass `nil` on sign-out.
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
Instatus.set_user(id: account.id, email: account.email)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `Instatus.set_tag(key, value)` / `Instatus.set_tags(tags)`
|
|
193
|
+
|
|
194
|
+
Adds attributes you can filter by.
|
|
195
|
+
|
|
196
|
+
```ruby
|
|
197
|
+
Instatus.set_tags(plan: "pro", region: "eu")
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### `Instatus.set_context(name, context)`
|
|
201
|
+
|
|
202
|
+
Adds a group of details, flattened into `name.key` attributes.
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
Instatus.set_context("cart", { items: 3, currency: "EUR" })
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### `Instatus.set_extra(key, value)`
|
|
209
|
+
|
|
210
|
+
Adds a detail as `extra.key`.
|
|
211
|
+
|
|
212
|
+
### `Instatus.with_scope { |scope| }`
|
|
213
|
+
|
|
214
|
+
Runs the block with a copy of the scope, so changes stay inside it.
|
|
215
|
+
|
|
216
|
+
```ruby
|
|
217
|
+
Instatus.with_scope do |scope|
|
|
218
|
+
scope.set_tag("job", job.id)
|
|
219
|
+
Instatus.capture_exception(error)
|
|
220
|
+
end
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
The scope lives in the OpenTelemetry context, so it is per fiber. Outside
|
|
224
|
+
`with_scope` each thread gets its own copy of the global scope; the main
|
|
225
|
+
thread uses the global scope itself.
|
|
226
|
+
|
|
227
|
+
### `Instatus.start_span(name, attributes: nil) { |span| }`
|
|
228
|
+
|
|
229
|
+
Runs the block inside an OpenTelemetry span and ends it when the block
|
|
230
|
+
returns. An exception marks the span as failed and is raised again.
|
|
231
|
+
|
|
232
|
+
```ruby
|
|
233
|
+
invoice = Instatus.start_span("build-invoice") { build_invoice(order) }
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Errors captured inside a span carry its trace and span id, and are added to
|
|
237
|
+
it as an `exception` event.
|
|
238
|
+
|
|
239
|
+
### `Instatus.logger`
|
|
240
|
+
|
|
241
|
+
A `::Logger` whose records go to Logs, with the active span's trace id.
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
Instatus.logger.info("Charged #{order.id}")
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
`Instatus::Otel::Logger.new($stdout)` writes to a device as well.
|
|
248
|
+
|
|
249
|
+
### `Instatus.flush(timeout = 2)` / `Instatus.close(timeout = 2)`
|
|
250
|
+
|
|
251
|
+
Sends what is queued. `close` also shuts the SDK down. Both return `false`
|
|
252
|
+
if the timeout, in seconds, passes first. Short scripts and jobs that call
|
|
253
|
+
`exit!` should flush before they end; a normal exit flushes on its own.
|
|
254
|
+
|
|
255
|
+
## Contributing
|
|
256
|
+
|
|
257
|
+
Issues are welcome at [instatushq/otel](https://github.com/instatushq/otel/issues). This repository is published from Instatus's monorepo, so changes are made there and synced here.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Instatus
|
|
7
|
+
module Otel
|
|
8
|
+
LogRecord = Struct.new(:timestamp, :severity_number, :severity_text, :body, :attributes, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
module Attributes
|
|
11
|
+
Severity = Struct.new(:number, :text)
|
|
12
|
+
|
|
13
|
+
SEVERITY = {
|
|
14
|
+
fatal: Severity.new(21, "FATAL"),
|
|
15
|
+
error: Severity.new(17, "ERROR"),
|
|
16
|
+
warning: Severity.new(13, "WARN"),
|
|
17
|
+
warn: Severity.new(13, "WARN"),
|
|
18
|
+
log: Severity.new(9, "INFO"),
|
|
19
|
+
info: Severity.new(9, "INFO"),
|
|
20
|
+
debug: Severity.new(5, "DEBUG")
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
RESERVED_PREFIX = "instatus."
|
|
24
|
+
MAX_VALUE_LENGTH = 8192
|
|
25
|
+
MAX_STACK_LENGTH = 32_000
|
|
26
|
+
DEFAULT_EXCEPTION_TYPE = "Error"
|
|
27
|
+
USER_KEYS = { "username" => "name", "ip_address" => "ip" }.freeze
|
|
28
|
+
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
def severity_of(level)
|
|
32
|
+
SEVERITY.fetch(level.to_s.to_sym, SEVERITY[:error])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def error?(level)
|
|
36
|
+
severity_of(level).number >= SEVERITY[:error].number
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_attribute_value(value)
|
|
40
|
+
case value
|
|
41
|
+
when nil then nil
|
|
42
|
+
when String then value[0, MAX_VALUE_LENGTH]
|
|
43
|
+
when Integer, Float, true, false then value
|
|
44
|
+
when Symbol, Numeric then value.to_s
|
|
45
|
+
else stringify(value)[0, MAX_VALUE_LENGTH]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def stringify(value)
|
|
50
|
+
JSON.generate(value)
|
|
51
|
+
rescue StandardError
|
|
52
|
+
value.to_s
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def put(attributes, key, value)
|
|
56
|
+
return if key.start_with?(RESERVED_PREFIX)
|
|
57
|
+
|
|
58
|
+
attribute = to_attribute_value(Scrub.scrub_value(key, value))
|
|
59
|
+
attributes[key] = attribute unless attribute.nil? || attribute == ""
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def user_attributes(user)
|
|
63
|
+
attributes = {}
|
|
64
|
+
return attributes unless user
|
|
65
|
+
|
|
66
|
+
user = user.transform_keys(&:to_s)
|
|
67
|
+
end_user = user["id"] || user["email"] || user["username"]
|
|
68
|
+
attributes["enduser.id"] = end_user.to_s unless end_user.nil? || end_user.to_s.empty?
|
|
69
|
+
user.each { |key, value| put(attributes, "user.#{USER_KEYS.fetch(key, key)}", value) }
|
|
70
|
+
attributes
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def metadata_of(event)
|
|
74
|
+
metadata = {}
|
|
75
|
+
event.tags.each { |key, value| put(metadata, key.to_s, value) }
|
|
76
|
+
event.contexts.each do |name, context|
|
|
77
|
+
if context.is_a?(Hash)
|
|
78
|
+
context.each { |key, value| put(metadata, "#{name}.#{key}", value) }
|
|
79
|
+
else
|
|
80
|
+
put(metadata, name.to_s, context)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
event.extra.each { |key, value| put(metadata, "extra.#{key}", value) }
|
|
84
|
+
metadata.merge(user_attributes(event.user))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def exception_attributes(event)
|
|
88
|
+
exception = event.exception
|
|
89
|
+
if exception
|
|
90
|
+
attributes = {
|
|
91
|
+
"exception.type" => exception.type.to_s.empty? ? DEFAULT_EXCEPTION_TYPE : exception.type.to_s,
|
|
92
|
+
"exception.message" => Scrub.scrub_text(exception.value)[0, MAX_VALUE_LENGTH]
|
|
93
|
+
}
|
|
94
|
+
if exception.stacktrace
|
|
95
|
+
attributes["exception.stacktrace"] = Scrub.scrub_text(exception.stacktrace)[0, MAX_STACK_LENGTH]
|
|
96
|
+
end
|
|
97
|
+
attributes["exception.handled"] = exception.handled unless exception.handled.nil?
|
|
98
|
+
return attributes
|
|
99
|
+
end
|
|
100
|
+
return {} unless error?(event.level) && event.message
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
"exception.type" => DEFAULT_EXCEPTION_TYPE,
|
|
104
|
+
"exception.message" => Scrub.scrub_text(event.message)[0, MAX_VALUE_LENGTH]
|
|
105
|
+
}
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def request_attributes(request)
|
|
109
|
+
return {} unless request&.dig(:url)
|
|
110
|
+
|
|
111
|
+
url = Scrub.scrub_url(request[:url])
|
|
112
|
+
attributes = { "url.full" => url[0, MAX_VALUE_LENGTH] }
|
|
113
|
+
path = path_of(url)
|
|
114
|
+
attributes["url.path"] = path if path && !path.empty?
|
|
115
|
+
attributes["http.request.method"] = request[:method].to_s if request[:method]
|
|
116
|
+
attributes
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def path_of(url)
|
|
120
|
+
URI.parse(url).path
|
|
121
|
+
rescue URI::Error
|
|
122
|
+
nil
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def headline(type, message)
|
|
126
|
+
message.empty? ? type : "#{type}: #{message}"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def body_of(event)
|
|
130
|
+
exception = event.exception
|
|
131
|
+
return Scrub.scrub_text(event.message.to_s) unless exception
|
|
132
|
+
|
|
133
|
+
type = exception.type.to_s.empty? ? DEFAULT_EXCEPTION_TYPE : exception.type.to_s
|
|
134
|
+
headline(type, Scrub.scrub_text(exception.value))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Error fields go first and win: receivers cap attribute counts.
|
|
138
|
+
def log_record(event)
|
|
139
|
+
severity = severity_of(event.level)
|
|
140
|
+
attributes = exception_attributes(event)
|
|
141
|
+
attributes["log.record.uid"] = event.event_id
|
|
142
|
+
attributes.merge!(request_attributes(event.request))
|
|
143
|
+
metadata_of(event).each { |key, value| attributes[key] = value unless attributes.key?(key) }
|
|
144
|
+
|
|
145
|
+
LogRecord.new(
|
|
146
|
+
timestamp: event.timestamp,
|
|
147
|
+
severity_number: severity.number,
|
|
148
|
+
severity_text: severity.text,
|
|
149
|
+
body: body_of(event),
|
|
150
|
+
attributes: attributes
|
|
151
|
+
)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "opentelemetry"
|
|
4
|
+
|
|
5
|
+
module Instatus
|
|
6
|
+
module Otel
|
|
7
|
+
class Client
|
|
8
|
+
SPAN_EVENT_KEYS = %w[exception.type exception.message exception.stacktrace].freeze
|
|
9
|
+
|
|
10
|
+
attr_reader :config
|
|
11
|
+
|
|
12
|
+
def initialize(config, sink:, rate_cap: RateCap.new(config.max_events_per_minute, 60))
|
|
13
|
+
@config = config
|
|
14
|
+
@sink = sink
|
|
15
|
+
@rate_cap = rate_cap
|
|
16
|
+
@dedupe = Dedupe.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def capture_event(event, hint = {})
|
|
20
|
+
dropped = drop_reason(event, hint)
|
|
21
|
+
if dropped
|
|
22
|
+
log("dropped an event: #{dropped}")
|
|
23
|
+
return event.event_id
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
Dedupe.mark_captured(hint[:exception])
|
|
27
|
+
span = OpenTelemetry::Trace.current_span
|
|
28
|
+
send_event(run_before_send(prepare(event), hint), span)
|
|
29
|
+
event.event_id
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def flush(timeout)
|
|
33
|
+
@sink.flush(timeout)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def close(timeout)
|
|
37
|
+
@sink.close(timeout)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def drop_reason(event, hint)
|
|
43
|
+
return "disabled" unless config.enabled?
|
|
44
|
+
return "already captured" if Dedupe.captured?(hint[:exception])
|
|
45
|
+
return "sampled out" if config.sample_rate < 1 && rand >= config.sample_rate
|
|
46
|
+
return "excluded" if excluded?(hint[:exception])
|
|
47
|
+
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def excluded?(error)
|
|
52
|
+
return false unless error.is_a?(Exception)
|
|
53
|
+
|
|
54
|
+
Array(config.excluded_exceptions).any? do |excluded|
|
|
55
|
+
excluded.is_a?(Module) ? error.is_a?(excluded) : error.class.ancestors.any? { |ancestor| ancestor.name == excluded.to_s }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def prepare(event)
|
|
60
|
+
event.release ||= config.release
|
|
61
|
+
event.environment ||= config.environment
|
|
62
|
+
event
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def run_before_send(event, hint)
|
|
66
|
+
return event unless config.before_send
|
|
67
|
+
|
|
68
|
+
result = config.before_send.call(event, hint)
|
|
69
|
+
return result if result.is_a?(Event)
|
|
70
|
+
|
|
71
|
+
log("before_send dropped an event")
|
|
72
|
+
nil
|
|
73
|
+
rescue StandardError => e
|
|
74
|
+
log("before_send raised #{e.class}")
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Reporting must never raise into an app that is already handling an error.
|
|
79
|
+
def send_event(event, span)
|
|
80
|
+
return unless event
|
|
81
|
+
return log("dropped a duplicate event") if @dedupe.duplicate?(event)
|
|
82
|
+
return log("rate cap reached") unless @rate_cap.take
|
|
83
|
+
|
|
84
|
+
record = Attributes.log_record(event)
|
|
85
|
+
record_on_span(span, event, record)
|
|
86
|
+
@sink.emit(record, span.context)
|
|
87
|
+
rescue StandardError => e
|
|
88
|
+
log("could not send an event: #{e.class}: #{e.message}")
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Built from the scrubbed record, so traces carry nothing the log doesn't.
|
|
92
|
+
def record_on_span(span, event, record)
|
|
93
|
+
return unless span.recording? && event.exception
|
|
94
|
+
|
|
95
|
+
attributes = record.attributes.slice(*SPAN_EVENT_KEYS).transform_values(&:to_s)
|
|
96
|
+
span.add_event("exception", attributes: attributes)
|
|
97
|
+
return unless event.exception.handled == false
|
|
98
|
+
|
|
99
|
+
span.status = OpenTelemetry::Trace::Status.error(attributes["exception.message"])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def log(message)
|
|
103
|
+
warn("[instatus] #{message}") if config.debug
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
class Configuration
|
|
5
|
+
DEFAULT_MAX_EVENTS_PER_MINUTE = 300
|
|
6
|
+
DEFAULT_SHUTDOWN_TIMEOUT = 2
|
|
7
|
+
|
|
8
|
+
attr_accessor :api_key, :site_id, :endpoint, :service_name, :release, :environment,
|
|
9
|
+
:traces_sample_rate, :sample_rate, :before_send, :max_events_per_minute,
|
|
10
|
+
:enabled, :debug, :instrumentations, :metrics_export_interval,
|
|
11
|
+
:shutdown_timeout, :excluded_exceptions
|
|
12
|
+
|
|
13
|
+
def initialize(env = ENV)
|
|
14
|
+
@api_key = env["INSTATUS_API_KEY"]
|
|
15
|
+
@site_id = env["INSTATUS_SITE_ID"]
|
|
16
|
+
@service_name = env["OTEL_SERVICE_NAME"]
|
|
17
|
+
@traces_sample_rate = 1.0
|
|
18
|
+
@sample_rate = 1.0
|
|
19
|
+
@max_events_per_minute = DEFAULT_MAX_EVENTS_PER_MINUTE
|
|
20
|
+
@enabled = true
|
|
21
|
+
@debug = false
|
|
22
|
+
@instrumentations = nil
|
|
23
|
+
@metrics_export_interval = 60
|
|
24
|
+
@shutdown_timeout = DEFAULT_SHUTDOWN_TIMEOUT
|
|
25
|
+
@excluded_exceptions = []
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def enabled?
|
|
29
|
+
@enabled != false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Instatus
|
|
4
|
+
module Otel
|
|
5
|
+
# As Sentry's Dedupe: a repeat of the last event is a re-raise or a second reporter.
|
|
6
|
+
class Dedupe
|
|
7
|
+
MARKER = :@__instatus_captured
|
|
8
|
+
|
|
9
|
+
def self.captured?(error)
|
|
10
|
+
error.is_a?(Exception) && error.instance_variable_defined?(MARKER)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.mark_captured(error)
|
|
14
|
+
error.instance_variable_set(MARKER, true) if error.is_a?(Exception) && !error.frozen?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@previous = nil
|
|
19
|
+
@mutex = Mutex.new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def duplicate?(event)
|
|
23
|
+
signature = signature_of(event)
|
|
24
|
+
@mutex.synchronize do
|
|
25
|
+
duplicate = signature == @previous
|
|
26
|
+
@previous = signature
|
|
27
|
+
duplicate
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def signature_of(event)
|
|
34
|
+
exception = event.exception
|
|
35
|
+
return "message|#{event.message}" unless exception
|
|
36
|
+
|
|
37
|
+
"#{exception.type}|#{exception.value}|#{exception.stacktrace}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module Instatus
|
|
6
|
+
ExceptionValue = Struct.new(:type, :value, :stacktrace, :handled, :mechanism, keyword_init: true)
|
|
7
|
+
|
|
8
|
+
Event = Struct.new(
|
|
9
|
+
:event_id, :timestamp, :level, :message, :exception, :user, :tags, :extra, :contexts, :request,
|
|
10
|
+
:release, :environment,
|
|
11
|
+
keyword_init: true
|
|
12
|
+
) do
|
|
13
|
+
def self.build(level:, message: nil, exception: nil)
|
|
14
|
+
new(
|
|
15
|
+
event_id: SecureRandom.hex(16),
|
|
16
|
+
timestamp: Time.now,
|
|
17
|
+
level: level.to_sym,
|
|
18
|
+
message: message,
|
|
19
|
+
exception: exception,
|
|
20
|
+
tags: {},
|
|
21
|
+
extra: {},
|
|
22
|
+
contexts: {}
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.from_exception(error, handled:, mechanism:)
|
|
27
|
+
build(level: :error, exception: exception_value(error, handled: handled, mechanism: mechanism))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.from_message(message, level)
|
|
31
|
+
build(level: level, message: message.to_s)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Not full_message: its first line carries the message, which the stack parser can't read as a frame.
|
|
35
|
+
def self.exception_value(error, handled:, mechanism:)
|
|
36
|
+
unless error.is_a?(Exception)
|
|
37
|
+
type = error.is_a?(String) ? "RuntimeError" : error.class.name
|
|
38
|
+
value = error.is_a?(String) ? error : error.inspect
|
|
39
|
+
return ExceptionValue.new(type: type, value: value, handled: handled, mechanism: mechanism)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
ExceptionValue.new(
|
|
43
|
+
type: error.class.name || error.class.inspect,
|
|
44
|
+
value: error.message.to_s,
|
|
45
|
+
stacktrace: error.backtrace&.join("\n"),
|
|
46
|
+
handled: handled,
|
|
47
|
+
mechanism: mechanism
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|