Class TripSwitch

java.lang.Object
dev.tripswitch.TripSwitch
All Implemented Interfaces:
AutoCloseable

public class TripSwitch extends Object implements 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();
 
  • Field Details

    • CONTRACT_VERSION

      public static final String CONTRACT_VERSION
      SDK Contract version.
      See Also:
    • LATENCY

      public static final Object LATENCY
      Sentinel value for automatic latency measurement in metrics.
  • Method Details

    • withBreakers

      public static ExecuteOption withBreakers(String... names)
      Specifies breaker names to check before executing.
    • withSelectedBreakers

      public static ExecuteOption withSelectedBreakers(Function<List<BreakerMeta>,List<String>> selector)
      Dynamically selects breakers based on cached metadata.
    • withRouter

      public static ExecuteOption withRouter(String routerId)
      Specifies the router ID for sample routing.
    • withSelectedRouter

      public static ExecuteOption withSelectedRouter(Function<List<RouterMeta>,String> selector)
      Dynamically selects a router based on cached metadata.
    • withMetrics

      public static ExecuteOption withMetrics(Map<String,Object> metrics)
      Sets metrics to report. Values can be:
      • LATENCY — auto-computed task duration in ms
      • Supplier<Double> — called after task completes
      • Number — static value
    • withDeferredMetrics

      public static <T> ExecuteOption withDeferredMetrics(BiFunction<T,Exception,Map<String,Double>> fn)
      Registers a function to extract metrics from the task result after execution.
    • withTag

      public static ExecuteOption withTag(String key, String value)
      Adds a single tag.
    • withTags

      public static ExecuteOption withTags(Map<String,String> tags)
      Adds multiple tags.
    • withIgnoreErrors

      @SafeVarargs public static ExecuteOption withIgnoreErrors(Class<? extends Exception>... types)
      Specifies exception classes that should not count as failures.
    • withErrorEvaluator

      public static ExecuteOption withErrorEvaluator(Predicate<Exception> evaluator)
      Custom predicate to determine if an exception is a failure. Return true = failure.
    • withTraceId

      public static ExecuteOption withTraceId(String traceId)
      Sets a specific trace ID.
    • execute

      public <T> T execute(Callable<T> task, ExecuteOption... options)
      Wraps a task with circuit breaker logic.
      Type Parameters:
      T - the return type
      Parameters:
      task - the task to execute
      options - per-call options
      Returns:
      the task result
      Throws:
      BreakerOpenException - if a breaker is open
    • report

      public void report(ReportInput input)
      Fire-and-forget sample reporting.
    • getState

      public BreakerStatus getState(String name)
      Returns the cached state for a single breaker, or null.
    • getAllStates

      public Map<String,BreakerStatus> getAllStates()
      Returns a copy of all cached breaker states.
    • stats

      public SDKStats stats()
      Returns SDK health metrics snapshot.
    • getStatus

      public Status getStatus()
      Fetches project health status from the API.
    • getBreakersMetadata

      public List<BreakerMeta> getBreakersMetadata()
      Returns a deep copy of cached breaker metadata, or null if not yet loaded.
    • getRoutersMetadata

      public List<RouterMeta> getRoutersMetadata()
      Returns a deep copy of cached router metadata, or null if not yet loaded.
    • listBreakersMetadata

      public MetadataResponse<BreakerMeta> listBreakersMetadata(String etag) throws IOException
      Lists breaker metadata with ETag support.
      Throws:
      IOException
    • listRoutersMetadata

      public MetadataResponse<RouterMeta> listRoutersMetadata(String etag) throws IOException
      Lists router metadata with ETag support.
      Throws:
      IOException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • close

      public void close(Duration timeout)
      Gracefully shuts down, flushing buffered samples within the timeout.
    • builder

      public static TripSwitch.Builder builder(String projectId)