Class ErrorReports
ErrorReports.
The framework's "one-line emit" entry point — consumers in catch blocks call:
try {
llmClient.chat(request);
} catch (LLMException e) {
ErrorReports.publish(e, ErrorCategory.TRANSIENT,
Map.of("provider", "openai", "model", "gpt-4o"));
throw e; // rethrow — emission is observation, not handling
}
The active publisher is one of:
- SPI-discovered (default): every
ErrorReportPublisherfound through Java service-provider discovery is wrapped in aCompositeErrorReportPublisheron first use. The framework shipsSlf4jErrorReportPublisheras the only SPI-registered publisher; consumers add Sentry / Loki / custom publishers by dropping a JAR with aMETA-INF/services/com.tnsai.observability.errors.ErrorReportPublisherfile. - Explicit override:
setPublisher(ErrorReportPublisher)replaces the discovered chain entirely. Use in tests (CapturingErrorReportPublisher) or in deployments that want a single-purpose pipeline.
Thread-safety: the active publisher reference is volatile +
atomically swapped via AtomicReference. Multiple threads
publishing concurrently see a consistent publisher. SPI discovery
is one-shot (lazy, double-checked) — switching publishers via
setPublisher(ErrorReportPublisher) discards the
discovered chain and uses the new one verbatim.
Reset between tests: call
resetForTesting() in @AfterEach to drop any
test-installed publisher and rediscover via SPI on the next
publish(ErrorReport) call. Tests that forget this leak
the test publisher across methods.
- Since:
- 0.5.0 (issue #86)
-
Method Summary
Modifier and TypeMethodDescriptionstatic ErrorReportPublisherRead the currently-active publisher, ornullif no publisher has been bound (and SPI discovery hasn't run yet).static voidpublish(ErrorReport report) Publish an already-constructedErrorReport.static voidConvenience: capture-and-publish in one call.static voidDrop any test-installed publisher and force the nextpublish(ErrorReport)to re-run SPI discovery.static voidsetPublisher(ErrorReportPublisher publisher) Replace the active publisher entirely.
-
Method Details
-
publish
Publish an already-constructedErrorReport. Errors during publication are swallowed — the caller is in an error path; making it harder is the wrong move. -
publish
Convenience: capture-and-publish in one call. Equivalent topublish(ErrorEmitter.report(t, category, attributes, Map.of())).- Parameters:
t- throwable; requiredcategory- required classificationattributes- diagnostic fields (low-cardinality recommended)
-
setPublisher
Replace the active publisher entirely. Subsequentpublish(ErrorReport)calls hit only the supplied publisher; SPI discovery is bypassed untilresetForTesting()or anothersetPublisher.Pass
nullto discard the override and re-enter SPI-discovery mode (next publish triggers a fresh ServiceLoader walk). -
activePublisher
Read the currently-active publisher, ornullif no publisher has been bound (and SPI discovery hasn't run yet). Useful for tests that want to assert teardown happened. -
resetForTesting
public static void resetForTesting()Drop any test-installed publisher and force the nextpublish(ErrorReport)to re-run SPI discovery. Call from@AfterEachin tests that override the publisher.
-