Skip to content

perf: cache the key used for OTEL traces and metrics #3814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.54.0</version>
<version>26.57.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class HeaderInterceptor implements ClientInterceptor {
CacheBuilder.newBuilder().maximumSize(1000).build();
private final Cache<String, Map<String, String>> builtInAttributesCache =
CacheBuilder.newBuilder().maximumSize(1000).build();
private final Cache<DatabaseName, Cache<String, String>> keyCache =
CacheBuilder.newBuilder().maximumSize(1000).build();

// Get the global singleton Tagger object.
private static final Tagger TAGGER = Tags.getTagger();
Expand Down Expand Up @@ -116,7 +118,7 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
try {
Span span = Span.current();
DatabaseName databaseName = extractDatabaseName(headers);
String key = databaseName + method.getFullMethodName();
String key = extractKey(databaseName, method.getFullMethodName());
TagContext tagContext = getTagContext(key, method.getFullMethodName(), databaseName);
Attributes attributes =
getMetricAttributes(key, method.getFullMethodName(), databaseName);
Expand Down Expand Up @@ -201,6 +203,13 @@ private Map<String, Long> parseServerTimingHeader(String serverTiming) {
return serverTimingMetrics;
}

private String extractKey(DatabaseName databaseName, String methodName)
throws ExecutionException {
Cache<String, String> keys =
keyCache.get(databaseName, () -> CacheBuilder.newBuilder().maximumSize(1000).build());
return keys.get(methodName, () -> databaseName + methodName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do you feel if there will be an improvement if we use StringBuilder instead of string concatenation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java automatically uses StringBuilder for string concatenation, so it does not make any difference. See https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

}

private DatabaseName extractDatabaseName(Metadata headers) throws ExecutionException {
String googleResourcePrefix = headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY);
if (googleResourcePrefix != null) {
Expand Down
Loading