Skip to content

Commit 5083920

Browse files
feat: add header for deno runtime for metrics tracking (#2220)
* feat: add header for deno runtime for metrics tracking * Update src/util.ts Co-authored-by: Ruy Adorno <[email protected]> * linter fix --------- Co-authored-by: Ruy Adorno <[email protected]>
1 parent 2c567b0 commit 5083920

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

src/nodejs-common/service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
PackageJson,
2727
util,
2828
} from './util';
29+
import {getRuntimeTrackingString} from '../util';
2930

3031
export const DEFAULT_PROJECT_ID_TOKEN = '{{projectId}}';
3132

@@ -246,7 +247,7 @@ export class Service {
246247
}
247248
reqOpts.headers = extend({}, reqOpts.headers, {
248249
'User-Agent': userAgent,
249-
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${
250+
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
250251
pkg.version
251252
} gccl-invocation-id/${uuid.v4()}`,
252253
});

src/nodejs-common/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {teenyRequest} from 'teeny-request';
3333
import {Interceptor} from './service-object';
3434
import * as uuid from 'uuid';
3535
import {DEFAULT_PROJECT_ID_TOKEN} from './service';
36+
import {getRuntimeTrackingString} from '../util';
3637

3738
const packageJson = require('../../../package.json');
3839

@@ -1011,7 +1012,7 @@ export class Util {
10111012
_getDefaultHeaders() {
10121013
return {
10131014
'User-Agent': util.getUserAgentFromPackageJson(packageJson),
1014-
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${
1015+
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
10151016
packageJson.version
10161017
} gccl-invocation-id/${uuid.v4()}`,
10171018
};

src/resumable-upload.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {Readable, Writable, WritableOptions} from 'stream';
2727
import retry = require('async-retry');
2828
import {RetryOptions, PreconditionOptions} from './storage';
2929
import * as uuid from 'uuid';
30+
import {getRuntimeTrackingString} from './util';
3031

3132
const NOT_FOUND_STATUS_CODE = 404;
3233
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
@@ -597,7 +598,9 @@ export class Upload extends Writable {
597598
),
598599
data: metadata,
599600
headers: {
600-
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.uri}`,
601+
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
602+
packageJson.version
603+
} gccl-invocation-id/${this.currentInvocationId.uri}`,
601604
...headers,
602605
},
603606
};
@@ -764,7 +767,9 @@ export class Upload extends Writable {
764767
});
765768

766769
const headers: GaxiosOptions['headers'] = {
767-
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.chunk}`,
770+
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
771+
packageJson.version
772+
} gccl-invocation-id/${this.currentInvocationId.chunk}`,
768773
};
769774

770775
// If using multiple chunk upload, set appropriate header
@@ -905,7 +910,9 @@ export class Upload extends Writable {
905910
headers: {
906911
'Content-Length': 0,
907912
'Content-Range': 'bytes */*',
908-
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.offset}`,
913+
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
914+
packageJson.version
915+
} gccl-invocation-id/${this.currentInvocationId.offset}`,
909916
},
910917
};
911918
try {

src/util.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ export function formatAsUTCISO(
168168
return resultString;
169169
}
170170

171+
/**
172+
* Examines the runtime environment and returns the appropriate tracking string.
173+
* @returns {string} metrics tracking string based on the current runtime environment.
174+
*/
175+
export function getRuntimeTrackingString(): string {
176+
if (
177+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
178+
// @ts-ignore
179+
globalThis.Deno &&
180+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
181+
// @ts-ignore
182+
globalThis.Deno.version &&
183+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
184+
// @ts-ignore
185+
globalThis.Deno.version.deno
186+
) {
187+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
188+
// @ts-ignore
189+
return `gl-deno/${globalThis.Deno.version.deno}`;
190+
} else {
191+
return `gl-node/${process.versions.node}`;
192+
}
193+
}
194+
171195
export class PassThroughShim extends PassThrough {
172196
private shouldEmitReading = true;
173197
private shouldEmitWriting = true;

test/headers.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ describe('headers', () => {
5151
},
5252
});
5353

54-
it('populates x-goog-api-client header', async () => {
54+
afterEach(() => {
55+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
56+
// @ts-ignore
57+
globalThis.Deno = undefined;
58+
});
59+
60+
it('populates x-goog-api-client header (node)', async () => {
5561
const storage = new Storage();
5662
const bucket = storage.bucket('foo-bucket');
5763
try {
@@ -65,4 +71,26 @@ describe('headers', () => {
6571
)
6672
);
6773
});
74+
75+
it('populates x-goog-api-client header (deno)', async () => {
76+
const storage = new Storage();
77+
const bucket = storage.bucket('foo-bucket');
78+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
79+
// @ts-ignore
80+
globalThis.Deno = {
81+
version: {
82+
deno: '0.00.0',
83+
},
84+
};
85+
try {
86+
await bucket.create();
87+
} catch (err) {
88+
if (err !== error) throw err;
89+
}
90+
assert.ok(
91+
/^gl-deno\/0.00.0 gccl\/(?<gccl>[^W]+) gccl-invocation-id\/(?<gcclInvocationId>[^W]+)$/.test(
92+
requests[1].headers['x-goog-api-client']
93+
)
94+
);
95+
});
6896
});

0 commit comments

Comments
 (0)