Module: Shipeasy::SDK::See::Context

Defined in:
lib/shipeasy/sdk/see.rb

Overview

A per-request buffer of extras that merge into EVERY see() report firing later in the same execution context. Lets a request attach context (order id, route, tenant) from anywhere without threading it into the rescue block: Shipeasy.add_extras(order_id: oid) here, and any subsequent see() in this request carries it.

Fiber-local (like AnonId), so concurrent requests never bleed into each other. The Rack middleware clears it in an ensure at the end of each request; outside a Rack request (jobs, scripts) call Shipeasy.clear_extras yourself when a logical unit of work ends.

Values are stored raw and sanitized (scalar-only, truncated, 20-key cap, private-attribute stripped) at build time, exactly like chained extras.

Constant Summary collapse

THREAD_KEY =
:shipeasy_see_ambient_extras

Class Method Summary collapse

Class Method Details

.add(extras) ⇒ Object

Merge fields into the current context's buffer (string keys, later wins). Non-hash / empty input is ignored. Never raises.



356
357
358
359
360
361
362
363
364
# File 'lib/shipeasy/sdk/see.rb', line 356

def add(extras)
  return unless extras.is_a?(Hash) && !extras.empty?

  buf = (Thread.current[THREAD_KEY] ||= {})
  extras.each { |k, v| buf[k.to_s] = v }
  nil
rescue StandardError
  nil
end

.clearObject

Drop the current context's buffer so extras never leak to the next request handled by this thread/fiber.



374
375
376
# File 'lib/shipeasy/sdk/see.rb', line 374

def clear
  Thread.current[THREAD_KEY] = nil
end

.currentObject

A copy of the current context's buffer, or {} when empty.



367
368
369
370
# File 'lib/shipeasy/sdk/see.rb', line 367

def current
  buf = Thread.current[THREAD_KEY]
  buf.nil? || buf.empty? ? {} : buf.dup
end