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