-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created builtin metrics tracer factory:
- Loading branch information
1 parent
3b83448
commit 7c17432
Showing
12 changed files
with
198 additions
and
169 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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
/* | ||
* 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.tracing.MethodName; | ||
import com.google.api.gax.tracing.MetricsTracer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class BuiltInMetricsTracer extends MetricsTracer { | ||
|
||
private final BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder; | ||
// These are RPC specific attributes and pertain to a specific API Trace | ||
private final Map<String, String> attributes = new HashMap<>(); | ||
|
||
public BuiltInMetricsTracer( | ||
MethodName methodName, BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder) { | ||
super(methodName, builtInOpenTelemetryMetricsRecorder); | ||
this.builtInOpenTelemetryMetricsRecorder = builtInOpenTelemetryMetricsRecorder; | ||
this.attributes.put(METHOD_ATTRIBUTE, methodName.toString()); | ||
this.attributes.put(LANGUAGE_ATTRIBUTE, DEFAULT_LANGUAGE); | ||
} | ||
|
||
public void gfeCapture(double gfeLatency) { | ||
this.builtInOpenTelemetryMetricsRecorder.recordGFELatency(gfeLatency, this.attributes); | ||
} | ||
|
||
@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); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInMetricsTracerFactory.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,66 @@ | ||
/* | ||
* 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.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.api.gax.tracing.ApiTracer; | ||
import com.google.api.gax.tracing.ApiTracerFactory; | ||
import com.google.api.gax.tracing.MethodName; | ||
import com.google.api.gax.tracing.MetricsRecorder; | ||
import com.google.api.gax.tracing.MetricsTracer; | ||
import com.google.api.gax.tracing.MetricsTracerFactory; | ||
import com.google.api.gax.tracing.SpanName; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link ApiTracerFactory} to build instances of {@link MetricsTracer}. | ||
* | ||
* <p>This class wraps the {@link MetricsRecorder} and pass it to {@link MetricsTracer}. It will be | ||
* used to record metrics in {@link MetricsTracer}. | ||
* | ||
* <p>This class is expected to be initialized once during client initialization. | ||
*/ | ||
@BetaApi | ||
@InternalApi | ||
public class BuiltInMetricsTracerFactory extends MetricsTracerFactory { | ||
|
||
protected BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder; | ||
private final Map<String, String> attributes; | ||
|
||
/** | ||
* Pass in a Map of client level attributes which will be added to every single MetricsTracer | ||
* created from the ApiTracerFactory. | ||
*/ | ||
public BuiltInMetricsTracerFactory( | ||
BuiltInMetricsRecorder builtInOpenTelemetryMetricsRecorder, Map<String, String> attributes) { | ||
super(builtInOpenTelemetryMetricsRecorder, attributes); | ||
this.builtInOpenTelemetryMetricsRecorder = builtInOpenTelemetryMetricsRecorder; | ||
this.attributes = ImmutableMap.copyOf(attributes); | ||
} | ||
|
||
@Override | ||
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { | ||
BuiltInMetricsTracer metricsTracer = | ||
new BuiltInMetricsTracer( | ||
MethodName.of(spanName.getClientName(), spanName.getMethodName()), | ||
builtInOpenTelemetryMetricsRecorder); | ||
attributes.forEach(metricsTracer::addAttributes); | ||
return metricsTracer; | ||
} | ||
} |
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
Oops, something went wrong.