Skip to content

Commit 381f62c

Browse files
committed
Add Highway hash algorithm for client_hash
1 parent 2e1329e commit 381f62c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/BuiltInOpenTelemetryMetricsProvider.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static com.google.cloud.spanner.BuiltInMetricsConstant.LOCATION_ID_KEY;
2525
import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY;
2626

27+
import com.google.common.hash.HashFunction;
28+
import com.google.common.hash.Hashing;
2729
import com.google.auth.Credentials;
2830
import com.google.cloud.opentelemetry.detection.AttributeKeys;
2931
import com.google.cloud.opentelemetry.detection.DetectedPlatform;
@@ -83,8 +85,9 @@ Map<String, String> createClientAttributes(String projectId, String client_name)
8385
// TODO: Replace this with real value.
8486
clientAttributes.put(INSTANCE_CONFIG_ID_KEY.getKey(), "unknown");
8587
clientAttributes.put(CLIENT_NAME_KEY.getKey(), client_name);
86-
clientAttributes.put(CLIENT_UID_KEY.getKey(), getDefaultTaskValue());
87-
clientAttributes.put(CLIENT_HASH_KEY.getKey(), "cloud_spanner_client_raw_metrics");
88+
String clientUid = getDefaultTaskValue();
89+
clientAttributes.put(CLIENT_UID_KEY.getKey(), clientUid);
90+
clientAttributes.put(CLIENT_HASH_KEY.getKey(), generateClientHash(clientUid));
8891
return clientAttributes;
8992
}
9093

@@ -143,4 +146,15 @@ private static String getProcessId() {
143146
}
144147
}
145148
}
149+
150+
private static String generateClientHash(String clientUid) {
151+
HashFunction hasher = Hashing.goodFastHash(64);
152+
Long hash = hasher.hashBytes(clientUid.getBytes()).asLong();
153+
return convertToHex(hash, 10);
154+
}
155+
156+
private static String convertToHex(long hash, int kPrefixLength) {
157+
long shiftedValue = hash >> (64 - kPrefixLength);
158+
return String.format("%06X", shiftedValue & 0x00FFFFFFL);
159+
}
146160
}

0 commit comments

Comments
 (0)