MetricsTracer

MetricsTracer

MetricsTracer is responsible for recording and managing metrics related to gRPC Spanner operations and attempts counters, and latencies, as well as Google Front End (GFE)/AFE metrics such as latency and connectivity errors.

This class provides methods to record the start and completion of operations and attempts, extract GFE/AFE latency from response headers. It also handles setting of required Spanner metric attributes to be later consumed by the SpannerMetricsExporter.

Constructor

new MetricsTracer(_instrumentAttemptCounter, _instrumentAttemptLatency, _instrumentOperationCounter, _instrumentOperationLatency, _instrumentGfeConnectivityErrorCount, _instrumentGfeLatency, _instrumentAfeConnectivityErrorCount, _instrumentAfeLatency, enabled)

Constructs a new MetricsTracer.

Parameters:
Name Type Description
_instrumentAttemptCounter

Counter for attempt count metrics.

_instrumentAttemptLatency

Histogram for attempt latency metrics.

_instrumentOperationCounter

Counter for operation count metrics.

_instrumentOperationLatency

Histogram for operation latency metrics.

_instrumentGfeConnectivityErrorCount

Counter for GFE connectivity errors.

_instrumentGfeLatency

Histogram for GFE latency metrics.

_instrumentAfeConnectivityErrorCount

Counter for AFE connectivity errors.

_instrumentAfeLatency

Histogram for AFE latency metrics.

enabled

Whether metrics recording is enabled.

Members

_attributesByStatus

Cached OTel attribute objects keyed by status name, avoiding repeated object allocations on every metric recording call.

_clientAttributes

Stores client and resource attributes for labeling metrics.

clientAttributes

Gets the current client and resource attributes for metrics.

currentOperation

The current MetricOperationTracer instance tracking the ongoing operation.

instrumentAttemptCounter

Gets the attempt counter OTEL instrument.

instrumentAttemptLatency

Gets the attempt latency histogram OTEL instrument.

instrumentOperationCounter

Gets the operation counter OTEL instrument.

instrumentOperationLatency

Gets the operation latency histogram OTEL instrument.

Methods

_createAttemptOtelAttributes()

Creates and returns a set of OTEL attributes for attempt-level metrics. The overall operation status is set at this time based on the last attempt's status.

Returns:
Type Description

The attempt attributes object.

_createOperationOtelAttributes()

Creates and returns a set of OTEL attributes for operation-level metrics.

Returns:
Type Description

The operation attributes object.

_getAttributesForStatus(status)

Returns a cached set of OTEL attributes for the given status, avoiding repeated object cloning on every metric record/add call.

Parameters:
Name Type Description
status

Optional status code (as numeric Status enum or status name string).

Returns:
Type Description

The cached attributes object.

_getMillisecondTimeDifference(start, end)

Returns the difference in milliseconds between two times.

Parameters:
Name Type Description
start

The start time.

end

The end time.

Returns:
Type Description

The time difference in milliseconds.

extractAfeLatency(header)

Extracts the AFE latency value (in milliseconds) from a 'server-timing' header string. Returns null if the header is missing or does not contain a valid latency value.

Parameters:
Name Type Description
header

The 'server-timing' header string.

Returns:
Type Description

The extracted AFE latency in milliseconds, or null if not found.

extractGfeLatency(header)

Extracts the GFE latency value (in milliseconds) from a 'server-timing' header string. Returns null if the header is missing or does not contain a valid latency value.

Parameters:
Name Type Description
header

The 'server-timing' header string.

Returns:
Type Description

The extracted GFE latency in milliseconds, or null if not found.

recordAfeConnectivityErrorCount()

Increments the AFE connectivity error count metric.

recordAfeLatency(statusCode)

Records the AFE latency for this attempt.

Parameters:
Name Type Description
statusCode

The gRPC status code of the attempt.

recordAttemptCompletion(status)

Records the completion of the current attempt, including its status and latency. These statuses code are defined in grpc.status

Parameters:
Name Type Description
status

The status code of the attempt (default: Status.OK).

recordAttemptStart()

Records the start of a new attempt within the current operation. Increments the attempt count and creates a new MetricAttemptTracer.

recordGfeConnectivityErrorCount()

Increments the GFE connectivity error count metric.

recordGfeLatency(statusCode)

Records the GFE latency for this attempt.

Parameters:
Name Type Description
statusCode

The gRPC status code of the attempt.

recordOperationCompletion()

Records the completion of the current operation, including its status, latency, and attempt count. Also clears the current tracer from the factory.

recordOperationStart()

Records the start of a new operation, resetting the operation tracer and start time.