Skip to main content
Openfuse
Guides

Production

Graceful shutdown, instance detection, and metrics tuning

Shut down gracefully

Call close() before your process exits to flush pending metrics:

process.on('SIGTERM', async () => {
  await openfuse.close({ timeoutMs: 3000 })
  process.exit(0)
})

The timeoutMs option (default: 5000) limits how long the final metrics flush can take. If your platform gives you 10 seconds for shutdown, set this to 3000-5000ms to leave room for other cleanup.

Instance ID auto-detection

The SDK generates a unique instance ID for metrics deduplication. It detects your platform automatically:

PlatformSource
AWS LambdaAWS_LAMBDA_LOG_STREAM_NAME
ECSECS_CONTAINER_METADATA_URI_V4
KubernetesKUBERNETES_SERVICE_HOST + pod name from /etc/hostname
Google Cloud RunK_SERVICE + K_REVISION
Google Cloud Run JobsCLOUD_RUN_JOB + CLOUD_RUN_EXECUTION + CLOUD_RUN_TASK_INDEX
Azure Container AppsCONTAINER_APP_NAME + CONTAINER_APP_REVISION + CONTAINER_APP_REPLICA_NAME
Azure App ServiceWEBSITE_INSTANCE_ID
Azure FunctionsWEBSITE_INSTANCE_ID
Fly.ioFLY_ALLOC_ID
RailwayRAILWAY_REPLICA_ID
RenderRENDER_INSTANCE_ID
HerokuDYNO
Fallbackhostname-pid-random

Set a custom instance ID

const openfuse = new OpenfuseCloud({
  system: 'payments',
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  instanceId: 'worker-3',
})

Read the resolved instance ID with openfuse.instanceId.

Tune metrics

Default metrics settings come from the server. Override locally:

SettingDefaultWhen to change
windowSizeMs10000Increase for low-traffic breakers. Decrease for real-time dashboards.
flushIntervalMs15000Decrease for faster metrics visibility. Increase to reduce API calls.
maxLatencySamples1000Increase for more accurate percentiles under high throughput.
enabledtrueSet to false in test environments.
const openfuse = new OpenfuseCloud({
  system: 'payments',
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  metrics: {
    flushIntervalMs: 30_000,
  },
})

Use a singleton

Create one client per process. The SDK manages internal caches, timers, and connections that shouldn't be duplicated.

lib/openfuse.ts
import { OpenfuseCloud } from '@openfuseio/sdk'

export const openfuse = new OpenfuseCloud({
  system: 'payments',
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
})

openfuse.init()

// Pre-create breaker handles for reuse
export const stripeBreaker = openfuse.breaker('stripe-get-customer')
export const emailBreaker = openfuse.breaker('twilio-send-email')
routes/customers.ts
import { stripeBreaker } from '../lib/openfuse.js'

// Use the pre-created handle directly
const customer = await stripeBreaker.protect(
  (signal) => stripe.customers.retrieve(customerId, { signal }),
  { fallback: () => cachedCustomer },
)

Zero dependencies

The SDK has zero runtime dependencies. It uses built-in Node.js APIs for HTTP, crypto, and timers.

On this page