Skip to main content

Monitoring Interface

TypeScript Support

A monitoring object has an associated TypeScript type:

  • Type Name - Monitoring
  • Package - @resolve-js/core

A monitoring object's interface is the same as the interface of a monitoring adapter except for the getMetrics and clearMetrics methods that require a monitoring adapter ID as an argument.

A monitoring object exposes the following API:

Member NameDescription
errorRegisters an occurred error.
executionRegisters an execution of an operation.
durationRegisters the duration of an operation.
timeStarts execution time measurement.
timeEndEnds execution time measurement and registers the resulting duration.
customRegisters a custom metric.
publishDefined by an implementation, publishes the collected metrics to the intended destination.
rateRegisters operation execution rate during the specified time interval in seconds.
groupCreates a monitoring group.
getMetricsGets a list of collected metrics.
clearMetricsClears the list of collected metrics.

errorā€‹

Registers an occurred error. The default implementation increments the count of the "Errors" metric.

Exampleā€‹

try {
...
} catch (error) {
monitoring.error(error)
}

Argumentsā€‹

Argument NameTypeDescription
errorAn error object.An error to add to monitoring metrics.

executionā€‹

Registers an operation's execution. The default implementation increments the count of the "Executions" metric. The execution method can also be passed an optional error parameter. If this parameter is not null, the function registers the error in metrics.

Exampleā€‹

monitoring.execution()

Argumentsā€‹

Argument NameTypeDescription
error?An error object or nullAn error to add to monitoring metrics.

durationā€‹

Registers the duration of an operation. The default implementation adds the specified value in milliseconds to the "Duration" metric.

Exampleā€‹

monitoring.duration(
'myOperation',
duration / operations.length,
operations.length
)

Argumentsā€‹

Argument NameTypeDescription
labelstringA text label to add to the 'Label' dimension.
durationnumberAn operation duration in milliseconds.
count?numberA number to add to the metric's count. Defaults to 1.

timeā€‹

Starts a timer to measure execution time.

Exampleā€‹

monitoring.time('Execution', startTimestamp)

Argumentsā€‹

Argument NameTypeDescription
namestringThe ID of the started timer.
timestamp?numberA moment in time from which to start counting. Defaults to Date.now().

timeEndā€‹

Ends time measurement and registers the resulting duration. The default implementation adds the measured time value in milliseconds to the "Duration" metric.

Exampleā€‹

monitoring.timeEnd('Execution')

Argumentsā€‹

Argument NameTypeDescription
namestringThe ID of the timer to stop.
timestamp?numberA moment in time at which to stop counting. Defaults to Date.now().

customā€‹

Registers the specified custom metric. If the metric object's value and/or count fields are not specified, the default implementation sets them to 1.

Exampleā€‹

monitoring.custom(myMetric)

Argumentsā€‹

Argument NameTypeDescription
metricDataA custom metric object.Specifies a custom metric's data.

publishā€‹

Defined by an implementation, publishes the collected metrics to the intended destination.

Exampleā€‹

await monitoring.publish()

Argumentsā€‹

Argument NameTypeDescription
options?objectSpecifies additional options for the publish operation.

Resultā€‹

The returned value is a promise that resolves when the monitoring information is successfully published.

The monitoring adapters shipped with reSolve implement the publish function as follows:

Module NameDescription
@resolve-js/monitoring-consolePrints metrics to the text console.
@resolve-js/monitoring-aws-cloudwatchPublishes metrics to AWS CloudWatch.

rateā€‹

Registers operation execution rate during the specified time interval in seconds. The default implementation adds a value in times per N seconds to the specified metric.

Exampleā€‹

monitoring.rate('ReadModelFeedingRate', eventCount, applyDuration / 1000)

Argumentsā€‹

Argument NameTypeDescription
metricNamestringThe name of the metric to add.
countnumberA number to add to the metric's count.
seconds?numberThe number of seconds for which to count the rate. Defaults to 1.

groupā€‹

Creates a monitoring group and returns a monitoring adapter instance for this group.

Exampleā€‹

const groupMonitoring = monitoring.group({ Part: 'ReadModel' })

Argumentsā€‹

Argument NameTypeDescription
configA key-value pair object.A key-value pair that identifies the group.

Resultā€‹

A monitoring object instance that operates on the created group.

getMetricsā€‹

Gets a list of collected metrics.

Exampleā€‹

const metrics = getMetrics('default')
Argument NameTypeDescription
idstringThe metrics adapter ID as specified in the application configuration.

Resultā€‹

The returned value is an array of metric objects.

clearMetricsā€‹

Exampleā€‹

monitoring.clearMetrics('default')

Clear the list of collected metrics.

Argument NameTypeDescription
idstringThe metrics adapter ID as specified in the application configuration.