brainzlab 0.1.26 → 0.1.28

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e298648a456d207e9ec5eb9be700b6aa898fc9b77666a3afd3c737d9c126705e
4
- data.tar.gz: b87707b7eb15cfb9dd11ca461203d0ccd1a7199bdd0ce972327381ecb675d592
3
+ metadata.gz: d9fdb52091b2fd8dc2da465cda4b4dc3f37f9b0bef3319f6b1bc6795a1a49c5d
4
+ data.tar.gz: 866b90f99c3f099618cb0cb981da3e75068824b89998adecad0edd5f89f9db68
5
5
  SHA512:
6
- metadata.gz: f834d99194f33b06a00f18016879e83fefcd92b5ac4bd589804a7629a2b123699ecf58082923920a72384502dacc1e40ced04ccfb2b36ec2cf6c2791adab2e3a
7
- data.tar.gz: 3999f40f6af0bb7b952baf59a35c78acbb38f89e41638824c86dc38a0319f0d964911fd3bc096bba272738c5bc1190777ce98a633a6050f9b7103b59ef8f13c7
6
+ metadata.gz: 3268bd499dfa2ae01e25bc18d57110ba45516d2a6971bca24158be6fd43d8c928ffb51f816946b7c3c084a8928d61c54b8507760deb78dec5a6f2288b5299db1
7
+ data.tar.gz: c7fd19b498c1d193f885ff0c0bddd86047006101195fe3a7f1a545ba3fa4e251691ce30ddf9589fbdd5b63dd5d98ab100527612b4c34e83c38d1fde8189b5211
@@ -42,6 +42,7 @@ module BrainzLab
42
42
  :pulse_buffer_size,
43
43
  :pulse_flush_interval,
44
44
  :pulse_sample_rate,
45
+ :pulse_sample_tier,
45
46
  :pulse_excluded_paths,
46
47
  :flux_enabled,
47
48
  :flux_url,
@@ -254,7 +255,12 @@ module BrainzLab
254
255
  @pulse_auto_provision = true
255
256
  @pulse_buffer_size = 50
256
257
  @pulse_flush_interval = 5
257
- @pulse_sample_rate = nil
258
+ # Trace sampling. Explicit rate (0.0–1.0) wins; otherwise derived from the
259
+ # tier. Defaults to the "internal" tier so the Brainz fleet samples ~10%
260
+ # (error traces are always kept). Client-facing apps set
261
+ # BRAINZLAB_PULSE_TIER=client (or PULSE_SAMPLE_RATE) to keep everything.
262
+ @pulse_sample_rate = (v = ENV['PULSE_SAMPLE_RATE'] || ENV['BRAINZLAB_PULSE_SAMPLE_RATE']).nil? || v.to_s.strip.empty? ? nil : v.to_f
263
+ @pulse_sample_tier = (ENV['BRAINZLAB_PULSE_TIER'] || 'internal').to_s.strip.downcase
258
264
  @pulse_excluded_paths = %w[/health /ping /up /assets]
259
265
 
260
266
  # Flux settings
@@ -509,6 +515,23 @@ module BrainzLab
509
515
  pulse_api_key || secret_key
510
516
  end
511
517
 
518
+ # Per-tier default trace sample rates. Internal (the Brainz fleet's own
519
+ # telemetry) samples aggressively; client/critical keep everything. Error
520
+ # traces are always kept regardless (see Pulse::Tracer).
521
+ PULSE_TIER_SAMPLE_RATES = {
522
+ 'internal' => 0.1,
523
+ 'standard' => 0.5,
524
+ 'client' => 1.0,
525
+ 'critical' => 1.0
526
+ }.freeze
527
+
528
+ # Effective 0.0–1.0 trace sample rate: explicit rate wins, else the tier's
529
+ # default, else 1.0 (sample everything) for unknown tiers.
530
+ def effective_pulse_sample_rate
531
+ rate = pulse_sample_rate || PULSE_TIER_SAMPLE_RATES.fetch(pulse_sample_tier, 1.0)
532
+ rate.to_f.clamp(0.0, 1.0)
533
+ end
534
+
512
535
  def flux_valid?
513
536
  key = flux_ingest_key || flux_api_key || secret_key
514
537
  !key.nil? && !key.empty?
@@ -118,7 +118,7 @@ module BrainzLab
118
118
 
119
119
  def update_content_length(headers, body)
120
120
  headers = headers.to_h.dup
121
- headers['Content-Length'] = body.sum(&:bytesize).to_s
121
+ headers['content-length'] = body.sum(&:bytesize).to_s
122
122
  headers
123
123
  end
124
124
  end
@@ -17,6 +17,7 @@ module BrainzLab
17
17
  end
18
18
 
19
19
  def start_trace(name, kind: 'custom', **attributes)
20
+ sample_rate = @config.effective_pulse_sample_rate
20
21
  trace = {
21
22
  trace_id: SecureRandom.uuid,
22
23
  name: name,
@@ -25,6 +26,10 @@ module BrainzLab
25
26
  environment: @config.environment,
26
27
  commit: @config.commit,
27
28
  host: @config.host,
29
+ sample_rate: sample_rate,
30
+ # Per-trace sampling decision (atomic, so spans never orphan). Errors
31
+ # override this at finish_trace and are always kept.
32
+ sampled: sample_rate >= 1.0 || rand < sample_rate,
28
33
  **attributes
29
34
  }
30
35
 
@@ -38,6 +43,14 @@ module BrainzLab
38
43
  trace = current_trace
39
44
  return unless trace
40
45
 
46
+ # Drop unsampled successful traces entirely (skip payload build + send);
47
+ # error traces are always kept regardless of the sampling decision.
48
+ unless error || trace.fetch(:sampled, true)
49
+ Thread.current[:brainzlab_pulse_trace] = nil
50
+ Thread.current[:brainzlab_pulse_spans] = nil
51
+ return
52
+ end
53
+
41
54
  ended_at = Time.now.utc
42
55
  duration_ms = ((ended_at - trace[:started_at]) * 1000).round(2)
43
56
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrainzLab
4
- VERSION = '0.1.26'
4
+ VERSION = '0.1.28'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainzlab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.26
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - BrainzLab