Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add header for deno runtime for metrics tracking #2220

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
16 changes: 16 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ 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 {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (globalThis.Deno) {
ddelgrosso1 marked this conversation as resolved.
Show resolved Hide resolved
// 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']
)
);
});
});