|
24 | 24 | import static com.google.cloud.spanner.BuiltInMetricsConstant.LOCATION_ID_KEY;
|
25 | 25 | import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY;
|
26 | 26 |
|
| 27 | +import com.google.common.hash.HashFunction; |
| 28 | +import com.google.common.hash.Hashing; |
27 | 29 | import com.google.auth.Credentials;
|
28 | 30 | import com.google.cloud.opentelemetry.detection.AttributeKeys;
|
29 | 31 | import com.google.cloud.opentelemetry.detection.DetectedPlatform;
|
@@ -83,8 +85,9 @@ Map<String, String> createClientAttributes(String projectId, String client_name)
|
83 | 85 | // TODO: Replace this with real value.
|
84 | 86 | clientAttributes.put(INSTANCE_CONFIG_ID_KEY.getKey(), "unknown");
|
85 | 87 | 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)); |
88 | 91 | return clientAttributes;
|
89 | 92 | }
|
90 | 93 |
|
@@ -143,4 +146,15 @@ private static String getProcessId() {
|
143 | 146 | }
|
144 | 147 | }
|
145 | 148 | }
|
| 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 | + } |
146 | 160 | }
|
0 commit comments