Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions handwritten/spanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@
* DirectedReadOptions won't be set for readWrite transactions"
* @property {ObservabilityOptions} [observabilityOptions] Sets the observability options to be used for OpenTelemetry tracing
* @property {boolean} [disableBuiltInMetrics=True] If set to true, built-in metrics will be disabled.
* @property {number} ['grpc.enable_channelz'=0] Whether to enable gRPC Channelz service tracking.
* Defaults to 0 (disabled) to eliminate per-RPC tracking and allocation overhead. Set to 1 to enable.
*/
export interface SpannerOptions extends GrpcClientOptions {
apiEndpoint?: string;
Expand All @@ -169,7 +171,7 @@
>;
observabilityOptions?: ObservabilityOptions;
disableBuiltInMetrics?: boolean;
interceptors?: any[];

Check warning on line 174 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
sessionLabels?: {[key: string]: string};
/**
* The Trusted Cloud Domain (TPC) DNS of the service used to make requests.
Expand All @@ -182,6 +184,12 @@
*/
universe_domain?: string;
universeDomain?: string;
/**
* Whether to enable gRPC Channelz service tracking.
* Defaults to `0` (disabled) to eliminate per-RPC allocation and tracking overhead.
* Set to `1` if live connection introspection via gRPC Channelz (e.g. grpcdebug) is required.
*/
'grpc.enable_channelz'?: number;
}
export interface RequestConfig {
client: string;
Expand Down Expand Up @@ -423,6 +431,8 @@
scopes,
// Add grpc keep alive setting
'grpc.keepalive_time_ms': 120000,
// Disable Channelz by default to reduce per-RPC tracking and allocation overhead
'grpc.enable_channelz': 0,
// Enable grpc-gcp support
'grpc.callInvocationTransformer': grpcGcp.gcpCallInvocationTransformer,
'grpc.channelFactoryOverride': grpcGcp.gcpChannelFactoryOverride,
Expand Down Expand Up @@ -534,7 +544,7 @@
if (!this.clients_.has(clientName)) {
this.clients_.set(
clientName,
new v1[clientName](this.options as ClientOptions),
new v1.InstanceAdminClient(this.options as ClientOptions),
);
}
return this.clients_.get(clientName)! as v1.InstanceAdminClient;
Expand All @@ -558,7 +568,7 @@
if (!this.clients_.has(clientName)) {
this.clients_.set(
clientName,
new v1[clientName](this.options as ClientOptions),
new v1.DatabaseAdminClient(this.options as ClientOptions),
);
}
return this.clients_.get(clientName)! as v1.DatabaseAdminClient;
Expand Down Expand Up @@ -615,10 +625,9 @@

if (callback) {
// process.nextTick prevents Unhandled Promise Rejections if callback throws
res.then(
() => process.nextTick(() => callback(null)),
err => process.nextTick(() => callback(err)),
);
res
.then(() => process.nextTick(() => callback(null)))

Check warning on line 629 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
.catch(err => process.nextTick(() => callback(err)));

Check warning on line 630 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid calling back inside of a promise
} else {
return res;
}
Expand Down Expand Up @@ -1727,7 +1736,10 @@
const clientName = config.client;
try {
if (!this.clients_.has(clientName)) {
this.clients_.set(clientName, new v1[clientName](this.options));
this.clients_.set(
clientName,
new (v1 as Record<string, any>)[clientName](this.options),

Check warning on line 1741 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
);
}
} catch (err) {
callback(err, null);
Expand Down Expand Up @@ -1773,7 +1785,7 @@
// Attach the x-goog-spanner-request-id to the currently active span.
attributeXGoogSpannerRequestIdToActiveSpan(config);
}
const interceptors: any[] = [];

Check warning on line 1788 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (this._metricsEnabled) {
interceptors.push(MetricInterceptor);
}
Expand Down Expand Up @@ -1828,7 +1840,7 @@
}

