visionary-sdk 1.1.1 → 1.2.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.
data/lib/visionary.rb CHANGED
@@ -7,10 +7,12 @@ require_relative "visionary/store"
7
7
  require_relative "visionary/schemas"
8
8
  require_relative "visionary/redactor"
9
9
  require_relative "visionary/context"
10
+ require_relative "visionary/trace_context"
10
11
  require_relative "visionary/queue"
11
12
  require_relative "visionary/writer"
12
13
  require_relative "visionary/logs"
13
14
  require_relative "visionary/retention"
15
+ require_relative "visionary/metrics"
14
16
  require_relative "visionary/system/sources"
15
17
  require_relative "visionary/system/cgroup"
16
18
  require_relative "visionary/system/recorder"
@@ -24,8 +26,8 @@ require_relative "visionary/fork"
24
26
 
25
27
  # Visionary collects what the Visionary server analyses, straight from a Ruby
26
28
  # service: one api_logs row per HTTP request, spans for database and outbound
27
- # HTTP calls, errors, deploy markers, periodic system_metrics samples and the
28
- # app's own log lines (app_logs).
29
+ # HTTP calls, errors, deploy markers, periodic system_metrics samples, the
30
+ # app's own log lines (app_logs) and its custom metrics (metrics)
29
31
  #
30
32
  # # config.ru (Sinatra, Roda, Hanami, any Rack app)
31
33
  # require "visionary"
@@ -106,6 +108,11 @@ module Visionary
106
108
  # The agent's span and error recorder, or nil when collection is off.
107
109
  def telemetry = @agent&.telemetry
108
110
 
111
+ # Rows queued, written and dropped since start
112
+ def writer_stats
113
+ @agent ? @agent.writer_stats : WriterStats.new(queued: 0, written: 0, dropped: 0)
114
+ end
115
+
109
116
  # Flushes and stops collection, waiting at most timeout seconds.
110
117
  def close(timeout = 10)
111
118
  @agent&.close(timeout)
@@ -141,25 +148,26 @@ module Visionary
141
148
 
142
149
  # Measures the block as a span and returns its value. kind is "custom",
143
150
  # "cache", "queue", "db", "http", "graphql" or "background"; name should be low-cardinality (the
144
- # operation, not its arguments).
151
+ # operation, not its arguments)
145
152
  #
146
153
  # Visionary.span("charge card", kind: "http", target: "api.stripe.com") { stripe.charge(...) }
147
- def span(name, kind: "custom", target: nil, detail: nil)
154
+ def span(name, kind: "custom", target: nil, detail: nil, &block)
148
155
  rec = telemetry
149
156
  return yield unless rec&.spans_on
150
157
 
158
+ trace = TraceContext.child
151
159
  t0 = Telemetry.now_ms
152
160
  status = "ok"
153
161
  error = nil
154
162
  begin
155
- yield
163
+ TraceContext.with(trace[0], &block)
156
164
  rescue Exception => e # re-raised; only observed here
157
165
  status = "error"
158
166
  error = "#{e.class}: #{e.message}"
159
167
  raise
160
168
  ensure
161
169
  rec.span(kind: kind, name: name, target: target, detail: detail, start_mono: t0,
162
- duration_ms: Telemetry.now_ms - t0, status: status, error: error)
170
+ duration_ms: Telemetry.now_ms - t0, status: status, error: error, trace: trace)
163
171
  end
164
172
  end
165
173
 
@@ -169,7 +177,7 @@ module Visionary
169
177
  # request_id and offset_ms even after the response was sent; those ending
170
178
  # after the request was logged are written on their own and never change
171
179
  # its totals. Outside a request the span is a lone span (kept when it
172
- # takes at least VISIONARY_SPAN_THRESHOLD_MS).
180
+ # takes at least VISIONARY_SPAN_THRESHOLD_MS)
173
181
  #
174
182
  # If the block raises, the span gets status "error" and the message, and
