zharp 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 +33 -0
- data/LICENSE +21 -0
- data/README.md +170 -0
- data/lib/generators/zharp/install/install_generator.rb +39 -0
- data/lib/generators/zharp/install/templates/zharp.rb.tt +15 -0
- data/lib/zharp/bodies.rb +386 -0
- data/lib/zharp/capture.rb +361 -0
- data/lib/zharp/config.rb +129 -0
- data/lib/zharp/context.rb +133 -0
- data/lib/zharp/db_errors.rb +229 -0
- data/lib/zharp/errors.rb +158 -0
- data/lib/zharp/fork.rb +70 -0
- data/lib/zharp/headers.rb +63 -0
- data/lib/zharp/instrumentations.rb +17 -0
- data/lib/zharp/logs.rb +128 -0
- data/lib/zharp/rack.rb +148 -0
- data/lib/zharp/railtie.rb +47 -0
- data/lib/zharp/setup.rb +206 -0
- data/lib/zharp/sidekiq.rb +13 -0
- data/lib/zharp/source.rb +118 -0
- data/lib/zharp/stamp.rb +124 -0
- data/lib/zharp/version.rb +5 -0
- data/lib/zharp.rb +218 -0
- metadata +341 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zlib"
|
|
4
|
+
require "stringio"
|
|
5
|
+
|
|
6
|
+
module Zharp
|
|
7
|
+
# capture_bodies: request and response bodies on incoming requests' spans (any Rack app, Rails
|
|
8
|
+
# included) and outgoing Net::HTTP calls (Faraday's default adapter too), and secrets masked in
|
|
9
|
+
# every span's URL.
|
|
10
|
+
#
|
|
11
|
+
# Nothing changes what the app reads or writes. A request body is seen as the app reads it (the
|
|
12
|
+
# input is teed), a response body as the server writes it; only the first bytes up to the raw
|
|
13
|
+
# limit are kept:
|
|
14
|
+
# - text: the first bytes, decoded (and inflated when it was gzip/deflate encoded);
|
|
15
|
+
# - a form upload (multipart/form-data): each field, and each file as its name, type and size;
|
|
16
|
+
# - anything else (an image, a PDF, an event stream): only its size.
|
|
17
|
+
module Capture
|
|
18
|
+
# Fields kept from one form upload.
|
|
19
|
+
MAX_FIELDS = 200
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
def inflate(raw, encoding)
|
|
24
|
+
case encoding.to_s.strip.downcase
|
|
25
|
+
when "gzip", "x-gzip" then Zlib::GzipReader.new(StringIO.new(raw)).read
|
|
26
|
+
when "deflate"
|
|
27
|
+
begin
|
|
28
|
+
Zlib::Inflate.inflate(raw)
|
|
29
|
+
rescue Zlib::Error
|
|
30
|
+
Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(raw)
|
|
31
|
+
end
|
|
32
|
+
when "", "identity" then raw
|
|
33
|
+
end
|
|
34
|
+
rescue Zlib::Error, EOFError
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The attributes for one side, from the bytes seen. cut: more came than was kept.
|
|
39
|
+
def side_attributes(side, raw, total, cut, content_type, encoding, settings)
|
|
40
|
+
return {} if total.to_i.zero?
|
|
41
|
+
return Bodies.described_attributes(side, content_type, total) unless Bodies.textual?(content_type)
|
|
42
|
+
|
|
43
|
+
text = inflate(raw, encoding)
|
|
44
|
+
return Bodies.described_attributes(side, content_type, total) if text.nil?
|
|
45
|
+
|
|
46
|
+
Bodies.body_attributes(side, text, content_type, settings, cut)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A form upload as Rack parsed it (rack.request.form_hash): fields as they are, files described.
|
|
50
|
+
def form_fields(value, depth = 0, count = [0])
|
|
51
|
+
return "[…]" if depth > 10
|
|
52
|
+
case value
|
|
53
|
+
when Hash
|
|
54
|
+
if value[:tempfile] || value["tempfile"]
|
|
55
|
+
file = value[:tempfile] || value["tempfile"]
|
|
56
|
+
size = file.respond_to?(:size) ? file.size : nil
|
|
57
|
+
return Bodies.file_label(value[:filename] || value["filename"], value[:type] || value["type"], size)
|
|
58
|
+
end
|
|
59
|
+
value.each_with_object({}) do |(k, v), out|
|
|
60
|
+
break out if (count[0] += 1) > MAX_FIELDS
|
|
61
|
+
|
|
62
|
+
out[k.to_s] = form_fields(v, depth + 1, count)
|
|
63
|
+
end
|
|
64
|
+
when Array then value.map { |v| form_fields(v, depth + 1, count) }
|
|
65
|
+
when nil then nil
|
|
66
|
+
else value.respond_to?(:tempfile) ? Bodies.file_label(value.original_filename, value.content_type, value.size) : value.to_s
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# rack.input, teed: what the app reads is kept up to the limit (a rewind isn't counted twice).
|
|
71
|
+
class TeeInput
|
|
72
|
+
attr_reader :total
|
|
73
|
+
|
|
74
|
+
def initialize(input, limit)
|
|
75
|
+
@input = input
|
|
76
|
+
@limit = limit
|
|
77
|
+
@kept = +"".b
|
|
78
|
+
@pos = 0
|
|
79
|
+
@total = 0
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def read(*args)
|
|
83
|
+
data = @input.read(*args)
|
|
84
|
+
see(data)
|
|
85
|
+
data
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def gets(*args)
|
|
89
|
+
data = @input.gets(*args)
|
|
90
|
+
see(data)
|
|
91
|
+
data
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def each
|
|
95
|
+
return enum_for(:each) unless block_given?
|
|
96
|
+
|
|
97
|
+
@input.each do |chunk|
|
|
98
|
+
see(chunk)
|
|
99
|
+
yield chunk
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def rewind
|
|
104
|
+
@pos = 0
|
|
105
|
+
@input.rewind
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def close = @input.respond_to?(:close) ? @input.close : nil
|
|
109
|
+
def respond_to_missing?(name, include_private = false) = @input.respond_to?(name, include_private)
|
|
110
|
+
def method_missing(name, ...) = @input.respond_to?(name) ? @input.public_send(name, ...) : super
|
|
111
|
+
|
|
112
|
+
# What the app left unread, read here (up to the limit) after it answered.
|
|
113
|
+
def drain(content_length)
|
|
114
|
+
length = content_length.to_i
|
|
115
|
+
return if length <= 0 || @total >= length
|
|
116
|
+
|
|
117
|
+
while @total < [length, @limit].min
|
|
118
|
+
chunk = read([16 * 1024, @limit - @total].min)
|
|
119
|
+
break if chunk.nil? || chunk.empty?
|
|
120
|
+
end
|
|
121
|
+
rescue StandardError
|
|
122
|
+
nil
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def kept = @kept
|
|
126
|
+
def cut? = @total > @kept.bytesize
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
def see(data)
|
|
131
|
+
return if data.nil? || data.empty?
|
|
132
|
+
|
|
133
|
+
data = data.b
|
|
134
|
+
start = @pos
|
|
135
|
+
@pos += data.bytesize
|
|
136
|
+
return if @pos <= @total # read again after a rewind
|
|
137
|
+
|
|
138
|
+
fresh = data.byteslice((@total - start).clamp(0, data.bytesize)..)
|
|
139
|
+
@total = @pos
|
|
140
|
+
room = @limit - @kept.bytesize
|
|
141
|
+
@kept << fresh.byteslice(0, room) if room.positive?
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# A response body that isn't an Array: chunks kept as the server writes them, the attributes
|
|
146
|
+
# set when it closes (before the request's span ends).
|
|
147
|
+
class TapBody
|
|
148
|
+
def initialize(body, limit, &done)
|
|
149
|
+
@body = body
|
|
150
|
+
@limit = limit
|
|
151
|
+
@kept = +"".b
|
|
152
|
+
@total = 0
|
|
153
|
+
@done = done
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def each
|
|
157
|
+
@body.each do |chunk|
|
|
158
|
+
@total += chunk.bytesize
|
|
159
|
+
room = @limit - @kept.bytesize
|
|
160
|
+
@kept << chunk.b.byteslice(0, room) if room.positive?
|
|
161
|
+
yield chunk
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def close
|
|
166
|
+
@body.close if @body.respond_to?(:close)
|
|
167
|
+
ensure
|
|
168
|
+
begin
|
|
169
|
+
@done&.call(@kept, @total, @total > @kept.bytesize)
|
|
170
|
+
rescue StandardError
|
|
171
|
+
nil
|
|
172
|
+
end
|
|
173
|
+
@done = nil
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def respond_to?(name, include_all = false)
|
|
177
|
+
return false if %i[to_ary call to_path].include?(name.to_sym)
|
|
178
|
+
|
|
179
|
+
super || @body.respond_to?(name, include_all)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def method_missing(name, ...) = @body.respond_to?(name) ? @body.public_send(name, ...) : super
|
|
183
|
+
def respond_to_missing?(name, include_private = false) = @body.respond_to?(name, include_private)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# ─── incoming (Rack) ─────────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
def rack_url(env)
|
|
189
|
+
scheme = env["rack.url_scheme"] || "http"
|
|
190
|
+
host = env["HTTP_HOST"] || env["SERVER_NAME"]
|
|
191
|
+
query = env["QUERY_STRING"].to_s
|
|
192
|
+
"#{scheme}://#{host}#{env['SCRIPT_NAME']}#{env['PATH_INFO']}#{query.empty? ? '' : "?#{query}"}"
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Before the app: tee its input. Returns the tee (or nil when this request isn't captured).
|
|
196
|
+
def start_request(env, settings)
|
|
197
|
+
return nil unless Bodies.allowed?(settings, rack_url(env))
|
|
198
|
+
|
|
199
|
+
input = env["rack.input"]
|
|
200
|
+
tee = TeeInput.new(input || StringIO.new(+""), Bodies.raw_limit(settings))
|
|
201
|
+
env["rack.input"] = tee if input
|
|
202
|
+
tee
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# After the app: the request body's attributes now, the response's when its body closes.
|
|
206
|
+
# Returns the body to hand back.
|
|
207
|
+
def finish_request(span, env, tee, status, headers, body, settings)
|
|
208
|
+
return body unless Bodies.wanted?(settings, status)
|
|
209
|
+
|
|
210
|
+
span.add_attributes(request_attributes(env, tee, settings))
|
|
211
|
+
response_body(span, headers, body, settings)
|
|
212
|
+
rescue StandardError => e
|
|
213
|
+
Zharp.debug("bodies: #{e.message}")
|
|
214
|
+
body
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def request_attributes(env, tee, settings)
|
|
218
|
+
type = env["CONTENT_TYPE"]
|
|
219
|
+
if Bodies.multipart?(type)
|
|
220
|
+
form = env["rack.request.form_hash"]
|
|
221
|
+
return Bodies.body_attributes("request", JSON.generate(form_fields(form)), type, settings) if form
|
|
222
|
+
|
|
223
|
+
return Bodies.described_attributes("request", type, env["CONTENT_LENGTH"]&.to_i)
|
|
224
|
+
end
|
|
225
|
+
tee.drain(env["CONTENT_LENGTH"]) if Bodies.textual?(type)
|
|
226
|
+
side_attributes("request", tee.kept, tee.total, tee.cut?, type, env["HTTP_CONTENT_ENCODING"], settings)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def header(headers, name)
|
|
230
|
+
return nil unless headers
|
|
231
|
+
|
|
232
|
+
headers[name] || headers.find { |k, _| k.to_s.casecmp?(name) }&.last
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def response_body(span, headers, body, settings)
|
|
236
|
+
type = header(headers, "content-type")
|
|
237
|
+
encoding = header(headers, "content-encoding")
|
|
238
|
+
length = header(headers, "content-length")&.to_i
|
|
239
|
+
unless Bodies.textual?(type)
|
|
240
|
+
span.add_attributes(Bodies.described_attributes("response", type, length))
|
|
241
|
+
return body
|
|
242
|
+
end
|
|
243
|
+
if body.respond_to?(:call) && !body.respond_to?(:each) # a streaming body (Rack 3)
|
|
244
|
+
span.add_attributes(Bodies.unread_attributes("response", type, length))
|
|
245
|
+
return body
|
|
246
|
+
end
|
|
247
|
+
if body.respond_to?(:to_ary)
|
|
248
|
+
parts = body.to_ary
|
|
249
|
+
raw = parts.join.b
|
|
250
|
+
limit = Bodies.raw_limit(settings)
|
|
251
|
+
span.add_attributes(side_attributes("response", raw.byteslice(0, limit), raw.bytesize, raw.bytesize > limit, type, encoding, settings))
|
|
252
|
+
return parts
|
|
253
|
+
end
|
|
254
|
+
TapBody.new(body, Bodies.raw_limit(settings)) do |kept, total, cut|
|
|
255
|
+
span.add_attributes(side_attributes("response", kept, total, cut, type, encoding, settings)) if span.recording?
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# ─── outgoing (Net::HTTP) ────────────────────────────────────────────────────
|
|
260
|
+
|
|
261
|
+
# Inside OpenTelemetry's Net::HTTP span (prepended before its instrumentation, so it runs within
|
|
262
|
+
# that span): the request body, and the response body Net::HTTP already read.
|
|
263
|
+
module NetHTTP
|
|
264
|
+
def request(req, body = nil, &block)
|
|
265
|
+
settings = Zharp.options&.capture_bodies
|
|
266
|
+
return super unless settings && started?
|
|
267
|
+
|
|
268
|
+
span = ::OpenTelemetry::Trace.current_span
|
|
269
|
+
return super unless span.recording? && span.respond_to?(:kind) && span.kind == :client
|
|
270
|
+
|
|
271
|
+
url = "#{use_ssl? ? 'https' : 'http'}://#{address}:#{port}#{req.path}"
|
|
272
|
+
return super unless Bodies.allowed?(settings, url)
|
|
273
|
+
|
|
274
|
+
begin
|
|
275
|
+
response = super
|
|
276
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
|
277
|
+
Capture.add(span) { Capture.outgoing_request(req, body, settings) }
|
|
278
|
+
raise
|
|
279
|
+
end
|
|
280
|
+
if Bodies.wanted?(settings, response.code.to_i)
|
|
281
|
+
Capture.add(span) do
|
|
282
|
+
attrs = Capture.outgoing_request(req, body, settings)
|
|
283
|
+
attrs.merge(Capture.outgoing_response(response, settings))
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
response
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def add(span)
|
|
291
|
+
span.add_attributes(yield)
|
|
292
|
+
rescue StandardError => e
|
|
293
|
+
Zharp.debug("bodies: #{e.message}")
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def outgoing_request(req, body, settings)
|
|
297
|
+
data = body || req.body
|
|
298
|
+
type = req["content-type"]
|
|
299
|
+
return Bodies.unread_attributes("request", type, req["content-length"]&.to_i) if data.nil? && req.body_stream
|
|
300
|
+
return {} if data.nil?
|
|
301
|
+
|
|
302
|
+
raw = data.to_s.b
|
|
303
|
+
limit = Bodies.raw_limit(settings)
|
|
304
|
+
side_attributes("request", raw.byteslice(0, limit), raw.bytesize, raw.bytesize > limit, type, req["content-encoding"], settings)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def outgoing_response(response, settings)
|
|
308
|
+
type = response["content-type"]
|
|
309
|
+
body = response.instance_variable_get(:@body)
|
|
310
|
+
# Streamed to the app in a block (read_body { |chunk| }), so not kept: described.
|
|
311
|
+
unless response.instance_variable_get(:@read) && (body.is_a?(String) || body.nil?)
|
|
312
|
+
return Bodies.unread_attributes("response", type, response["content-length"]&.to_i)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
raw = body.to_s.b
|
|
316
|
+
limit = Bodies.raw_limit(settings)
|
|
317
|
+
# Net::HTTP inflates what it asked to be compressed and drops the header then.
|
|
318
|
+
side_attributes("response", raw.byteslice(0, limit), raw.bytesize, raw.bytesize > limit, type, response["content-encoding"], settings)
|
|
319
|
+
rescue StandardError
|
|
320
|
+
{}
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def install_net_http
|
|
324
|
+
::Net::HTTP.prepend(NetHTTP) unless ::Net::HTTP.ancestors.include?(NetHTTP)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# ─── URLs ────────────────────────────────────────────────────────────────────
|
|
328
|
+
|
|
329
|
+
URL_KEYS = %w[url.full http.url http.target].freeze
|
|
330
|
+
|
|
331
|
+
# Secrets in URLs never leave the process: secret-looking query values (token, key, sig,
|
|
332
|
+
# password…) in a span's URL attributes become [Filtered] and user:password@ is dropped, on
|
|
333
|
+
# every span, whatever made it, with or without capture_bodies.
|
|
334
|
+
class UrlRedaction
|
|
335
|
+
def on_start(span, parent_context); end
|
|
336
|
+
|
|
337
|
+
# Runs inside the span's own lock as it ends, so the attributes are changed in place.
|
|
338
|
+
def on_finishing(span)
|
|
339
|
+
attributes = span.instance_variable_get(:@attributes)
|
|
340
|
+
return unless attributes.is_a?(Hash) && !attributes.frozen?
|
|
341
|
+
|
|
342
|
+
URL_KEYS.each do |key|
|
|
343
|
+
value = attributes[key]
|
|
344
|
+
attributes[key] = Bodies.redact_url(value) if value.is_a?(String) && value.include?("?") || value.is_a?(String) && value.include?("@")
|
|
345
|
+
end
|
|
346
|
+
query = attributes["url.query"]
|
|
347
|
+
if query.is_a?(String)
|
|
348
|
+
bare = query.delete_prefix("?")
|
|
349
|
+
redacted = Bodies.redact_query(bare)
|
|
350
|
+
attributes["url.query"] = "#{query.start_with?('?') ? '?' : ''}#{redacted}" if redacted != bare
|
|
351
|
+
end
|
|
352
|
+
rescue StandardError
|
|
353
|
+
nil
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def on_finish(span); end
|
|
357
|
+
def force_flush(timeout: nil) = ::OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
358
|
+
def shutdown(timeout: nil) = ::OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
end
|
data/lib/zharp/config.rb
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zharp
|
|
4
|
+
# Options from Zharp.init and the environment, read like the Python SDK's: what is passed wins,
|
|
5
|
+
# then ZHARP_*, then the default. Returns nil from .resolve when there is no key.
|
|
6
|
+
module Config
|
|
7
|
+
DEFAULT_ENDPOINT = "https://api.zharp.io/api/v1/otlp"
|
|
8
|
+
|
|
9
|
+
TRUE = /\A(true|1|yes|on)\z/i
|
|
10
|
+
FALSE = /\A(false|0|no|off|none)\z/i
|
|
11
|
+
|
|
12
|
+
Options = Struct.new(
|
|
13
|
+
:key, :service, :environment, :release, :endpoint, :sample_rate,
|
|
14
|
+
:tracing, :logs, :log_correlation, :capture_uncaught,
|
|
15
|
+
:capture_headers, :capture_query_errors, :capture_source_context, :capture_bodies,
|
|
16
|
+
:instrument, :instrumentation,
|
|
17
|
+
keyword_init: true
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# An environment variable; nil when unset or blank.
|
|
23
|
+
def env(name)
|
|
24
|
+
value = ENV[name]
|
|
25
|
+
return nil if value.nil?
|
|
26
|
+
|
|
27
|
+
value = value.strip
|
|
28
|
+
value.empty? ? nil : value
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The first value that is non-blank text.
|
|
32
|
+
def pick(*values)
|
|
33
|
+
values.each do |value|
|
|
34
|
+
next if value.nil?
|
|
35
|
+
|
|
36
|
+
text = value.to_s.strip
|
|
37
|
+
return text unless text.empty?
|
|
38
|
+
end
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def flag(value)
|
|
43
|
+
return nil if value.nil?
|
|
44
|
+
return true if TRUE.match?(value.to_s)
|
|
45
|
+
return false if FALSE.match?(value.to_s)
|
|
46
|
+
|
|
47
|
+
nil
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# ZHARP_DISABLED=true: start nothing, whatever the code asks for.
|
|
51
|
+
def disabled?
|
|
52
|
+
flag(env("ZHARP_DISABLED")) == true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# An on/off setting: the argument when passed, else the environment variable, else the default.
|
|
56
|
+
def switch(value, variable, default)
|
|
57
|
+
return value ? true : false unless value.nil?
|
|
58
|
+
|
|
59
|
+
from_env = flag(env(variable))
|
|
60
|
+
from_env.nil? ? default : from_env
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def resolve(args = {})
|
|
64
|
+
key = pick(args[:key], env("ZHARP_KEY"), env("ZHARP_API_KEY"), credentials_key)
|
|
65
|
+
return nil unless key
|
|
66
|
+
|
|
67
|
+
Options.new(
|
|
68
|
+
key: key,
|
|
69
|
+
service: pick(args[:service], env("ZHARP_SERVICE"), env("OTEL_SERVICE_NAME"), app_name) || "unknown-service",
|
|
70
|
+
environment: pick(args[:environment], env("ZHARP_ENVIRONMENT"), env("ZHARP_ENV"), rails_env),
|
|
71
|
+
release: pick(args[:release], env("ZHARP_RELEASE"), env("ZHARP_VERSION")),
|
|
72
|
+
endpoint: (pick(args[:endpoint], env("ZHARP_ENDPOINT")) || DEFAULT_ENDPOINT).sub(%r{/+\z}, ""),
|
|
73
|
+
sample_rate: sample_rate(args[:sample_rate]),
|
|
74
|
+
tracing: switch(args[:tracing], "ZHARP_TRACING", true),
|
|
75
|
+
logs: switch(args[:logs], "ZHARP_LOGS", false),
|
|
76
|
+
log_correlation: switch(args[:log_correlation], "ZHARP_LOG_CORRELATION", true),
|
|
77
|
+
capture_uncaught: args.fetch(:capture_uncaught, true) ? true : false,
|
|
78
|
+
capture_headers: Headers.settings(args[:capture_headers], env("ZHARP_CAPTURE_HEADERS")),
|
|
79
|
+
capture_query_errors: switch(args[:capture_query_errors], "ZHARP_CAPTURE_QUERY_ERRORS", true),
|
|
80
|
+
capture_source_context: switch(args[:capture_source_context], "ZHARP_CAPTURE_SOURCE_CONTEXT", true),
|
|
81
|
+
capture_bodies: Bodies.settings(args[:capture_bodies], env("ZHARP_CAPTURE_BODIES")),
|
|
82
|
+
instrument: args.fetch(:instrument, true) ? true : false,
|
|
83
|
+
instrumentation: args[:instrumentation] || {}
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def sample_rate(value)
|
|
88
|
+
rate = value.nil? ? Float(env("ZHARP_SAMPLE_RATE") || 1.0) : Float(value)
|
|
89
|
+
rate.clamp(0.0, 1.0)
|
|
90
|
+
rescue ArgumentError, TypeError
|
|
91
|
+
1.0
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Rails: credentials.zharp.key (bin/rails credentials:edit), after ZHARP_KEY.
|
|
95
|
+
def credentials_key
|
|
96
|
+
return nil unless defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application
|
|
97
|
+
|
|
98
|
+
::Rails.application.credentials.dig(:zharp, :key)
|
|
99
|
+
rescue StandardError
|
|
100
|
+
nil
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def rails_env
|
|
104
|
+
defined?(::Rails) && ::Rails.respond_to?(:env) ? ::Rails.env.to_s : nil
|
|
105
|
+
rescue StandardError
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The Rails app's name (Shop::Application -> "shop"), else the project folder Bundler runs in.
|
|
110
|
+
def app_name
|
|
111
|
+
if defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application
|
|
112
|
+
name = ::Rails.application.class.name.to_s.split("::").first
|
|
113
|
+
return name.gsub(/([a-z\d])([A-Z])/, '\1-\2').downcase if name && !name.empty?
|
|
114
|
+
end
|
|
115
|
+
return File.basename(::Bundler.root.to_s) if defined?(::Bundler) && ::Bundler.respond_to?(:root)
|
|
116
|
+
|
|
117
|
+
nil
|
|
118
|
+
rescue StandardError
|
|
119
|
+
nil
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# One line on stderr, prefixed so it can be searched for.
|
|
123
|
+
def announce(message)
|
|
124
|
+
$stderr.write("[Zharp] #{message}\n")
|
|
125
|
+
rescue StandardError
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zharp
|
|
4
|
+
# Who and what a span or log belongs to: the user, the session, and tags.
|
|
5
|
+
#
|
|
6
|
+
# A browser or mobile app using a Zharp SDK sends enduser.id and session.id in W3C baggage next to
|
|
7
|
+
# traceparent. The processors here copy them onto every span and log of that request, so the back
|
|
8
|
+
# end's telemetry joins the front end's. Zharp.set_user overrides them for the rest of the
|
|
9
|
+
# request (or, outside one, for everything after).
|
|
10
|
+
module Context
|
|
11
|
+
USER_ID = "enduser.id"
|
|
12
|
+
USER_EMAIL = "enduser.email"
|
|
13
|
+
USER_NAME = "enduser.name"
|
|
14
|
+
SESSION_ID = "session.id"
|
|
15
|
+
PROPAGATED = [USER_ID, SESSION_ID].freeze
|
|
16
|
+
INHERITED = [USER_ID, USER_EMAIL, USER_NAME, SESSION_ID].freeze
|
|
17
|
+
USER_IVAR = :@__zharp_user
|
|
18
|
+
|
|
19
|
+
@tags = {}
|
|
20
|
+
@global_user = nil
|
|
21
|
+
@lock = Mutex.new
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
attr_reader :tags
|
|
25
|
+
|
|
26
|
+
# The user goes on the current span and the request's span (and from them onto every span
|
|
27
|
+
# started after), or, outside a request, on everything after.
|
|
28
|
+
def set_user(id: nil, email: nil, name: nil)
|
|
29
|
+
user = { USER_ID => id, USER_EMAIL => email, USER_NAME => name }.compact.transform_values(&:to_s)
|
|
30
|
+
spans = request_spans
|
|
31
|
+
if spans.empty?
|
|
32
|
+
@global_user = user.empty? ? nil : user
|
|
33
|
+
return
|
|
34
|
+
end
|
|
35
|
+
spans.each do |span|
|
|
36
|
+
span.instance_variable_set(USER_IVAR, user)
|
|
37
|
+
span.add_attributes(user) unless user.empty?
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def clear_user
|
|
42
|
+
set_user
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def set_tag(key, value)
|
|
46
|
+
@lock.synchronize do
|
|
47
|
+
if value.nil?
|
|
48
|
+
@tags = @tags.reject { |k, _| k == key.to_s }
|
|
49
|
+
else
|
|
50
|
+
@tags = @tags.merge(key.to_s => plain_value(value))
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def request_spans
|
|
56
|
+
spans = [::OpenTelemetry::Trace.current_span]
|
|
57
|
+
spans << ::OpenTelemetry::Instrumentation::Rack.current_span if defined?(::OpenTelemetry::Instrumentation::Rack) && ::OpenTelemetry::Instrumentation::Rack.respond_to?(:current_span)
|
|
58
|
+
spans.select(&:recording?).uniq
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The tags, then user and session from the parent span, baggage and set_user (later wins).
|
|
62
|
+
def attributes_for(context = nil)
|
|
63
|
+
context ||= ::OpenTelemetry::Context.current
|
|
64
|
+
out = @tags.dup
|
|
65
|
+
parent = ::OpenTelemetry::Trace.current_span(context)
|
|
66
|
+
if parent.respond_to?(:attributes) && parent.recording?
|
|
67
|
+
attrs = parent.attributes || {}
|
|
68
|
+
INHERITED.each { |k| out[k] = attrs[k] if attrs[k] && !attrs[k].to_s.empty? }
|
|
69
|
+
end
|
|
70
|
+
PROPAGATED.each do |k|
|
|
71
|
+
value = ::OpenTelemetry::Baggage.value(k, context: context)
|
|
72
|
+
out[k] = value.to_s if value && !value.to_s.empty?
|
|
73
|
+
end
|
|
74
|
+
user = parent.instance_variable_get(USER_IVAR) if parent.instance_variable_defined?(USER_IVAR)
|
|
75
|
+
user ||= @global_user
|
|
76
|
+
if user
|
|
77
|
+
[USER_ID, USER_EMAIL, USER_NAME].each { |k| out.delete(k) }
|
|
78
|
+
out.merge!(user)
|
|
79
|
+
end
|
|
80
|
+
out
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def plain_value(value)
|
|
84
|
+
case value
|
|
85
|
+
when String, Integer, Float, true, false then value
|
|
86
|
+
when Array then value.all? { |v| v.is_a?(String) || v.is_a?(Numeric) || v == true || v == false } ? value : value.to_s
|
|
87
|
+
else value.to_s
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def plain(attributes)
|
|
92
|
+
(attributes || {}).each_with_object({}) do |(k, v), out|
|
|
93
|
+
out[k.to_s] = plain_value(v) unless v.nil?
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Puts user, session and tags on every span as it starts; a span inherits its parent's set_user.
|
|
99
|
+
class SpanProcessor
|
|
100
|
+
def on_start(span, parent_context)
|
|
101
|
+
attrs = Context.attributes_for(parent_context)
|
|
102
|
+
span.add_attributes(attrs) unless attrs.empty?
|
|
103
|
+
parent = ::OpenTelemetry::Trace.current_span(parent_context)
|
|
104
|
+
if parent.instance_variable_defined?(USER_IVAR)
|
|
105
|
+
span.instance_variable_set(USER_IVAR, parent.instance_variable_get(USER_IVAR))
|
|
106
|
+
end
|
|
107
|
+
rescue StandardError => e
|
|
108
|
+
Zharp.debug("context: #{e.message}")
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def on_finishing(span); end
|
|
112
|
+
def on_finish(span); end
|
|
113
|
+
|
|
114
|
+
def force_flush(timeout: nil) = ::OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
115
|
+
def shutdown(timeout: nil) = ::OpenTelemetry::SDK::Trace::Export::SUCCESS
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Puts user, session and tags on every log record, before it is batched.
|
|
119
|
+
class LogProcessor
|
|
120
|
+
def on_emit(log_record, context)
|
|
121
|
+
extra = Context.attributes_for(context)
|
|
122
|
+
return if extra.empty?
|
|
123
|
+
|
|
124
|
+
log_record.attributes = extra.merge(log_record.attributes || {})
|
|
125
|
+
rescue StandardError => e
|
|
126
|
+
Zharp.debug("context: #{e.message}")
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def force_flush(timeout: nil) = ::OpenTelemetry::SDK::Logs::Export::SUCCESS
|
|
130
|
+
def shutdown(timeout: nil) = ::OpenTelemetry::SDK::Logs::Export::SUCCESS
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|