Skip to content

Commit 044fbdd

Browse files
committed
add ref to docs
1 parent 3b9e3a1 commit 044fbdd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docs/ts/observability/metrics.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ function processJob(jobType: string, priority: number) {
183183
}
184184
```
185185

186+
## Metric references
187+
188+
Encore uses static analysis to determine which services are using each metric, and what operations each service is performing.
189+
190+
This means metric objects can't be passed around however you like, as it makes static analysis impossible in many cases. To simplify your workflow, given these restrictions, Encore supports defining a "reference" to a metric that can be passed around any way you want.
191+
192+
To create a reference, call the `.ref()` method on any metric:
193+
194+
```typescript
195+
import { Counter } from "encore.dev/metrics";
196+
197+
export const ordersProcessed = new Counter("orders_processed");
198+
199+
// Create a reference that can be passed around
200+
const metricRef = ordersProcessed.ref();
201+
202+
// Pass the reference to other functions
203+
function logMetric(metric: Counter) {
204+
metric.increment();
205+
}
206+
207+
logMetric(metricRef);
208+
```
209+
210+
This works for all metric types (`Counter`, `CounterGroup`, `Gauge`, and `GaugeGroup`).
211+
186212
<Callout type="important">
187213

188214
Each combination of label values creates a unique time series tracked in memory and stored by the monitoring system.

0 commit comments

Comments
 (0)