175
183
  # the exception is re-raised in the thread as Ruby normally does: printed
@@ -181,12 +189,27 @@ module Visionary
181
189
  raise ArgumentError, "Visionary.background needs a block" unless block
182
190
 
183
191
  ctx = RequestContext.current # thread locals don't carry over on Ruby 3.1
192
+ trace = TraceContext.current
184
193
  Thread.new do
185
194
  RequestContext.current = ctx
195
+ TraceContext.current = trace
186
196
  span(name, kind: "background", &block)
187
197
  end
188
198
  end
189
199
 
200
+ # Labels are a Hash or keywords, and each distinct set is one series
201
+ def counter(name, labels = nil, **kw) = Metrics.counter(name, Metrics.labels(labels, kw))
202
+
203
+ def gauge(name, labels = nil, **kw) = Metrics.gauge(name, Metrics.labels(labels, kw))
204
+
205
+ def histogram(name, labels = nil, **kw) = Metrics.histogram(name, Metrics.labels(labels, kw))
206
+
207
+ # Sets a metric's unit and bucket upper bounds, before its first use
208
+ def describe_metric(name, unit: nil, buckets: nil)
209
+ Metrics.describe(name, unit: unit, buckets: buckets)
210
+ nil
211
+ end
212
+
190
213
  # Records a handled error (handled = 1). Inside a request it links to it.
191
214
  #
192
215
  # rescue PaymentError => e
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionary-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - styropyr0
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-30 00:00:00.000000000 Z
11
+ date: 2026-10-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  The Ruby collector for Visionary. A Rack middleware (with a Railtie for Rails)
@@ -32,6 +32,7 @@ files:
32
32
  - lib/visionary/logs.rb
33
33
  - lib/visionary/logs/logger.rb
34
34
  - lib/visionary/logs/pipeline.rb
35
+ - lib/visionary/metrics.rb
35
36
  - lib/visionary/middleware.rb
36
37
  - lib/visionary/profiler.rb
37
38
  - lib/visionary/queue.rb
@@ -40,14 +41,17 @@ files:
40
41
  - lib/visionary/retention.rb
41
42
  - lib/visionary/schema.rb
42
43
  - lib/visionary/schema/mysql/logs.sql
44
+ - lib/visionary/schema/mysql/metrics.sql
43
45
  - lib/visionary/schema/mysql/requests.sql
44
46
  - lib/visionary/schema/mysql/system.sql
45
47
  - lib/visionary/schema/mysql/telemetry.sql
46
48
  - lib/visionary/schema/postgres/logs.sql
49
+ - lib/visionary/schema/postgres/metrics.sql
47
50
  - lib/visionary/schema/postgres/requests.sql
48
51
  - lib/visionary/schema/postgres/system.sql
49
52
  - lib/visionary/schema/postgres/telemetry.sql
50
53
  - lib/visionary/schema/sqlite/logs.sql
54
+ - lib/visionary/schema/sqlite/metrics.sql
51
55
  - lib/visionary/schema/sqlite/requests.sql
52
56
  - lib/visionary/schema/sqlite/system.sql
53
57
  - lib/visionary/schema/sqlite/telemetry.sql
@@ -63,11 +67,12 @@ files:
63
67
  - lib/visionary/telemetry/errors.rb
64
68
  - lib/visionary/telemetry/hooks.rb
65
69
  - lib/visionary/telemetry/sql_text.rb
70
+ - lib/visionary/trace_context.rb
66
71
  - lib/visionary/version.rb
67
72
  - lib/visionary/writer.rb
68
73
  homepage: https://github.com/styropyr0/Visionary
69
74
  licenses:
70
- - LicenseRef-VNCL-1.0
75
+ - LicenseRef-VSAL-2.0
71
76
  metadata:
72
77
  homepage_uri: https://github.com/styropyr0/Visionary
73
78
  source_code_uri: https://github.com/styropyr0/Visionary/tree/main/sdks/ruby