-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding gfe_latencies metric to built-in metrics (#3490)
- Loading branch information
1 parent
4a1f99c
commit 314dadc
Showing
15 changed files
with
549 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner; | ||
|
||
import com.google.api.gax.core.GaxProperties; | ||
import com.google.api.gax.tracing.OpenTelemetryMetricsRecorder; | ||
import com.google.common.base.Preconditions; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.api.metrics.DoubleHistogram; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import java.util.Map; | ||
|
||
/** | ||
* Implementation for recording built in metrics. | ||
* | ||
* <p>This class extends the {@link OpenTelemetryMetricsRecorder} which implements the * | ||
* measurements related to the lifecyle of an RPC. | ||
*/ | ||
class BuiltInMetricsRecorder extends OpenTelemetryMetricsRecorder { | ||
|
||
private final DoubleHistogram gfeLatencyRecorder; | ||
|
||
/** | ||
* Creates the following instruments for the following metrics: | ||
* | ||
* <ul> | ||
* <li>GFE Latency: Histogram | ||
* </ul> | ||
* | ||
* @param openTelemetry OpenTelemetry instance | ||
* @param serviceName Service Name | ||
*/ | ||
BuiltInMetricsRecorder(OpenTelemetry openTelemetry, String serviceName) { | ||
super(openTelemetry, serviceName); | ||
Meter meter = | ||
openTelemetry | ||
.meterBuilder(BuiltInMetricsConstant.SPANNER_METER_NAME) | ||
.setInstrumentationVersion(GaxProperties.getLibraryVersion(getClass())) | ||
.build(); | ||
this.gfeLatencyRecorder = | ||
meter | ||
.histogramBuilder(serviceName + '/' + BuiltInMetricsConstant.GFE_LATENCIES_NAME) | ||
.setDescription( | ||
"Latency between Google's network receiving an RPC and reading back the first byte of the response") | ||
.setUnit("ms") | ||
.build(); | ||
} | ||
|
||
/** | ||
* Record the latency between Google's network receiving an RPC and reading back the first byte of | ||
* the response. Data is stored in a Histogram. | ||
* | ||
* @param gfeLatency Attempt Latency in ms | ||
* @param attributes Map of the attributes to store | ||
*/ | ||
void recordGFELatency(double gfeLatency, Map<String, String> attributes) { | ||
gfeLatencyRecorder.record(gfeLatency, toOtelAttributes(attributes)); | ||
} | ||
|
||
Attributes toOtelAttributes(Map<String, String> attributes) { | ||
Preconditions.checkNotNull(attributes, "Attributes map cannot be null"); | ||
AttributesBuilder attributesBuilder = Attributes.builder(); | ||
attributes.forEach(attributesBuilder::put); | ||
return attributesBuilder.build(); | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner; | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.api.gax.rpc.StatusCode; | ||
import com.google.api.gax.tracing.ApiTracer; | ||
import com.google.api.gax.tracing.MethodName; | ||
import com.google.api.gax.tracing.MetricsTracer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CancellationException; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Implements built-in metrics tracer. | ||
* | ||
* <p>This class extends the {@link MetricsTracer} which computes generic metrics that can be | ||
* observed in the lifecycle of an RPC operation. | ||
*/ | ||
class BuiltInMetricsTracer extends MetricsTracer implements ApiTracer { | ||
|
||
private final BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder; | ||
// These are RPC specific attributes and pertain to a specific API Trace | ||
private final Map<String, String> attributes = new HashMap<>(); | ||
|
||
private Long gfeLatency = null; | ||
|
||
BuiltInMetricsTracer( | ||
MethodName methodName, BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder) { | ||
super(methodName, builtInOpenTelemetryMetricsRecorder); | ||
this.builtInOpenTelemetryMetricsRecorder = builtInOpenTelemetryMetricsRecorder; | ||
this.attributes.put(METHOD_ATTRIBUTE, methodName.toString()); | ||
} | ||
|
||
/** | ||
* Adds an annotation that the attempt succeeded. Successful attempt add "OK" value to the status | ||
* attribute key. | ||
*/ | ||
@Override | ||
public void attemptSucceeded() { | ||
super.attemptSucceeded(); | ||
if (gfeLatency != null) { | ||
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.OK.toString()); | ||
builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); | ||
} | ||
} | ||
|
||
/** | ||
* Add an annotation that the attempt was cancelled by the user. Cancelled attempt add "CANCELLED" | ||
* to the status attribute key. | ||
*/ | ||
@Override | ||
public void attemptCancelled() { | ||
super.attemptCancelled(); | ||
if (gfeLatency != null) { | ||
attributes.put(STATUS_ATTRIBUTE, StatusCode.Code.CANCELLED.toString()); | ||
builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); | ||
} | ||
} | ||
|
||
/** | ||
* Adds an annotation that the attempt failed, but another attempt will be made after the delay. | ||
* | ||
* @param error the error that caused the attempt to fail. | ||
* @param delay the amount of time to wait before the next attempt will start. | ||
* <p>Failed attempt extracts the error from the throwable and adds it to the status attribute | ||
* key. | ||
*/ | ||
@Override | ||
public void attemptFailedDuration(Throwable error, java.time.Duration delay) { | ||
super.attemptFailedDuration(error, delay); | ||
if (gfeLatency != null) { | ||
attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); | ||
builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); | ||
} | ||
} | ||
|
||
/** | ||
* Adds an annotation that the attempt failed and that no further attempts will be made because | ||
* retry limits have been reached. This extracts the error from the throwable and adds it to the | ||
* status attribute key. | ||
* | ||
* @param error the last error received before retries were exhausted. | ||
*/ | ||
@Override | ||
public void attemptFailedRetriesExhausted(Throwable error) { | ||
super.attemptFailedRetriesExhausted(error); | ||
if (gfeLatency != null) { | ||
attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); | ||
builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); | ||
} | ||
} | ||
|
||
/** | ||
* Adds an annotation that the attempt failed and that no further attempts will be made because | ||
* the last error was not retryable. This extracts the error from the throwable and adds it to the | ||
* status attribute key. | ||
* | ||
* @param error the error that caused the final attempt to fail. | ||
*/ | ||
@Override | ||
public void attemptPermanentFailure(Throwable error) { | ||
super.attemptPermanentFailure(error); | ||
if (gfeLatency != null) { | ||
attributes.put(STATUS_ATTRIBUTE, extractStatus(error)); | ||
builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, attributes); | ||
} | ||
} | ||
|
||
void recordGFELatency(Long gfeLatency) { | ||
this.gfeLatency = gfeLatency; | ||
} | ||
|
||
@Override | ||
public void addAttributes(Map<String, String> attributes) { | ||
super.addAttributes(attributes); | ||
this.attributes.putAll(attributes); | ||
}; | ||
|
||
@Override | ||
public void addAttributes(String key, String value) { | ||
super.addAttributes(key, value); | ||
this.attributes.put(key, value); | ||
} | ||
|
||
private static String extractStatus(@Nullable Throwable error) { | ||
final String statusString; | ||
|
||
if (error == null) { | ||
return StatusCode.Code.OK.toString(); | ||
} else if (error instanceof CancellationException) { | ||
statusString = StatusCode.Code.CANCELLED.toString(); | ||
} else if (error instanceof ApiException) { | ||
statusString = ((ApiException) error).getStatusCode().getCode().toString(); | ||
} else { | ||
statusString = StatusCode.Code.UNKNOWN.toString(); | ||
} | ||
|
||
return statusString; | ||
} | ||
} |
Oops, something went wrong.