Skip to content

Commit b378e2e

Browse files
fix: provide option to disable built in metrics (#2380)
* fix: provide option to disable built in metrics * fix: provide option to disable built in metrics * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * review comments * changed isEmulator variable to isInSecureCredentials --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 75dc4da commit b378e2e

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
matrix:
1212
node: [18, 20, 22, 24]
1313
steps:
14-
- uses: actions/checkout@v5
14+
- uses: actions/checkout@v4
1515
- uses: actions/setup-node@v4
1616
with:
1717
node-version: ${{ matrix.node }}
@@ -29,7 +29,7 @@ jobs:
2929
test-script:
3030
runs-on: ubuntu-latest
3131
steps:
32-
- uses: actions/checkout@v5
32+
- uses: actions/checkout@v4
3333
- uses: actions/setup-node@v4
3434
with:
3535
node-version: 18
@@ -43,7 +43,7 @@ jobs:
4343
windows:
4444
runs-on: windows-latest
4545
steps:
46-
- uses: actions/checkout@v5
46+
- uses: actions/checkout@v4
4747
- uses: actions/setup-node@v4
4848
with:
4949
node-version: 18
@@ -54,7 +54,7 @@ jobs:
5454
lint:
5555
runs-on: ubuntu-latest
5656
steps:
57-
- uses: actions/checkout@v5
57+
- uses: actions/checkout@v4
5858
- uses: actions/setup-node@v4
5959
with:
6060
node-version: 18
@@ -63,7 +63,7 @@ jobs:
6363
docs:
6464
runs-on: ubuntu-latest
6565
steps:
66-
- uses: actions/checkout@v5
66+
- uses: actions/checkout@v4
6767
- uses: actions/setup-node@v4
6868
with:
6969
node-version: 18

.github/workflows/issues-no-repro.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
issues: write
1111
pull-requests: write
1212
steps:
13-
- uses: actions/checkout@v5
13+
- uses: actions/checkout@v4
1414
- uses: actions/setup-node@v4
1515
with:
1616
node-version: 18

.github/workflows/response.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
issues: write
1414
pull-requests: write
1515
steps:
16-
- uses: actions/checkout@v5
16+
- uses: actions/checkout@v4
1717
- uses: actions/github-script@v7
1818
with:
1919
script: |
@@ -27,7 +27,7 @@ jobs:
2727
issues: write
2828
pull-requests: write
2929
steps:
30-
- uses: actions/checkout@v5
30+
- uses: actions/checkout@v4
3131
- uses: actions/github-script@v7
3232
with:
3333
script: |

src/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export type GetInstanceConfigOperationsCallback = PagedCallback<
150150
* Indicates which replicas or regions should be used for non-transactional reads or queries.
151151
* DirectedReadOptions won't be set for readWrite transactions"
152152
* @property {ObservabilityOptions} [observabilityOptions] Sets the observability options to be used for OpenTelemetry tracing
153+
* @property {boolean} [disableBuiltInMetrics=True] If set to true, built-in metrics will be disabled.
153154
*/
154155
export interface SpannerOptions extends GrpcClientOptions {
155156
apiEndpoint?: string;
@@ -160,6 +161,7 @@ export interface SpannerOptions extends GrpcClientOptions {
160161
directedReadOptions?: google.spanner.v1.IDirectedReadOptions | null;
161162
defaultTransactionOptions?: Pick<RunTransactionOptions, 'isolationLevel'>;
162163
observabilityOptions?: ObservabilityOptions;
164+
disableBuiltInMetrics?: boolean;
163165
interceptors?: any[];
164166
/**
165167
* The Trusted Cloud Domain (TPC) DNS of the service used to make requests.
@@ -315,7 +317,7 @@ class Spanner extends GrpcService {
315317
defaultTransactionOptions: RunTransactionOptions;
316318
_observabilityOptions: ObservabilityOptions | undefined;
317319
private _universeDomain: string;
318-
private _isEmulatorEnabled: boolean;
320+
private _isInSecureCredentials: boolean;
319321
private static _isAFEServerTimingEnabled: boolean | undefined;
320322
readonly _nthClientId: number;
321323

@@ -443,14 +445,12 @@ class Spanner extends GrpcService {
443445
);
444446
}
445447

446-
let isEmulatorEnabled = false;
447448
const emulatorHost = Spanner.getSpannerEmulatorHost();
448449
if (
449450
emulatorHost &&
450451
emulatorHost.endpoint &&
451452
emulatorHost.endpoint.length > 0
452453
) {
453-
isEmulatorEnabled = true;
454454
options.servicePath = emulatorHost.endpoint;
455455
options.port = emulatorHost.port;
456456
options.sslCreds = grpc.credentials.createInsecure();
@@ -477,7 +477,7 @@ class Spanner extends GrpcService {
477477
this.routeToLeaderEnabled = false;
478478
}
479479

480-
this._isEmulatorEnabled = isEmulatorEnabled;
480+
this._isInSecureCredentials = options.sslCreds?._isSecure() === false;
481481
this.options = options;
482482
this.auth = new GoogleAuth(this.options);
483483
this.clients_ = new Map();
@@ -495,7 +495,7 @@ class Spanner extends GrpcService {
495495
ensureInitialContextManagerSet();
496496
this._nthClientId = nextSpannerClientId();
497497
this._universeDomain = universeEndpoint;
498-
this.configureMetrics_();
498+
this.configureMetrics_(options.disableBuiltInMetrics);
499499
}
500500

501501
get universeDomain() {
@@ -1614,10 +1614,11 @@ class Spanner extends GrpcService {
16141614
/**
16151615
* Setup the OpenTelemetry metrics capturing for service metrics to Google Cloud Monitoring.
16161616
*/
1617-
configureMetrics_() {
1617+
configureMetrics_(disableBuiltInMetrics?: boolean) {
16181618
const metricsEnabled =
16191619
process.env.SPANNER_DISABLE_BUILTIN_METRICS !== 'true' &&
1620-
!this._isEmulatorEnabled;
1620+
!disableBuiltInMetrics &&
1621+
!this._isInSecureCredentials;
16211622
MetricsTracerFactory.enabled = metricsEnabled;
16221623
if (metricsEnabled) {
16231624
const factory = MetricsTracerFactory.getInstance(this.projectId);

test/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ describe('Spanner', () => {
327327
assert.strictEqual(spanner.routeToLeaderEnabled, false);
328328
});
329329

330+
it('should optionally accept disableBuiltInMetrics', () => {
331+
const spanner = new Spanner({disableBuiltInMetrics: true});
332+
assert.strictEqual(MetricsTracerFactory.enabled, false);
333+
MetricsTracerFactory.enabled = true; // Reset for other tests.
334+
});
335+
330336
it('should optionally accept directedReadOptions', () => {
331337
const fakeDirectedReadOptions = {
332338
includeReplicas: {

test/metrics/metrics.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ describe('Test metrics with mock server', () => {
209209
spannerMock.resetRequests();
210210
spannerMock.removeExecutionTimes();
211211
// Reset the MetricsFactoryReader to an in-memory reader for the tests
212+
MetricsTracerFactory.enabled = true;
212213
factory = MetricsTracerFactory.getInstance();
213214
await factory!.resetMeterProvider();
214215
reader = new InMemoryMetricReader();

0 commit comments

Comments
 (0)