-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce interfaces for metrics instrumentation (#2403)
This PR introduces new public interfaces and refactors existing interfaces for metrics instrumentation. All the interfaces are framework-agnostic. Implementation of the interfaces should use an observability framework like OpenTelemetry.
- Loading branch information
Showing
8 changed files
with
287 additions
and
19 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
65 changes: 65 additions & 0 deletions
65
gax-java/gax/src/main/java/com/google/api/gax/tracing/MethodName.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,65 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.tracing; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.api.gax.rpc.StubSettings; | ||
import com.google.auto.value.AutoValue; | ||
|
||
/** A value class to represent the name of the RPC method in an {@link ApiTracer}. */ | ||
@BetaApi | ||
@InternalApi | ||
@AutoValue | ||
public abstract class MethodName { | ||
/** | ||
* Creates a new instance of the RPC method name. | ||
* | ||
* @param serviceName The name of the service. In general this will be GAPIC generated service | ||
* name {@link StubSettings#getServiceName()}. However, in some cases, when the GAPIC | ||
* generated service is wrapped, this will be overridden to specify the manually written | ||
* wrapper's name. | ||
* @param methodName The name of the logical operation being traced. | ||
*/ | ||
public static MethodName of(String serviceName, String methodName) { | ||
return new AutoValue_MethodName(serviceName, methodName); | ||
} | ||
|
||
/** The name of the service. ie BigtableData */ | ||
public abstract String getServiceName(); | ||
|
||
/** The name of the logical operation being traced. ie. ReadRows. */ | ||
public abstract String getMethodName(); | ||
|
||
@Override | ||
public String toString() { | ||
return getServiceName() + "." + getMethodName(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsRecorder.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,61 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.google.api.gax.tracing; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provides an interface for metrics recording. The implementer is expected to use an observability | ||
* framework, e.g. OpenTelemetry. There should be only one instance of MetricsRecorder per client, | ||
* all the methods in this class are expected to be called from multiple threads, hence the | ||
* implementation must be thread safe. | ||
*/ | ||
@BetaApi | ||
@InternalApi | ||
public interface MetricsRecorder { | ||
|
||
/** Records the latency of an RPC attempt */ | ||
default void recordAttemptLatency(double attemptLatency, Map<String, String> attributes) {} | ||
|
||
/** Records the count of RPC attempts */ | ||
default void recordAttemptCount(long count, Map<String, String> attributes) {} | ||
|
||
/** | ||
* Records the total end-to-end latency for an operation, including the initial RPC attempts and | ||
* subsequent retries. | ||
*/ | ||
default void recordOperationLatency(double operationLatency, Map<String, String> attributes) {} | ||
|
||
/** Records the count of operations */ | ||
default void recordOperationCount(long count, Map<String, String> attributes) {} | ||
} |
53 changes: 53 additions & 0 deletions
53
gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracer.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 | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.google.api.gax.tracing; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
|
||
/** | ||
* This class computes generic metrics that can be observed in the lifecycle of an RPC operation. | ||
* The responsibility of recording metrics should delegate to {@link MetricsRecorder}, hence this | ||
* class should not have any knowledge about the observability framework used for metrics recording. | ||
*/ | ||
@BetaApi | ||
@InternalApi | ||
public class MetricsTracer implements ApiTracer { | ||
|
||
public MetricsTracer(MethodName methodName, MetricsRecorder metricsRecorder) {} | ||
|
||
/** | ||
* Add attributes that will be attached to all metrics. This is expected to be called by | ||
* handwritten client teams to add additional attributes that are not supposed be collected by | ||
* Gax. | ||
*/ | ||
public void addAttributes(String key, String value) {}; | ||
} |
57 changes: 57 additions & 0 deletions
57
gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracerFactory.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,57 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.tracing; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
|
||
/** | ||
* 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 MetricsTracerFactory implements ApiTracerFactory { | ||
protected MetricsRecorder metricsRecorder; | ||
|
||
public MetricsTracerFactory(MetricsRecorder metricsRecorder) { | ||
this.metricsRecorder = metricsRecorder; | ||
} | ||
|
||
@Override | ||
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) { | ||
return new MetricsTracer( | ||
MethodName.of(spanName.getClientName(), spanName.getMethodName()), metricsRecorder); | ||
} | ||
} |
Oops, something went wrong.