MetricsTracerFactory

MetricsTracerFactory

Factory class for creating and managing MetricsTracer instances and OTEL metric instruments.

The MetricsTracerFactory is responsible for:

  • Creating and managing a singleton instance for metrics collection.
  • Initializing and providing OTEL MeterProvider.
  • Generating and storing client-specific metadata (UID, hash, name, location, projectId).
  • Creating and tracking MetricsTracer instances for individual gRPC Spanner operations.
  • Providing utility methods for extracting resource attributes and managing tracers.

This class is designed to be used as a singleton. Metrics collection can be enabled or disabled globally via the static enabled property, that is set from the SpannerClient.

Constructor

new MetricsTracerFactory(projectId)

Private constructor to enforce singleton pattern. Initializes client metadata and detects client location if metrics are enabled. Location will default to global if host machine is not a GCE or GKE instance.

Parameters:
Name Type Description
projectId

The GCP project ID used by the Spanner Client.

Members

clientName

Returns the Client Name.

clientUid

Returns the Client UID.

instrumentAttemptCounter

Returns the attempt counter instrument.

instrumentAttemptLatency

Returns the attempt latency histogram instrument.

instrumentGfeConnectivityErrorCount

Returns the GFE connectivity error count counter instrument.

instrumentGfeLatency

Returns the GFE latency histogram instrument.

instrumentOperationCounter

Returns the operation counter instrument.

instrumentOperationLatency

Returns the operation latency histogram instrument.

location

Returns the detected client location.

Methods

_createMetricInstruments()

Creates and initializes all metric instruments (counters and histograms) for the MeterProvider. Instruments are only created if metrics are enabled.

clearCurrentTracer(requestId)

Removes the MetricsTracer associated with the given request id.

Deprecated: MetricsTracer is carried directly on the call context.

Parameters:
Name Type Description
requestId

The request id of the gRPC call set under 'x-goog-spanner-request-id'.

createMetricsTracer(formattedName, method)

Creates a new MetricsTracer for a given resource name and method, and stores it for later retrieval. Returns null if metrics are disabled.

Parameters:
Name Type Description
formattedName

The formatted resource name (e.g., full database path).

method

The gRPC method name.

Returns:
Type Description

A new MetricsTracer instance or null if metrics are disabled.

getCurrentTracer(requestId)

Retrieves the current MetricsTracer for a given request id.

Deprecated: MetricsTracer is carried directly on the call context.

Parameters:
Name Type Description
requestId

The request id of the gRPC call set under 'x-goog-spanner-request-id'.

Returns:
Type Description

null.

getInstanceAttributes(formattedName)

Takes a formatted name and parses the project, instance, and database.

Parameters:
Name Type Description
formattedName

The formatted resource name (e.g., full database path).

Returns:
Type Description

An object containing project, instance, and database strings.

getMeterProvider(readers)

Returns the MeterProvider, creating it and metric instruments if not already initialized. Client-wide attributes that are known at this time are cached to be provided to all MetricsTracers.

If called again with readers when the existing MeterProvider has none, the provider is recreated with those readers. If readers are already attached, additional readers are ignored.

Parameters:
Name Type Description
readers

Optional array of MetricReader instances to attach to the MeterProvider.

Returns:
Type Description

The OTEL MeterProvider instance.

hasMetricReaders()

Returns true if the MeterProvider is initialized with at least one MetricReader.

(async) resetMeterProvider()

Resets the MeterProvider.

(async, static) _detectClientLocation()

Gets the location (region or zone) of the client, or defaults to "global". Uses the GCP resource detector to detect the location from the environment. Checks cloud.region first and falls back to cloud.availability_zone for zonal GKE clusters where cloud.region is not set.

Returns:
Type Description

The detected location string, or "global" if not found.

(static) _generateClientHash(clientUid)

Generates a 6-digit zero-padded lowercase hexadecimal hash using the 10 most significant bits of a 64-bit hash value.

The primary purpose of this function is to generate a hash value for the client_hash resource label using client_uid metric field. The range of values is chosen to be small enough to keep the cardinality of the Resource targets under control. Note: If at later time the range needs to be increased, it can be done by increasing the value of kPrefixLength to up to 24 bits without changing the format of the returned value.

Parameters:
Name Type Description
clientUid

The client UID string to hash.

Returns:
Type Description

A 6-digit hexadecimal hash string.

(static) _generateClientUId()

Generates a unique identifier for the client_uid metric field. The identifier is composed of a UUID, the process ID (PID), and the hostname.

Returns:
Type Description

A unique string identifier for the client.

(static) getInstance(projectId)

Returns the singleton instance of MetricsTracerFactory. If metrics are disabled, returns null. The instance is created only once, and enabling/disabling metrics can only be done on the initial call.

Parameters:
Name Type Description
projectId

Optional GCP project ID for the factory instantiation. Does nothing for subsequent calls.

Returns:
Type Description

The singleton MetricsTracerFactory instance or null if disabled.

(async, static) resetInstance()

Resets the singleton instance of the MetricsTracerFactory.