@@ -18,6 +18,7 @@ package spanner
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
"os"
22
23
"sort"
23
24
"testing"
@@ -41,21 +42,19 @@ func TestNewBuiltinMetricsTracerFactory(t *testing.T) {
41
42
os .Setenv ("SPANNER_ENABLE_BUILTIN_METRICS" , "true" )
42
43
defer os .Unsetenv ("SPANNER_ENABLE_BUILTIN_METRICS" )
43
44
ctx := context .Background ()
44
- project := "test-project"
45
- instance := "test-instance"
46
45
clientUID := "test-uid"
47
46
createSessionRPC := "Spanner.BatchCreateSessions"
48
47
if isMultiplexEnabled {
49
48
createSessionRPC = "Spanner.CreateSession"
50
49
}
51
50
52
51
wantClientAttributes := []attribute.KeyValue {
53
- attribute .String (monitoredResLabelKeyProject , project ),
54
- attribute .String (monitoredResLabelKeyInstance , instance ),
52
+ attribute .String (monitoredResLabelKeyProject , "[PROJECT]" ),
53
+ attribute .String (monitoredResLabelKeyInstance , "[INSTANCE]" ),
55
54
attribute .String (metricLabelKeyDatabase , "[DATABASE]" ),
56
55
attribute .String (metricLabelKeyClientUID , clientUID ),
57
56
attribute .String (metricLabelKeyClientName , clientName ),
58
- attribute .String (monitoredResLabelKeyClientHash , "cloud_spanner_client_raw_metrics " ),
57
+ attribute .String (monitoredResLabelKeyClientHash , "0000ed " ),
59
58
attribute .String (monitoredResLabelKeyInstanceConfig , "unknown" ),
60
59
attribute .String (monitoredResLabelKeyLocation , "global" ),
61
60
}
@@ -74,11 +73,16 @@ func TestNewBuiltinMetricsTracerFactory(t *testing.T) {
74
73
75
74
// return constant client UID instead of random, so that attributes can be compared
76
75
origGenerateClientUID := generateClientUID
76
+ origDetectClientLocation := detectClientLocation
77
77
generateClientUID = func () (string , error ) {
78
78
return clientUID , nil
79
79
}
80
+ detectClientLocation = func (ctx context.Context ) string {
81
+ return "global"
82
+ }
80
83
defer func () {
81
84
generateClientUID = origGenerateClientUID
85
+ detectClientLocation = origDetectClientLocation
82
86
}()
83
87
84
88
// Setup mock monitoring server
@@ -153,8 +157,7 @@ func TestNewBuiltinMetricsTracerFactory(t *testing.T) {
153
157
t .Errorf ("builtinEnabled: got: %v, want: %v" , client .metricsTracerFactory .enabled , test .wantBuiltinEnabled )
154
158
}
155
159
156
- if diff := testutil .Diff (client .metricsTracerFactory .clientAttributes , wantClientAttributes ,
157
- cmpopts .IgnoreUnexported (attribute.KeyValue {}, attribute.Value {})); diff != "" {
160
+ if diff := testutil .Diff (client .metricsTracerFactory .clientAttributes , wantClientAttributes , cmpopts .EquateComparable (attribute.KeyValue {}, attribute.Value {})); diff != "" {
158
161
t .Errorf ("clientAttributes: got=-, want=+ \n %v" , diff )
159
162
}
160
163
@@ -235,3 +238,49 @@ func TestNewBuiltinMetricsTracerFactory(t *testing.T) {
235
238
})
236
239
}
237
240
}
241
+
242
+ // TestGenerateClientHash tests the generateClientHash function.
243
+ func TestGenerateClientHash (t * testing.T ) {
244
+ tests := []struct {
245
+ name string
246
+ clientUID string
247
+ expectedValue string
248
+ expectedLength int
249
+ expectedMaxValue int64
250
+ }{
251
+ {"Simple UID" , "exampleUID" , "00006b" , 6 , 0x3FF },
252
+ {"Empty UID" , "" , "000000" , 6 , 0x3FF },
253
+ {"Special Characters" , "!@#$%^&*()" , "000389" , 6 , 0x3FF },
254
+ {"Very Long UID" , "aVeryLongUniqueIdentifierThatExceedsNormalLength" , "000125" , 6 , 0x3FF },
255
+ {"Numeric UID" , "1234567890" , "00003e" , 6 , 0x3FF },
256
+ }
257
+
258
+ for _ , tt := range tests {
259
+ t .Run (tt .name , func (t * testing.T ) {
260
+ hash := generateClientHash (tt .clientUID )
261
+ if hash != tt .expectedValue {
262
+ t .Errorf ("expected hash value %s, got %s" , tt .expectedValue , hash )
263
+ }
264
+ // Check if the hash length is 6
265
+ if len (hash ) != tt .expectedLength {
266
+ t .Errorf ("expected hash length %d, got %d" , tt .expectedLength , len (hash ))
267
+ }
268
+
269
+ // Check if the hash is in the range [000000, 0003ff]
270
+ hashValue , err := parseHex (hash )
271
+ if err != nil {
272
+ t .Errorf ("failed to parse hash: %v" , err )
273
+ }
274
+ if hashValue < 0 || hashValue > tt .expectedMaxValue {
275
+ t .Errorf ("expected hash value in range [0, %d], got %d" , tt .expectedMaxValue , hashValue )
276
+ }
277
+ })
278
+ }
279
+ }
280
+
281
+ // parseHex converts a hexadecimal string to an int64.
282
+ func parseHex (hexStr string ) (int64 , error ) {
283
+ var value int64
284
+ _ , err := fmt .Sscanf (hexStr , "%x" , & value )
285
+ return value , err
286
+ }
0 commit comments