Class SamplingAgentEventPublisher
- All Implemented Interfaces:
com.tnsai.observability.events.AgentEventPublisher
AgentEventPublisher decorator that consults an
EventSamplingPolicy before delegating each publish call
(issue #82).
Why a decorator: keeps the AgentEventPublisher SPI
in tnsai-core unchanged. Sampling is opt-in by wrapping:
AgentEventPublisher slf4j = new Slf4jAgentEventPublisher();
AgentEventPublisher sampled = new SamplingAgentEventPublisher(
slf4j, SamplingPolicies.production());
Decision flow: each publish call builds a SamplingInput
from the event's TnsAIEvent.eventType(), the supplied
EventLevel, and the active EventContext; the policy
returns a SamplingDecision and the decorator publishes only
when SamplingDecision.shouldPublish() is true.
Two publish overloads: when the level-aware overload is
called, the decorator uses the supplied level + context directly.
When the bare publish(event) overload is called, the
decorator falls back to EventLevel.INFO + the configured
Supplier for context. INFO is the rfc5424-aligned operational
default — most events worth publishing are at INFO unless the caller
said otherwise.
Composition: stacks naturally with other decorators —
wrap inside RedactingAgentEventPublisher so sampled events
are redacted on the way out, or wrap outside so the redactor only
runs on captured events (cheaper for high-drop policies).
isEnabled fast-path: pass-through to the delegate. Sampling happens at publish time — there is no policy decision available without an event in hand, so we don't second-guess the publisher's level filter here.
Thread-safety: stateless beyond the immutable delegate + policy + context supplier. Policies must themselves be thread-safe (the SPI requires it).
- Since:
- 0.9.0 (issue #82)
-
Constructor Summary
ConstructorsConstructorDescriptionSamplingAgentEventPublisher(com.tnsai.observability.events.AgentEventPublisher delegate, EventSamplingPolicy policy) Wrapdelegatewith sampling usingpolicy, falling back toEventContext.empty()for the barepublish(event)overload.SamplingAgentEventPublisher(com.tnsai.observability.events.AgentEventPublisher delegate, EventSamplingPolicy policy, Supplier<com.tnsai.observability.events.EventContext> contextSupplier) Wrapdelegatewith sampling usingpolicyand acontextSupplierthat returns theEventContextto attach to barepublish(event)calls. -
Method Summary
Modifier and TypeMethodDescriptionbooleanisEnabled(com.tnsai.observability.events.EventLevel level) voidpublish(com.tnsai.events.TnsAIEvent event) voidpublish(com.tnsai.events.TnsAIEvent event, com.tnsai.observability.events.EventLevel level, com.tnsai.observability.events.EventContext context)
-
Constructor Details
-
SamplingAgentEventPublisher
public SamplingAgentEventPublisher(com.tnsai.observability.events.AgentEventPublisher delegate, EventSamplingPolicy policy) Wrapdelegatewith sampling usingpolicy, falling back toEventContext.empty()for the barepublish(event)overload. -
SamplingAgentEventPublisher
public SamplingAgentEventPublisher(com.tnsai.observability.events.AgentEventPublisher delegate, EventSamplingPolicy policy, Supplier<com.tnsai.observability.events.EventContext> contextSupplier) Wrapdelegatewith sampling usingpolicyand acontextSupplierthat returns theEventContextto attach to barepublish(event)calls.- Throws:
NullPointerException- if any arg is null
-
-
Method Details
-
publish
public void publish(com.tnsai.events.TnsAIEvent event) - Specified by:
publishin interfacecom.tnsai.observability.events.AgentEventPublisher
-
publish
public void publish(com.tnsai.events.TnsAIEvent event, com.tnsai.observability.events.EventLevel level, com.tnsai.observability.events.EventContext context) - Specified by:
publishin interfacecom.tnsai.observability.events.AgentEventPublisher
-
isEnabled
public boolean isEnabled(com.tnsai.observability.events.EventLevel level) - Specified by:
isEnabledin interfacecom.tnsai.observability.events.AgentEventPublisher
-