Skip to main content

How can I send metrics to a named output

· 2 min read
Jeremy Scott
Co-founder

Opscotch 3.1.1 expands bootstrap workflow.outputs so you can define additional named outputs and route metrics to them from workflow JavaScript.

This is a metric-routing feature. In the current runtime, the workflow-facing API added here is context.sendMetric(outputId, ...).

Before 3.1.1, workflows could emit metrics through the default workflow metric output.

In 3.1.1, bootstrap configuration can also define additional named outputs under workflow.outputs, and workflow JavaScript can target one of those outputs by id:

context.sendMetric("fast-otel", "metric.name", 1.25);

Configure Named Outputs

Named outputs live in the bootstrap, not the remote workflow config.

{
"deploymentId": "demo",
"remoteConfiguration": "config.json",
"workflow": {
"metricOutput": {
"type": "otel",
"outputUrl": "https://collector-default:4317"
},
"outputs": [
{
"id": "primary-metrics",
"type": "otel",
"routingToken": "primary",
"outputUrl": "https://collector-primary:4317",
"typeProperties": {
"otel": {
"protocol": "grpc"
}
}
},
{
"id": "audit-metrics",
"type": "otel",
"routingToken": "audit",
"outputUrl": "https://collector-audit:4317",
"typeProperties": {
"otel": {
"protocol": "grpc"
}
}
}
]
}
}

Important details from the schema and bootstrap loader:

  • workflow.outputs is an array of Output objects.
  • Each named output must have a unique non-empty id.
  • Disabled outputs are skipped.
  • The runtime wires named outputs at bootstrap load time.

Route Metrics to a Named Output

The runtime adds these named-output metric overloads on JavascriptContext:

  • context.sendMetric(outputId, key, value)
  • context.sendMetric(outputId, timestamp, key, value)
  • context.sendMetric(outputId, key, value, metadata)

Examples:

// Default workflow metric output
context.sendMetric("request_count", 1);

// Specific named output
context.sendMetric("primary-metrics", "request_count", 1);

// Specific named output with metadata
context.sendMetric("audit-metrics", "security_event", 1, {
type: "login",
userId: "user123"
});

The value is still numeric. Metadata is the optional fourth argument, not the third.

What Happens at Runtime

When you call context.sendMetric(outputId, ...), Opscotch:

  1. Looks up the named output by id from the bootstrap-loaded registry.
  2. Uses that output's configured sender.
  3. Uses the output's routingToken when present, otherwise the current deployment id.

If the output id is not found, the runtime logs a warning and drops that routed metric.

Use Cases

Separate Audit Metrics

context.sendMetric("audit-metrics", "security_event", 1, {
action: "password_reset"
});

Explicit Timestamp Routing

context.sendMetric("primary-metrics", context.getTimestamp(), "latency_ms", latency);

Limits

This post should stay narrow about what is implemented today:

  • Named outputs are configured in bootstrap workflow.outputs.
  • The workflow API added here is metric routing through context.sendMetric(outputId, ...).
  • The current AuthenticationJavascriptContext implementation does not support sendMetric(...).

If you need named-output examples, keep them in normal workflow JavaScript contexts rather than authentication flows.