return new Promise((resolve, reject) => {
requestFn(...args)

Check warning on line 1843 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid using promises inside of callbacks

Check warning on line 1843 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid using promises inside of callbacks
.then(resolve)
.catch(err => {
injectRequestIDIntoError(config, err as Error);
Expand Down Expand Up @@ -1892,10 +1904,11 @@
} else {
const result = requestFn();
if (result && typeof result.then === 'function') {
result

Check warning on line 1907 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid using promises inside of callbacks

Check warning on line 1907 in handwritten/spanner/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Avoid using promises inside of callbacks
.then(val => {
metricsTracer?.recordOperationCompletion();
resolve(val);
return val;
})
.catch(error => {
metricsTracer?.recordOperationCompletion();
Expand Down
18 changes: 18 additions & 0 deletions handwritten/spanner/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
scopes: [],
grpc,
'grpc.keepalive_time_ms': 120000,
'grpc.enable_channelz': 0,
'grpc.callInvocationTransformer':
fakeGrpcGcp().gcpCallInvocationTransformer,
'grpc.channelFactoryOverride': fakeGrpcGcp().gcpChannelFactoryOverride,
Expand Down Expand Up @@ -295,6 +296,22 @@
);
});

it('should disable channelz by default and allow overriding it', () => {
const spannerDefault = new Spanner(OPTIONS);
assert.strictEqual(
(spannerDefault.options as any)['grpc.enable_channelz'],

Check warning on line 302 in handwritten/spanner/test/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
0,
);

const spannerEnabled = new Spanner(
Object.assign({}, OPTIONS, {'grpc.enable_channelz': 1}),
);
assert.strictEqual(
(spannerEnabled.options as any)['grpc.enable_channelz'],
1,
);
});

it('should inherit from GrpcService', () => {
assert(spanner instanceof FakeGrpcService);

Expand Down Expand Up @@ -2314,6 +2331,7 @@

return spanner.request(CONFIG).then(result => {
assert.strictEqual(result, gapicRequestFnResult);
return result;
});
});
});
Expand Down
44 changes: 44 additions & 0 deletions handwritten/spanner/test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
Snapshot,
Spanner,
Transaction,
v1 as gapicV1,
} from '../src';
import * as mock from './mockserver/mockspanner';
import {
Expand Down Expand Up @@ -386,6 +387,49 @@ describe('Spanner with mock server', () => {
assert.notStrictEqual(dbWithDefaultOptions, dbWithWriteSessions);
});

it('should disable channelz by default and allow overriding it', async () => {
assert.strictEqual((spanner.options as any)['grpc.enable_channelz'], 0);

const client = new gapicV1.SpannerClient(spanner.options as any);
await client.initialize();
const stub = (await (client as any).spannerStub) as any;
const channel = stub.getChannel();
const pooledChannel = channel.channelRefs?.[0]?.channel as any;
assert.ok(pooledChannel, 'Expected pooledChannel to be initialized');
assert.strictEqual(pooledChannel.internalChannel?.channelzEnabled, false);

const customSpanner = new Spanner({
servicePath: 'localhost',
port,
sslCreds: grpc.credentials.createInsecure(),
'grpc.enable_channelz': 1,
});
try {
assert.strictEqual(
(customSpanner.options as any)['grpc.enable_channelz'],
1,
);
const customClient = new gapicV1.SpannerClient(
customSpanner.options as any,
);
await customClient.initialize();
const customStub = (await (customClient as any).spannerStub) as any;
const customChannel = customStub.getChannel();
const customPooledChannel = customChannel.channelRefs?.[0]
?.channel as any;
assert.ok(
customPooledChannel,
'Expected customPooledChannel to be initialized',
);
assert.strictEqual(
customPooledChannel.internalChannel?.channelzEnabled,
true,
);
} finally {
await customSpanner.close();
}
});
Comment thread
olavloite marked this conversation as resolved.

it('should execute query', async () => {
// The query to execute
const query = {
Expand Down
Loading