Metrics & Support Utilities

Record custom FHIR operations and use platform-provided retry, backoff, and chunking utilities.

Injection

@InjectMetrics
ScriptMetrics metrics

@InjectSupport
ScriptSupport support

Injected services are shared across hook invocations within the same script generation. Use the runtime-provided instances rather than constructing platform services manually.

ScriptMetrics

The platform already records core processing and normal Bundle delivery metrics. ScriptMetrics is intended for script logic that performs additional FHIR operations outside the standard Java delivery path.

Bundle postConvert(MessageEnvelope envelope, Bundle bundle) {
  try {
    // Additional script-side FHIR operation
    fhir.postTransaction(customBundle)
    metrics.onFhirBundlePosted()
  } catch (Exception e) {
    metrics.onFhirRequestFailed()
    throw e
  }

  return bundle
}
Do not double-count standard delivery

FireSonic records normal Bundle delivery itself. Use ScriptMetrics only for additional FHIR requests initiated directly by script logic, and record failure metrics before rethrowing errors from those custom operations.

ScriptSupport

ScriptSupport provides small reusable utilities for retry logic, sleep/backoff, and list chunking so workflow code stays concise and consistent.

MethodPurpose
sleepMillis(millis)Sleep/backoff helper.
withRetries(label, delays, action)Run an action with retries and return a value.
runWithRetries(label, delays, action)Run a void action with retries.
chunk(items, size)Split a list into chunks.

Retry with return value

def patient = support.withRetries(
  "search patient",
  [1000L, 2000L, 5000L]
) {
  return fhir.findPatientByIdentifier(identifier)
    .orElse(null)
}

Retry void operation

support.runWithRetries(
  "post custom transaction",
  [1000L, 2000L, 5000L]
) {
  fhir.postTransaction(bundle)
}

Apply retries only when the operation is safe to repeat. Retry delays are explicit, which keeps backoff behavior visible in the workflow code.

Chunking

def resourceChunks = support.chunk(resources, 50)

resourceChunks.each { chunk ->
  LOG.info("Processing chunk size=" + chunk.size())
}

Chunking is useful when a workflow must process or send large collections in controlled batches.