@google-cloud/spanner 8.13.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/codec.d.ts +12 -30
- package/build/src/codec.js +98 -45
- package/build/src/common.js +2 -3
- package/build/src/database.js +30 -7
- package/build/src/index.d.ts +8 -0
- package/build/src/index.js +99 -63
- package/build/src/metrics/constants.d.ts +7 -6
- package/build/src/metrics/constants.js +18 -13
- package/build/src/metrics/external-types.d.ts +14 -0
- package/build/src/metrics/interceptor.js +8 -17
- package/build/src/metrics/metrics-tracer-factory.d.ts +19 -3
- package/build/src/metrics/metrics-tracer-factory.js +57 -14
- package/build/src/metrics/spanner-metrics-exporter.js +4 -0
- package/build/src/metrics/transform.d.ts +3 -4
- package/build/src/metrics/transform.js +16 -10
- package/build/src/multiplexed-session.d.ts +13 -0
- package/build/src/multiplexed-session.js +20 -8
- package/build/src/session-factory.d.ts +13 -0
- package/build/src/session-factory.js +12 -1
- package/build/src/transaction.d.ts +16 -0
- package/build/src/transaction.js +35 -24
- package/package.json +20 -28
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ViewOptions } from '@opentelemetry/sdk-metrics';
|
|
2
2
|
export declare const SPANNER_METER_NAME = "spanner-nodejs";
|
|
3
3
|
export declare const CLIENT_METRICS_PREFIX = "spanner.googleapis.com/internal/client";
|
|
4
4
|
export declare const SPANNER_RESOURCE_TYPE = "spanner_instance_client";
|
|
5
5
|
export declare const TRACER_CLEANUP_THRESHOLD_MS: number;
|
|
6
6
|
export declare const TRACER_CLEANUP_INTERVAL_MS: number;
|
|
7
7
|
export declare const ATTR_CLOUD_REGION = "cloud.region";
|
|
8
|
+
export declare const ATTR_CLOUD_AVAILABILITY_ZONE = "cloud.availability_zone";
|
|
8
9
|
export declare const MIN_EXPORT_FREQUENCY_MS: number;
|
|
9
10
|
export declare const MONITORED_RES_LABEL_KEY_PROJECT = "project_id";
|
|
10
11
|
export declare const MONITORED_RES_LABEL_KEY_INSTANCE = "instance_id";
|
|
@@ -29,8 +30,8 @@ export declare const METRIC_NAME_AFE_CONNECTIVITY_ERROR_COUNT = "afe_connectivit
|
|
|
29
30
|
export declare const METRIC_NAMES: Set<string>;
|
|
30
31
|
export declare const UNKNOWN_ATTRIBUTE = "unknown";
|
|
31
32
|
export declare const HISTOGRAM_BUCKET_BOUNDARIES: number[];
|
|
32
|
-
export declare const OPERATION_LATENCY_VIEW:
|
|
33
|
-
export declare const ATTEMPT_LATENCY_VIEW:
|
|
34
|
-
export declare const GFE_LATENCY_VIEW:
|
|
35
|
-
export declare const AFE_LATENCY_VIEW:
|
|
36
|
-
export declare const METRIC_VIEWS:
|
|
33
|
+
export declare const OPERATION_LATENCY_VIEW: ViewOptions;
|
|
34
|
+
export declare const ATTEMPT_LATENCY_VIEW: ViewOptions;
|
|
35
|
+
export declare const GFE_LATENCY_VIEW: ViewOptions;
|
|
36
|
+
export declare const AFE_LATENCY_VIEW: ViewOptions;
|
|
37
|
+
export declare const METRIC_VIEWS: ViewOptions[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.METRIC_VIEWS = exports.AFE_LATENCY_VIEW = exports.GFE_LATENCY_VIEW = exports.ATTEMPT_LATENCY_VIEW = exports.OPERATION_LATENCY_VIEW = exports.HISTOGRAM_BUCKET_BOUNDARIES = exports.UNKNOWN_ATTRIBUTE = exports.METRIC_NAMES = exports.METRIC_NAME_AFE_CONNECTIVITY_ERROR_COUNT = exports.METRIC_NAME_AFE_LATENCIES = exports.METRIC_NAME_GFE_CONNECTIVITY_ERROR_COUNT = exports.METRIC_NAME_GFE_LATENCIES = exports.METRIC_NAME_ATTEMPT_COUNT = exports.METRIC_NAME_OPERATION_COUNT = exports.METRIC_NAME_ATTEMPT_LATENCIES = exports.METRIC_NAME_OPERATION_LATENCIES = exports.METRIC_LABELS = exports.METRIC_LABEL_KEY_STATUS = exports.METRIC_LABEL_KEY_METHOD = exports.METRIC_LABEL_KEY_DATABASE = exports.METRIC_LABEL_KEY_CLIENT_NAME = exports.METRIC_LABEL_KEY_CLIENT_UID = exports.MONITORED_RESOURCE_LABELS = exports.MONITORED_RES_LABEL_KEY_CLIENT_HASH = exports.MONITORED_RES_LABEL_KEY_LOCATION = exports.MONITORED_RES_LABEL_KEY_INSTANCE_CONFIG = exports.MONITORED_RES_LABEL_KEY_INSTANCE = exports.MONITORED_RES_LABEL_KEY_PROJECT = exports.MIN_EXPORT_FREQUENCY_MS = exports.ATTR_CLOUD_REGION = exports.TRACER_CLEANUP_INTERVAL_MS = exports.TRACER_CLEANUP_THRESHOLD_MS = exports.SPANNER_RESOURCE_TYPE = exports.CLIENT_METRICS_PREFIX = exports.SPANNER_METER_NAME = void 0;
|
|
3
|
+
exports.METRIC_VIEWS = exports.AFE_LATENCY_VIEW = exports.GFE_LATENCY_VIEW = exports.ATTEMPT_LATENCY_VIEW = exports.OPERATION_LATENCY_VIEW = exports.HISTOGRAM_BUCKET_BOUNDARIES = exports.UNKNOWN_ATTRIBUTE = exports.METRIC_NAMES = exports.METRIC_NAME_AFE_CONNECTIVITY_ERROR_COUNT = exports.METRIC_NAME_AFE_LATENCIES = exports.METRIC_NAME_GFE_CONNECTIVITY_ERROR_COUNT = exports.METRIC_NAME_GFE_LATENCIES = exports.METRIC_NAME_ATTEMPT_COUNT = exports.METRIC_NAME_OPERATION_COUNT = exports.METRIC_NAME_ATTEMPT_LATENCIES = exports.METRIC_NAME_OPERATION_LATENCIES = exports.METRIC_LABELS = exports.METRIC_LABEL_KEY_STATUS = exports.METRIC_LABEL_KEY_METHOD = exports.METRIC_LABEL_KEY_DATABASE = exports.METRIC_LABEL_KEY_CLIENT_NAME = exports.METRIC_LABEL_KEY_CLIENT_UID = exports.MONITORED_RESOURCE_LABELS = exports.MONITORED_RES_LABEL_KEY_CLIENT_HASH = exports.MONITORED_RES_LABEL_KEY_LOCATION = exports.MONITORED_RES_LABEL_KEY_INSTANCE_CONFIG = exports.MONITORED_RES_LABEL_KEY_INSTANCE = exports.MONITORED_RES_LABEL_KEY_PROJECT = exports.MIN_EXPORT_FREQUENCY_MS = exports.ATTR_CLOUD_AVAILABILITY_ZONE = exports.ATTR_CLOUD_REGION = exports.TRACER_CLEANUP_INTERVAL_MS = exports.TRACER_CLEANUP_THRESHOLD_MS = exports.SPANNER_RESOURCE_TYPE = exports.CLIENT_METRICS_PREFIX = exports.SPANNER_METER_NAME = void 0;
|
|
4
4
|
// Copyright 2025 Google LLC
|
|
5
5
|
//
|
|
6
6
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -24,6 +24,7 @@ exports.TRACER_CLEANUP_INTERVAL_MS = 30 * 60 * 1000; // 30 Minutes
|
|
|
24
24
|
// OTel semantic conventions
|
|
25
25
|
// See https://github.com/open-telemetry/opentelemetry-js/blob/main/semantic-conventions/README.md#unstable-semconv
|
|
26
26
|
exports.ATTR_CLOUD_REGION = 'cloud.region';
|
|
27
|
+
exports.ATTR_CLOUD_AVAILABILITY_ZONE = 'cloud.availability_zone';
|
|
27
28
|
// Minimum period that must past between metric exports
|
|
28
29
|
exports.MIN_EXPORT_FREQUENCY_MS = 30 * 1000;
|
|
29
30
|
// Monitored resource labels
|
|
@@ -81,22 +82,26 @@ exports.HISTOGRAM_BUCKET_BOUNDARIES = [
|
|
|
81
82
|
800000.0, 1600000.0, 3200000.0,
|
|
82
83
|
];
|
|
83
84
|
// Defined Views for metric aggregation
|
|
84
|
-
|
|
85
|
+
const HISTOGRAM_AGGREGATION = {
|
|
86
|
+
type: sdk_metrics_1.AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
|
|
87
|
+
options: { boundaries: exports.HISTOGRAM_BUCKET_BOUNDARIES },
|
|
88
|
+
};
|
|
89
|
+
exports.OPERATION_LATENCY_VIEW = {
|
|
85
90
|
instrumentName: exports.METRIC_NAME_OPERATION_LATENCIES,
|
|
86
|
-
aggregation:
|
|
87
|
-
}
|
|
88
|
-
exports.ATTEMPT_LATENCY_VIEW =
|
|
91
|
+
aggregation: HISTOGRAM_AGGREGATION,
|
|
92
|
+
};
|
|
93
|
+
exports.ATTEMPT_LATENCY_VIEW = {
|
|
89
94
|
instrumentName: exports.METRIC_NAME_ATTEMPT_LATENCIES,
|
|
90
|
-
aggregation:
|
|
91
|
-
}
|
|
92
|
-
exports.GFE_LATENCY_VIEW =
|
|
95
|
+
aggregation: HISTOGRAM_AGGREGATION,
|
|
96
|
+
};
|
|
97
|
+
exports.GFE_LATENCY_VIEW = {
|
|
93
98
|
instrumentName: exports.METRIC_NAME_GFE_LATENCIES,
|
|
94
|
-
aggregation:
|
|
95
|
-
}
|
|
96
|
-
exports.AFE_LATENCY_VIEW =
|
|
99
|
+
aggregation: HISTOGRAM_AGGREGATION,
|
|
100
|
+
};
|
|
101
|
+
exports.AFE_LATENCY_VIEW = {
|
|
97
102
|
instrumentName: exports.METRIC_NAME_AFE_LATENCIES,
|
|
98
|
-
aggregation:
|
|
99
|
-
}
|
|
103
|
+
aggregation: HISTOGRAM_AGGREGATION,
|
|
104
|
+
};
|
|
100
105
|
exports.METRIC_VIEWS = [
|
|
101
106
|
exports.OPERATION_LATENCY_VIEW,
|
|
102
107
|
exports.ATTEMPT_LATENCY_VIEW,
|
|
@@ -18,3 +18,17 @@ export declare enum ValueType {
|
|
|
18
18
|
DOUBLE = "DOUBLE",
|
|
19
19
|
DISTRIBUTION = "DISTRIBUTION"
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* A Google Cloud Monitoring monitored resource.
|
|
23
|
+
*
|
|
24
|
+
* Previously imported from `@google-cloud/opentelemetry-resource-util`, which is
|
|
25
|
+
* deprecated and scheduled for archival. The interface is a plain data shape, so
|
|
26
|
+
* it is declared locally instead.
|
|
27
|
+
* See https://cloud.google.com/monitoring/api/ref_v3/rest/v3/MonitoredResource
|
|
28
|
+
*/
|
|
29
|
+
export interface MonitoredResource {
|
|
30
|
+
type: string;
|
|
31
|
+
labels: {
|
|
32
|
+
[key: string]: string;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -41,8 +41,8 @@ const MetricInterceptor = (options, nextCall) => {
|
|
|
41
41
|
const requestId = metadata.get('x-goog-spanner-request-id')[0];
|
|
42
42
|
const metricsTracer = factory?.getCurrentTracer(requestId);
|
|
43
43
|
metricsTracer?.recordAttemptStart();
|
|
44
|
-
const
|
|
45
|
-
onReceiveMetadata: function (metadata
|
|
44
|
+
const interceptingListener = {
|
|
45
|
+
onReceiveMetadata: function (metadata) {
|
|
46
46
|
// Record GFE/AFE Metrics
|
|
47
47
|
// GFE/AFE latency if available,
|
|
48
48
|
// or else increase the GFE/AFE connectivity error count
|
|
@@ -56,13 +56,13 @@ const MetricInterceptor = (options, nextCall) => {
|
|
|
56
56
|
const afeTiming = metricsTracer?.extractAfeLatency(serverTimingHeader);
|
|
57
57
|
metricsTracer.afeLatency = afeTiming ?? null;
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
listener.onReceiveMetadata(metadata);
|
|
60
60
|
},
|
|
61
|
-
onReceiveMessage: function (message
|
|
62
|
-
|
|
61
|
+
onReceiveMessage: function (message) {
|
|
62
|
+
listener.onReceiveMessage(message);
|
|
63
63
|
},
|
|
64
|
-
onReceiveStatus: function (status
|
|
65
|
-
|
|
64
|
+
onReceiveStatus: function (status) {
|
|
65
|
+
listener.onReceiveStatus(status);
|
|
66
66
|
// Record attempt metric completion
|
|
67
67
|
metricsTracer?.recordAttemptCompletion(status.code);
|
|
68
68
|
if (metricsTracer?.gfeLatency) {
|
|
@@ -79,16 +79,7 @@ const MetricInterceptor = (options, nextCall) => {
|
|
|
79
79
|
}
|
|
80
80
|
},
|
|
81
81
|
};
|
|
82
|
-
next(metadata,
|
|
83
|
-
},
|
|
84
|
-
sendMessage: function (message, next) {
|
|
85
|
-
next(message);
|
|
86
|
-
},
|
|
87
|
-
halfClose: function (next) {
|
|
88
|
-
next();
|
|
89
|
-
},
|
|
90
|
-
cancel: function (next) {
|
|
91
|
-
next();
|
|
82
|
+
next(metadata, interceptingListener);
|
|
92
83
|
},
|
|
93
84
|
});
|
|
94
85
|
};
|
|
@@ -29,6 +29,8 @@ export declare class MetricsTracerFactory {
|
|
|
29
29
|
private _clientName;
|
|
30
30
|
private _clientUid;
|
|
31
31
|
private _location;
|
|
32
|
+
private _locationPromise;
|
|
33
|
+
private _metricReaders;
|
|
32
34
|
private _projectId;
|
|
33
35
|
private _currentOperationTracers;
|
|
34
36
|
private _currentOperationLastUpdatedMs;
|
|
@@ -52,6 +54,10 @@ export declare class MetricsTracerFactory {
|
|
|
52
54
|
/**
|
|
53
55
|
* Returns the MeterProvider, creating it and metric instruments if not already initialized.
|
|
54
56
|
* Client-wide attributes that are known at this time are cached to be provided to all MetricsTracers.
|
|
57
|
+
*
|
|
58
|
+
* If called again with readers when the existing MeterProvider has none, the provider is
|
|
59
|
+
* recreated with those readers. If readers are already attached, additional readers are ignored.
|
|
60
|
+
*
|
|
55
61
|
* @param readers Optional array of MetricReader instances to attach to the MeterProvider.
|
|
56
62
|
* @returns The OTEL MeterProvider instance.
|
|
57
63
|
*/
|
|
@@ -96,6 +102,14 @@ export declare class MetricsTracerFactory {
|
|
|
96
102
|
* Returns the Client Name.
|
|
97
103
|
*/
|
|
98
104
|
get clientName(): string;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the detected client location.
|
|
107
|
+
*/
|
|
108
|
+
get location(): string;
|
|
109
|
+
/**
|
|
110
|
+
* Returns true if the MeterProvider is initialized with at least one MetricReader.
|
|
111
|
+
*/
|
|
112
|
+
hasMetricReaders(): boolean;
|
|
99
113
|
/**
|
|
100
114
|
* Creates a new MetricsTracer for a given resource name and method, and stores it for later retrieval.
|
|
101
115
|
* Returns null if metrics are disabled.
|
|
@@ -155,9 +169,11 @@ export declare class MetricsTracerFactory {
|
|
|
155
169
|
*/
|
|
156
170
|
private static _generateClientHash;
|
|
157
171
|
/**
|
|
158
|
-
* Gets the location (region) of the client,
|
|
159
|
-
* Uses
|
|
160
|
-
*
|
|
172
|
+
* Gets the location (region or zone) of the client, or defaults to "global".
|
|
173
|
+
* Uses the GCP resource detector to detect the location from the environment.
|
|
174
|
+
* Checks `cloud.region` first and falls back to `cloud.availability_zone`
|
|
175
|
+
* for zonal GKE clusters where `cloud.region` is not set.
|
|
176
|
+
* @returns The detected location string, or "global" if not found.
|
|
161
177
|
*/
|
|
162
178
|
private static _detectClientLocation;
|
|
163
179
|
private _cleanMetricsTracers;
|
|
@@ -20,7 +20,7 @@ const process = require("process");
|
|
|
20
20
|
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
21
21
|
const api_1 = require("@opentelemetry/api");
|
|
22
22
|
const resources_1 = require("@opentelemetry/resources");
|
|
23
|
-
const
|
|
23
|
+
const resource_detector_gcp_1 = require("@opentelemetry/resource-detector-gcp");
|
|
24
24
|
const Constants = require("./constants");
|
|
25
25
|
const metrics_tracer_1 = require("./metrics-tracer");
|
|
26
26
|
const version = require('../../../package.json').version;
|
|
@@ -52,6 +52,8 @@ class MetricsTracerFactory {
|
|
|
52
52
|
_clientName;
|
|
53
53
|
_clientUid;
|
|
54
54
|
_location = 'global';
|
|
55
|
+
_locationPromise = null;
|
|
56
|
+
_metricReaders = [];
|
|
55
57
|
_projectId;
|
|
56
58
|
_currentOperationTracers = new Map();
|
|
57
59
|
_currentOperationLastUpdatedMs = new Map();
|
|
@@ -69,11 +71,17 @@ class MetricsTracerFactory {
|
|
|
69
71
|
this._clientName = `${Constants.SPANNER_METER_NAME}/${version}`;
|
|
70
72
|
// Only perform async call to retrieve location is metrics are enabled.
|
|
71
73
|
if (MetricsTracerFactory.enabled) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
this._locationPromise = MetricsTracerFactory._detectClientLocation()
|
|
75
|
+
.then(location => {
|
|
74
76
|
this._location = location.length > 0 ? location : 'global';
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
return this._location;
|
|
78
|
+
})
|
|
79
|
+
.catch(error => {
|
|
80
|
+
console.warn('Unable to detect location.', error);
|
|
81
|
+
return this._location;
|
|
82
|
+
})
|
|
83
|
+
.finally(() => {
|
|
84
|
+
this._locationPromise = null;
|
|
77
85
|
});
|
|
78
86
|
}
|
|
79
87
|
this._clientHash = MetricsTracerFactory._generateClientHash(this._clientUid);
|
|
@@ -103,23 +111,42 @@ class MetricsTracerFactory {
|
|
|
103
111
|
/**
|
|
104
112
|
* Returns the MeterProvider, creating it and metric instruments if not already initialized.
|
|
105
113
|
* Client-wide attributes that are known at this time are cached to be provided to all MetricsTracers.
|
|
114
|
+
*
|
|
115
|
+
* If called again with readers when the existing MeterProvider has none, the provider is
|
|
116
|
+
* recreated with those readers. If readers are already attached, additional readers are ignored.
|
|
117
|
+
*
|
|
106
118
|
* @param readers Optional array of MetricReader instances to attach to the MeterProvider.
|
|
107
119
|
* @returns The OTEL MeterProvider instance.
|
|
108
120
|
*/
|
|
109
121
|
getMeterProvider(readers = []) {
|
|
122
|
+
if (this._meterProvider !== null && readers.length > 0) {
|
|
123
|
+
if (this._metricReaders.length === 0) {
|
|
124
|
+
const staleMeterProvider = this._meterProvider;
|
|
125
|
+
this._meterProvider = null;
|
|
126
|
+
staleMeterProvider.shutdown().catch(error => {
|
|
127
|
+
console.warn('Unable to shut down the previous MeterProvider.', error);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
console.warn('MeterProvider is already initialized with metric readers. The ' +
|
|
132
|
+
'additional readers passed to getMeterProvider() are ignored.');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
110
135
|
if (this._meterProvider === null) {
|
|
111
|
-
const resource =
|
|
136
|
+
const resource = (0, resources_1.resourceFromAttributes)({
|
|
112
137
|
[Constants.MONITORED_RES_LABEL_KEY_PROJECT]: this._projectId,
|
|
113
138
|
[Constants.MONITORED_RES_LABEL_KEY_CLIENT_HASH]: this._clientHash,
|
|
114
|
-
[Constants.MONITORED_RES_LABEL_KEY_LOCATION]: this._location,
|
|
139
|
+
[Constants.MONITORED_RES_LABEL_KEY_LOCATION]: this._locationPromise ?? this._location,
|
|
115
140
|
[Constants.MONITORED_RES_LABEL_KEY_INSTANCE]: 'unknown',
|
|
116
141
|
[Constants.MONITORED_RES_LABEL_KEY_INSTANCE_CONFIG]: 'unknown',
|
|
117
142
|
});
|
|
143
|
+
void resource.waitForAsyncAttributes?.();
|
|
118
144
|
this._meterProvider = new sdk_metrics_1.MeterProvider({
|
|
119
145
|
resource: resource,
|
|
120
146
|
readers: readers,
|
|
121
147
|
views: Constants.METRIC_VIEWS,
|
|
122
148
|
});
|
|
149
|
+
this._metricReaders = readers;
|
|
123
150
|
this._createMetricInstruments();
|
|
124
151
|
}
|
|
125
152
|
return this._meterProvider;
|
|
@@ -140,6 +167,7 @@ class MetricsTracerFactory {
|
|
|
140
167
|
await this._meterProvider.shutdown();
|
|
141
168
|
}
|
|
142
169
|
this._meterProvider = null;
|
|
170
|
+
this._metricReaders = [];
|
|
143
171
|
this._currentOperationTracers = new Map();
|
|
144
172
|
this._currentOperationLastUpdatedMs = new Map();
|
|
145
173
|
}
|
|
@@ -191,6 +219,18 @@ class MetricsTracerFactory {
|
|
|
191
219
|
get clientName() {
|
|
192
220
|
return this._clientName;
|
|
193
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns the detected client location.
|
|
224
|
+
*/
|
|
225
|
+
get location() {
|
|
226
|
+
return this._location;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Returns true if the MeterProvider is initialized with at least one MetricReader.
|
|
230
|
+
*/
|
|
231
|
+
hasMetricReaders() {
|
|
232
|
+
return this._metricReaders.length > 0;
|
|
233
|
+
}
|
|
194
234
|
/**
|
|
195
235
|
* Creates a new MetricsTracer for a given resource name and method, and stores it for later retrieval.
|
|
196
236
|
* Returns null if metrics are disabled.
|
|
@@ -350,20 +390,23 @@ class MetricsTracerFactory {
|
|
|
350
390
|
return shiftedValue.toString(16).padStart(6, '0');
|
|
351
391
|
}
|
|
352
392
|
/**
|
|
353
|
-
* Gets the location (region) of the client,
|
|
354
|
-
* Uses
|
|
355
|
-
*
|
|
393
|
+
* Gets the location (region or zone) of the client, or defaults to "global".
|
|
394
|
+
* Uses the GCP resource detector to detect the location from the environment.
|
|
395
|
+
* Checks `cloud.region` first and falls back to `cloud.availability_zone`
|
|
396
|
+
* for zonal GKE clusters where `cloud.region` is not set.
|
|
397
|
+
* @returns The detected location string, or "global" if not found.
|
|
356
398
|
*/
|
|
357
399
|
static async _detectClientLocation() {
|
|
358
400
|
const defaultRegion = 'global';
|
|
359
401
|
try {
|
|
360
402
|
const resource = await (0, resources_1.detectResources)({
|
|
361
|
-
detectors: [
|
|
403
|
+
detectors: [resource_detector_gcp_1.gcpDetector],
|
|
362
404
|
});
|
|
363
405
|
await resource?.waitForAsyncAttributes?.();
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
|
|
406
|
+
const location = resource.attributes[Constants.ATTR_CLOUD_REGION] ??
|
|
407
|
+
resource.attributes[Constants.ATTR_CLOUD_AVAILABILITY_ZONE];
|
|
408
|
+
if (typeof location === 'string' && location) {
|
|
409
|
+
return location;
|
|
367
410
|
}
|
|
368
411
|
}
|
|
369
412
|
catch (err) {
|
|
@@ -47,6 +47,7 @@ class CloudMonitoringMetricsExporter {
|
|
|
47
47
|
const now = new Date();
|
|
48
48
|
if (now.getTime() - this._lastExported.getTime() <=
|
|
49
49
|
constants_1.MIN_EXPORT_FREQUENCY_MS) {
|
|
50
|
+
resultCallback({ code: core_1.ExportResultCode.SUCCESS });
|
|
50
51
|
return;
|
|
51
52
|
}
|
|
52
53
|
this._lastExported = now;
|
|
@@ -66,6 +67,9 @@ class CloudMonitoringMetricsExporter {
|
|
|
66
67
|
* @param resourceMetrics Metrics to be sent to the Google Cloud Monitoring backend
|
|
67
68
|
*/
|
|
68
69
|
async _exportAsync(resourceMetrics) {
|
|
70
|
+
if (resourceMetrics?.resource?.asyncAttributesPending) {
|
|
71
|
+
await resourceMetrics.resource.waitForAsyncAttributes?.();
|
|
72
|
+
}
|
|
69
73
|
const timeSeriesList = (0, transform_1.transformResourceMetricToTimeSeriesArray)(resourceMetrics, this._projectId);
|
|
70
74
|
let failure = {
|
|
71
75
|
sendFailed: false,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { MetricData, DataPoint, ResourceMetrics } from '@opentelemetry/sdk-metrics';
|
|
2
2
|
import { Resource } from '@opentelemetry/resources';
|
|
3
|
-
import { MonitoredResource } from '
|
|
4
|
-
import { MetricKind, ValueType } from './external-types';
|
|
3
|
+
import { MetricKind, MonitoredResource, ValueType } from './external-types';
|
|
5
4
|
/** Transforms a OpenTelemetry instrument type to a GCM MetricKind. */
|
|
6
5
|
declare function _transformMetricKind(metric: MetricData): MetricKind;
|
|
7
6
|
/** Transforms resource to Google Cloud Monitoring monitored resource */
|
|
@@ -120,8 +119,8 @@ declare function _transformPoint<T>(metric: MetricData, dataPoint: DataPoint<T>)
|
|
|
120
119
|
startTime?: undefined;
|
|
121
120
|
};
|
|
122
121
|
};
|
|
123
|
-
/** Extracts metric and monitored resource labels from data point */
|
|
124
|
-
declare function _extractLabels<T>({ attributes }: DataPoint<T> | Resource, projectId: string): {
|
|
122
|
+
/** Extracts metric and monitored resource labels from data point or resource */
|
|
123
|
+
declare function _extractLabels<T>({ attributes }: DataPoint<T> | Resource, projectId: string, isResource?: boolean): {
|
|
125
124
|
metricLabels: {};
|
|
126
125
|
monitoredResourceLabels: {};
|
|
127
126
|
};
|
|
@@ -83,9 +83,9 @@ function transformResourceMetricToTimeSeriesArray(resourceMetrics, projectId) {
|
|
|
83
83
|
function _createTimeSeries(metric, dataPoint, resource, projectId) {
|
|
84
84
|
const type = path.posix.join(constants_1.CLIENT_METRICS_PREFIX, metric.descriptor.name);
|
|
85
85
|
const resourceLabels = resource
|
|
86
|
-
? _extractLabels(resource, projectId)
|
|
86
|
+
? _extractLabels(resource, projectId, true)
|
|
87
87
|
: { metricLabels: {}, monitoredResourceLabels: {} };
|
|
88
|
-
const dataLabels = _extractLabels(dataPoint, projectId);
|
|
88
|
+
const dataLabels = _extractLabels(dataPoint, projectId, false);
|
|
89
89
|
const labels = {
|
|
90
90
|
...resourceLabels.metricLabels,
|
|
91
91
|
...dataLabels.metricLabels,
|
|
@@ -152,15 +152,21 @@ function _transformPoint(metric, dataPoint) {
|
|
|
152
152
|
};
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
-
/** Extracts metric and monitored resource labels from data point */
|
|
156
|
-
function _extractLabels({ attributes = {} }, projectId) {
|
|
155
|
+
/** Extracts metric and monitored resource labels from data point or resource */
|
|
156
|
+
function _extractLabels({ attributes = {} }, projectId, isResource = false) {
|
|
157
157
|
const factory = metrics_tracer_factory_1.MetricsTracerFactory.getInstance(projectId);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
factory?.clientUid ?? constants_1.UNKNOWN_ATTRIBUTE
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
158
|
+
const combinedAttributes = {
|
|
159
|
+
...attributes,
|
|
160
|
+
[constants_1.METRIC_LABEL_KEY_CLIENT_UID]: factory?.clientUid ?? constants_1.UNKNOWN_ATTRIBUTE,
|
|
161
|
+
[constants_1.METRIC_LABEL_KEY_CLIENT_NAME]: factory?.clientName ?? constants_1.UNKNOWN_ATTRIBUTE,
|
|
162
|
+
};
|
|
163
|
+
if (isResource &&
|
|
164
|
+
factory?.location &&
|
|
165
|
+
(!combinedAttributes[constants_1.MONITORED_RES_LABEL_KEY_LOCATION] ||
|
|
166
|
+
combinedAttributes[constants_1.MONITORED_RES_LABEL_KEY_LOCATION] === 'global')) {
|
|
167
|
+
combinedAttributes[constants_1.MONITORED_RES_LABEL_KEY_LOCATION] = factory.location;
|
|
168
|
+
}
|
|
169
|
+
return Object.entries(combinedAttributes).reduce((result, [key, value]) => {
|
|
164
170
|
const normalizedKey = _normalizeLabelKey(key);
|
|
165
171
|
const val = value?.toString();
|
|
166
172
|
if (constants_1.METRIC_LABELS.has(key))
|
|
@@ -42,6 +42,12 @@ export interface MultiplexedSessionInterface extends EventEmitter {
|
|
|
42
42
|
* @param {GetSessionCallback} callback The callback function.
|
|
43
43
|
*/
|
|
44
44
|
getSession(callback: GetSessionCallback): void;
|
|
45
|
+
/**
|
|
46
|
+
* When called returns a cached multiplexed session synchronously if available.
|
|
47
|
+
*
|
|
48
|
+
* @name MultiplexedSessionInterface#getSessionSync
|
|
49
|
+
*/
|
|
50
|
+
getSessionSync(): Session | null;
|
|
45
51
|
}
|
|
46
52
|
/**
|
|
47
53
|
* Class used to manage connections to Spanner using multiplexed session.
|
|
@@ -98,6 +104,13 @@ export declare class MultiplexedSession extends EventEmitter implements Multiple
|
|
|
98
104
|
*
|
|
99
105
|
*/
|
|
100
106
|
_maintain(): void;
|
|
107
|
+
/**
|
|
108
|
+
* Synchronously returns the cached multiplexed session if available,
|
|
109
|
+
* or null if no session is currently cached.
|
|
110
|
+
*
|
|
111
|
+
* @returns {Session|null} The cached multiplexed session or null.
|
|
112
|
+
*/
|
|
113
|
+
getSessionSync(): Session | null;
|
|
101
114
|
/**
|
|
102
115
|
* Retrieves a session asynchronously and invokes a callback with the session details.
|
|
103
116
|
* Note: The callback receives `(null, session)`. To prevent unnecessary allocations on
|
|
@@ -144,6 +144,20 @@ class MultiplexedSession extends events_1.EventEmitter {
|
|
|
144
144
|
// force the process to stay open.
|
|
145
145
|
this._refreshHandle.unref();
|
|
146
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Synchronously returns the cached multiplexed session if available,
|
|
149
|
+
* or null if no session is currently cached.
|
|
150
|
+
*
|
|
151
|
+
* @returns {Session|null} The cached multiplexed session or null.
|
|
152
|
+
*/
|
|
153
|
+
getSessionSync() {
|
|
154
|
+
if (this._multiplexedSession !== null) {
|
|
155
|
+
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
156
|
+
span.addEvent('Cache hit: has usable multiplexed session');
|
|
157
|
+
return this._multiplexedSession;
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
147
161
|
/**
|
|
148
162
|
* Retrieves a session asynchronously and invokes a callback with the session details.
|
|
149
163
|
* Note: The callback receives `(null, session)`. To prevent unnecessary allocations on
|
|
@@ -156,10 +170,8 @@ class MultiplexedSession extends events_1.EventEmitter {
|
|
|
156
170
|
*
|
|
157
171
|
*/
|
|
158
172
|
getSession(callback) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
162
|
-
span.addEvent('Cache hit: has usable multiplexed session');
|
|
173
|
+
const session = this.getSessionSync();
|
|
174
|
+
if (session !== null) {
|
|
163
175
|
// Use process.nextTick to guarantee asynchronous callback execution ("never release Zalgo").
|
|
164
176
|
// This avoids microtask and Promise allocation overhead while preventing race conditions
|
|
165
177
|
// where callers (such as Database.prototype.runStream) need to return their stream and
|
|
@@ -196,12 +208,12 @@ class MultiplexedSession extends events_1.EventEmitter {
|
|
|
196
208
|
*
|
|
197
209
|
*/
|
|
198
210
|
async _getSession() {
|
|
199
|
-
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
200
211
|
// Check if the multiplexed session is already available
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return
|
|
212
|
+
const cachedSession = this.getSessionSync();
|
|
213
|
+
if (cachedSession !== null) {
|
|
214
|
+
return cachedSession;
|
|
204
215
|
}
|
|
216
|
+
const span = (0, instrument_1.getActiveOrNoopSpan)();
|
|
205
217
|
span.addEvent('Waiting for a multiplexed session to become available');
|
|
206
218
|
// If initialization is ALREADY in progress, join the existing line!
|
|
207
219
|
if (this._sharedMuxSessionWaitPromise) {
|
|
@@ -42,6 +42,12 @@ export interface SessionFactoryInterface {
|
|
|
42
42
|
* @param {GetSessionCallback} callback The callback function.
|
|
43
43
|
*/
|
|
44
44
|
getSession(callback: GetSessionCallback): void;
|
|
45
|
+
/**
|
|
46
|
+
* When called returns a cached multiplexed session synchronously if available.
|
|
47
|
+
*
|
|
48
|
+
* @name SessionFactoryInterface#getSessionSync
|
|
49
|
+
*/
|
|
50
|
+
getSessionSync(): Session | null;
|
|
45
51
|
/**
|
|
46
52
|
* When called returns a session for paritioned dml.
|
|
47
53
|
*
|
|
@@ -100,6 +106,13 @@ export declare class SessionFactory extends common.GrpcServiceObject implements
|
|
|
100
106
|
isMultiplexedPartitionOps: boolean;
|
|
101
107
|
isMultiplexedRW: boolean;
|
|
102
108
|
constructor(database: Database, name: String, poolOptions?: SessionPoolConstructor | SessionPoolOptions);
|
|
109
|
+
/**
|
|
110
|
+
* Synchronously returns a cached multiplexed session if multiplexed sessions
|
|
111
|
+
* are enabled and one is available, otherwise null.
|
|
112
|
+
*
|
|
113
|
+
* @returns {Session|null} The cached multiplexed session or null.
|
|
114
|
+
*/
|
|
115
|
+
getSessionSync(): Session | null;
|
|
103
116
|
/**
|
|
104
117
|
* Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
|
|
105
118
|
*
|
|
@@ -64,6 +64,17 @@ class SessionFactory extends common.GrpcServiceObject {
|
|
|
64
64
|
this.pool_.open();
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Synchronously returns a cached multiplexed session if multiplexed sessions
|
|
69
|
+
* are enabled and one is available, otherwise null.
|
|
70
|
+
*
|
|
71
|
+
* @returns {Session|null} The cached multiplexed session or null.
|
|
72
|
+
*/
|
|
73
|
+
getSessionSync() {
|
|
74
|
+
return this.isMultiplexed
|
|
75
|
+
? (this.multiplexedSession_?.getSessionSync?.() ?? null)
|
|
76
|
+
: null;
|
|
77
|
+
}
|
|
67
78
|
/**
|
|
68
79
|
* Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
|
|
69
80
|
*
|
|
@@ -76,7 +87,7 @@ class SessionFactory extends common.GrpcServiceObject {
|
|
|
76
87
|
const sessionHandler = this.isMultiplexed
|
|
77
88
|
? this.multiplexedSession_
|
|
78
89
|
: this.pool_;
|
|
79
|
-
sessionHandler.getSession(
|
|
90
|
+
sessionHandler.getSession(callback);
|
|
80
91
|
}
|
|
81
92
|
/**
|
|
82
93
|
* Retrieves a session for partitioned operations, selecting the appropriate session type
|
|
@@ -304,6 +304,22 @@ export declare class Snapshot extends EventEmitter {
|
|
|
304
304
|
* are specified for a query.
|
|
305
305
|
*/
|
|
306
306
|
constructor(session: Session, options?: TimestampBounds, queryOptions?: IQueryOptions);
|
|
307
|
+
/**
|
|
308
|
+
* Binds the multiplexed session affinity key to the gax options of an
|
|
309
|
+
* outgoing request, so that all requests of this transaction are routed to
|
|
310
|
+
* the same gRPC channel.
|
|
311
|
+
*
|
|
312
|
+
* `config` is always a request descriptor that was freshly constructed by the
|
|
313
|
+
* caller for this one RPC (and {@link Spanner#prepareGapicRequest_} already
|
|
314
|
+
* modifies `config.headers` in place), so the affinity key is assigned
|
|
315
|
+
* directly instead of allocating a copy of the descriptor per request.
|
|
316
|
+
*
|
|
317
|
+
* @private
|
|
318
|
+
*
|
|
319
|
+
* @param {object} config The request configuration.
|
|
320
|
+
* @returns {object} The same request configuration.
|
|
321
|
+
*/
|
|
322
|
+
private _applyAffinityGaxOpts;
|
|
307
323
|
protected _updatePrecommitToken(resp: PrecommitTokenProvider): void;
|
|
308
324
|
/**
|
|
309
325
|
* Selects a single representative mutation from a list to be used as the
|