This document describes how to capture custom client metrics using OpenTelemetry. Custom client metrics are available using the Java and Go client libraries.
Custom client-side metrics can help you find the source of latency in your system. For more information, see Latency points in a Spanner request.
Spanner client libraries also provide statistics and traces using the OpenTelemetry observability framework. For more information, see Set up trace collection using OpenTelemetry.
OpenTelemetry is an open source observability framework and toolkit that lets you create and manage telemetry data such as traces, metrics, and logs.
Before you begin
You need to configure the OpenTelemetry SDK with appropriate options for exporting your telemetry data. We recommend using the OpenTelemetry Protocol (OTLP) exporter.
To set up custom client-side metrics using OpenTelemetry, you need to configure the OpenTelemetry SDK and OTLP exporter:
Add the necessary dependencies to your application using the following code:
Java
Go
Create an OpenTelemetry object with the OTLP exporter and inject it into Spanner using
SpannerOptions
:Java
Go
Capture GFE latency
Google Front End (GFE) latency is the duration in milliseconds between when the Google network receives a remote procedure call from the client and when the GFE receives the first byte of the response.
You can capture the GFE latency using the following code:
Java
Go
The code sample appends the string spanner/gfe_latency
to the metric name when
it's exported to Cloud Monitoring. You can search for this metric on
Cloud Monitoring using the appended string.
Capture Cloud Spanner API request latency
Cloud Spanner API request latency is the time in seconds between the first byte of client request that the Cloud Spanner API frontend receives and the last byte of response that the Cloud Spanner API frontend sends.
This latency metric is available as part of Cloud Monitoring metrics.
Capture client round-trip latency
Client round-trip latency is the duration in milliseconds between the first byte of the Cloud Spanner API request that the client sends to the database (through both the GFE and the Cloud Spanner API frontend), and the last byte of response that the client receives from the database.
The Spanner client round-trip latency metric is not supported using OpenTelemetry. You can view the operation latency client-side metric instead. For more information, see Client-side metric descriptions.
You can also instrument the metric using OpenCensus with a bridge and migrate the data to OpenTelemetry.
Capture query latency
Query latency is the duration in milliseconds to run SQL queries in the Spanner database.
You can capture query latency using the following code:
Java
Go
The code sample appends the string spanner/query_stats_elapsed
to the metric name when
it's exported to Cloud Monitoring. You can search for this metric on
Cloud Monitoring using the appended string.
View metrics in the Metrics Explorer
In the Google Cloud console, go to the Metrics Explorer page.
Select your project.
Click Select a metric.
Search for a latency metrics using the following strings:
roundtrip_latency
: for the client round-trip latency metric.spanner/gfe_latency
: for the GFE latency metric.spanner/query_stats_elapsed
: for the query latency metric.
Select the metric, then click Apply.
For more information on grouping or aggregating your metric, see Build queries using menus.
What's next
- Learn more about OpenTelemetry.
- Learn how to configure the OpenTelemetry SDK.
- Learn how to migrate to OpenTelemetry.
- Learn how to use metrics to diagnose latency.