Package dev.tripswitch
Class TripSwitch
java.lang.Object
dev.tripswitch.TripSwitch
- All Implemented Interfaces:
AutoCloseable
The main Tripswitch runtime client. Maintains real-time breaker state via SSE
and reports execution samples to the Tripswitch API.
TripSwitch client = TripSwitch.builder("proj_123")
.apiKey("eb_pk_...")
.ingestSecret("64-char-hex")
.build();
String result = client.execute(() -> doWork(),
TripSwitch.withBreakers("my-breaker"),
TripSwitch.withRouter("my-router"),
TripSwitch.withMetrics(Map.of("latency", TripSwitch.LATENCY))
);
client.close();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfaceCallback for breaker state changes. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic TripSwitch.Buildervoidclose()voidGracefully shuts down, flushing buffered samples within the timeout.<T> Texecute(Callable<T> task, ExecuteOption... options) Wraps a task with circuit breaker logic.Returns a copy of all cached breaker states.Returns a deep copy of cached breaker metadata, or null if not yet loaded.Returns a deep copy of cached router metadata, or null if not yet loaded.Returns the cached state for a single breaker, or null.Fetches project health status from the API.listBreakersMetadata(String etag) Lists breaker metadata with ETag support.listRoutersMetadata(String etag) Lists router metadata with ETag support.voidreport(ReportInput input) Fire-and-forget sample reporting.stats()Returns SDK health metrics snapshot.static ExecuteOptionwithBreakers(String... names) Specifies breaker names to check before executing.static <T> ExecuteOptionRegisters a function to extract metrics from the task result after execution.static ExecuteOptionwithErrorEvaluator(Predicate<Exception> evaluator) Custom predicate to determine if an exception is a failure.static ExecuteOptionwithIgnoreErrors(Class<? extends Exception>... types) Specifies exception classes that should not count as failures.static ExecuteOptionwithMetrics(Map<String, Object> metrics) Sets metrics to report.static ExecuteOptionwithRouter(String routerId) Specifies the router ID for sample routing.static ExecuteOptionwithSelectedBreakers(Function<List<BreakerMeta>, List<String>> selector) Dynamically selects breakers based on cached metadata.static ExecuteOptionwithSelectedRouter(Function<List<RouterMeta>, String> selector) Dynamically selects a router based on cached metadata.static ExecuteOptionAdds a single tag.static ExecuteOptionAdds multiple tags.static ExecuteOptionwithTraceId(String traceId) Sets a specific trace ID.
-
Field Details
-
CONTRACT_VERSION
SDK Contract version.- See Also:
-
LATENCY
Sentinel value for automatic latency measurement in metrics.
-
-
Method Details
-
withBreakers
Specifies breaker names to check before executing. -
withSelectedBreakers
Dynamically selects breakers based on cached metadata. -
withRouter
Specifies the router ID for sample routing. -
withSelectedRouter
Dynamically selects a router based on cached metadata. -
withMetrics
-
withDeferredMetrics
Registers a function to extract metrics from the task result after execution. -
withTag
Adds a single tag. -
withTags
Adds multiple tags. -
withIgnoreErrors
Specifies exception classes that should not count as failures. -
withErrorEvaluator
Custom predicate to determine if an exception is a failure. Return true = failure. -
withTraceId
Sets a specific trace ID. -
execute
Wraps a task with circuit breaker logic.- Type Parameters:
T- the return type- Parameters:
task- the task to executeoptions- per-call options- Returns:
- the task result
- Throws:
BreakerOpenException- if a breaker is open
-
report
Fire-and-forget sample reporting. -
getState
Returns the cached state for a single breaker, or null. -
getAllStates
Returns a copy of all cached breaker states. -
stats
Returns SDK health metrics snapshot. -
getStatus
Fetches project health status from the API. -
getBreakersMetadata
Returns a deep copy of cached breaker metadata, or null if not yet loaded. -
getRoutersMetadata
Returns a deep copy of cached router metadata, or null if not yet loaded. -
listBreakersMetadata
Lists breaker metadata with ETag support.- Throws:
IOException
-
listRoutersMetadata
Lists router metadata with ETag support.- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
close
Gracefully shuts down, flushing buffered samples within the timeout. -
builder
-