Skip to content

Commit

Permalink
feat: add header for deno runtime for metrics tracking (#2220)
Browse files Browse the repository at this point in the history
* feat: add header for deno runtime for metrics tracking

* Update src/util.ts

Co-authored-by: Ruy Adorno <ruyadorno@google.com>

* linter fix

---------

Co-authored-by: Ruy Adorno <ruyadorno@google.com>
  • Loading branch information
ddelgrosso1 and ruyadorno committed Jul 13, 2023
1 parent 2c567b0 commit 5083920
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/nodejs-common/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
PackageJson,
util,
} from './util';
import {getRuntimeTrackingString} from '../util';

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

Expand Down Expand Up @@ -246,7 +247,7 @@ export class Service {
}
reqOpts.headers = extend({}, reqOpts.headers, {
'User-Agent': userAgent,
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
pkg.version
} gccl-invocation-id/${uuid.v4()}`,
});
Expand Down
3 changes: 2 additions & 1 deletion src/nodejs-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {teenyRequest} from 'teeny-request';
import {Interceptor} from './service-object';
import * as uuid from 'uuid';
import {DEFAULT_PROJECT_ID_TOKEN} from './service';
import {getRuntimeTrackingString} from '../util';

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

Expand Down Expand Up @@ -1011,7 +1012,7 @@ export class Util {
_getDefaultHeaders() {
return {
'User-Agent': util.getUserAgentFromPackageJson(packageJson),
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${uuid.v4()}`,
};
Expand Down
13 changes: 10 additions & 3 deletions src/resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {Readable, Writable, WritableOptions} from 'stream';
import retry = require('async-retry');
import {RetryOptions, PreconditionOptions} from './storage';
import * as uuid from 'uuid';
import {getRuntimeTrackingString} from './util';

const NOT_FOUND_STATUS_CODE = 404;
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
Expand Down Expand Up @@ -597,7 +598,9 @@ export class Upload extends Writable {
),
data: metadata,
headers: {
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.uri}`,
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${this.currentInvocationId.uri}`,
...headers,
},
};
Expand Down Expand Up @@ -764,7 +767,9 @@ export class Upload extends Writable {
});

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

// If using multiple chunk upload, set appropriate header
Expand Down Expand Up @@ -905,7 +910,9 @@ export class Upload extends Writable {
headers: {
'Content-Length': 0,
'Content-Range': 'bytes */*',
'x-goog-api-client': `gl-node/${process.versions.node} gccl/${packageJson.version} gccl-invocation-id/${this.currentInvocationId.offset}`,
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${this.currentInvocationId.offset}`,
},
};
try {
Expand Down
24 changes: 24 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ export function formatAsUTCISO(
return resultString;
}

/**
* Examines the runtime environment and returns the appropriate tracking string.
* @returns {string} metrics tracking string based on the current runtime environment.
*/
export function getRuntimeTrackingString(): string {
if (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.Deno &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.Deno.version &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.Deno.version.deno
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return `gl-deno/${globalThis.Deno.version.deno}`;
} else {
return `gl-node/${process.versions.node}`;
}
}

export class PassThroughShim extends PassThrough {
private shouldEmitReading = true;
private shouldEmitWriting = true;
Expand Down
30 changes: 29 additions & 1 deletion test/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ describe('headers', () => {
},
});

it('populates x-goog-api-client header', async () => {
afterEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.Deno = undefined;
});

it('populates x-goog-api-client header (node)', async () => {
const storage = new Storage();
const bucket = storage.bucket('foo-bucket');
try {
Expand All @@ -65,4 +71,26 @@ describe('headers', () => {
)
);
});

it('populates x-goog-api-client header (deno)', async () => {
const storage = new Storage();
const bucket = storage.bucket('foo-bucket');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.Deno = {
version: {
deno: '0.00.0',
},
};
try {
await bucket.create();
} catch (err) {
if (err !== error) throw err;
}
assert.ok(
/^gl-deno\/0.00.0 gccl\/(?<gccl>[^W]+) gccl-invocation-id\/(?<gcclInvocationId>[^W]+)$/.test(
requests[1].headers['x-goog-api-client']
)
);
});
});

0 comments on commit 5083920

Please sign in to comment.