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:
| Platform | Source |
|---|---|
| AWS Lambda | AWS_LAMBDA_LOG_STREAM_NAME |
| ECS | ECS_CONTAINER_METADATA_URI_V4 |
| Kubernetes | KUBERNETES_SERVICE_HOST + pod name from /etc/hostname |
| Google Cloud Run | K_SERVICE + K_REVISION |
| Google Cloud Run Jobs | CLOUD_RUN_JOB + CLOUD_RUN_EXECUTION + CLOUD_RUN_TASK_INDEX |
| Azure Container Apps | CONTAINER_APP_NAME + CONTAINER_APP_REVISION + CONTAINER_APP_REPLICA_NAME |
| Azure App Service | WEBSITE_INSTANCE_ID |
| Azure Functions | WEBSITE_INSTANCE_ID |
| Fly.io | FLY_ALLOC_ID |
| Railway | RAILWAY_REPLICA_ID |
| Render | RENDER_INSTANCE_ID |
| Heroku | DYNO |
| Fallback | hostname-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:
| Setting | Default | When to change |
|---|---|---|
windowSizeMs | 10000 | Increase for low-traffic breakers. Decrease for real-time dashboards. |
flushIntervalMs | 15000 | Decrease for faster metrics visibility. Increase to reduce API calls. |
maxLatencySamples | 1000 | Increase for more accurate percentiles under high throughput. |
enabled | true | Set 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.
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')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.