|
28 | 28 | import com.google.cloud.opentelemetry.detection.AttributeKeys;
|
29 | 29 | import com.google.cloud.opentelemetry.detection.DetectedPlatform;
|
30 | 30 | import com.google.cloud.opentelemetry.detection.GCPPlatformDetector;
|
| 31 | +import com.google.common.hash.HashFunction; |
| 32 | +import com.google.common.hash.Hashing; |
31 | 33 | import io.opentelemetry.api.OpenTelemetry;
|
32 | 34 | import io.opentelemetry.sdk.OpenTelemetrySdk;
|
33 | 35 | import io.opentelemetry.sdk.metrics.SdkMeterProvider;
|
@@ -83,11 +85,40 @@ 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 |
|
| 94 | + /** |
| 95 | + * Generates a 6-digit zero-padded all lower case hexadecimal representation of hash of the |
| 96 | + * accounting group. The hash utilizes the 10 most significant bits of the value returned by |
| 97 | + * `Hashing.goodFastHash(64).hashBytes()`, so effectively the returned values are uniformly |
| 98 | + * distributed in the range [000000, 0003ff]. |
| 99 | + * |
| 100 | + * <p>The primary purpose of this function is to generate a hash value for the `client_hash` |
| 101 | + * resource label using `client_uid` metric field. The range of values is chosen to be small |
| 102 | + * enough to keep the cardinality of the Resource targets under control. Note: If at later time |
| 103 | + * the range needs to be increased, it can be done by increasing the value of `kPrefixLength` to |
| 104 | + * up to 24 bits without changing the format of the returned value. |
| 105 | + * |
| 106 | + * @return Returns a 6-digit zero-padded all lower case hexadecimal representation of hash of the |
| 107 | + * accounting group. |
| 108 | + */ |
| 109 | + static String generateClientHash(String clientUid) { |
| 110 | + if (clientUid == null) { |
| 111 | + return "000000"; |
| 112 | + } |
| 113 | + |
| 114 | + HashFunction hashFunction = Hashing.goodFastHash(64); |
| 115 | + Long hash = hashFunction.hashBytes(clientUid.getBytes()).asLong(); |
| 116 | + // Don't change this value without reading above comment |
| 117 | + int kPrefixLength = 10; |
| 118 | + long shiftedValue = hash >>> (64 - kPrefixLength); |
| 119 | + return String.format("%06x", shiftedValue); |
| 120 | + } |
| 121 | + |
91 | 122 | static String detectClientLocation() {
|
92 | 123 | GCPPlatformDetector detector = GCPPlatformDetector.DEFAULT_INSTANCE;
|
93 | 124 | DetectedPlatform detectedPlatform = detector.detectPlatform();
|
|
0 commit comments