Skip to main content

Function: createCounter()

@arolariu/website


@arolariu/website / instrumentation.server / createCounter

Function: createCounter()

createCounter(name, description?, unit?): Counter<Attributes>

Defined in: instrumentation.server.ts:1151

Create a counter metric instrument.

Counters are monotonically increasing values used to track totals. They can only go up (or reset to zero). Use for counting events or operations.

Parameters

name

MetricName

Metric name following semantic conventions (e.g., "http.server.requests")

description?

string

Human-readable description of what the metric measures

unit?

string

Optional unit of measurement (e.g., "1", "requests", "bytes")

Returns

Counter<Attributes>

Counter instrument for recording values with type-safe attributes

Remarks

  • Counter values should only increase
  • Use add() with positive values only
  • Include units in the name (e.g., "requests", "bytes", "errors")
  • Keep attribute cardinality low to avoid metric explosion
  • Counters are aggregated as sums in backends

Examples

const requestCounter = createCounter(
'http.server.requests',
'Total number of HTTP requests received',
'1'
);

// Record a request with type-safe attributes
requestCounter.add(1, {
'http.method': 'GET',
'http.status_code': 200,
'next.render_context': 'api',
});
// User action counter
const userActionCounter = createCounter('user.actions', 'Total user actions');
userActionCounter.add(1, {
'user.authenticated': true,
'user.role': 'admin',
});

See

// was this page useful?