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

add custom data to request log when provided #1430

Closed
Closed
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
20 changes: 16 additions & 4 deletions src/utils/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import * as http from 'http';
export type RawHttpRequest = http.IncomingMessage & CloudLoggingHttpRequest;

export interface CustomRequestLoggingData {
[index: string]: string | number | boolean;
}

export interface CloudLoggingHttpRequest {
requestMethod?: string;
requestUrl?: string;
Expand All @@ -41,13 +45,15 @@ export interface CloudLoggingHttpRequest {
cacheValidatedWithOriginServer?: boolean;
cacheFillBytes?: number;
protocol?: string;
customData?: CustomRequestLoggingData;
}

/**
* Abstraction of http.IncomingMessage used by middleware implementation.
*/
export interface ServerRequest extends http.IncomingMessage {
originalUrl: string;
originalUrl?: string;
customRequestLoggingData?: CustomRequestLoggingData;
}

/**
Expand All @@ -59,7 +65,7 @@ export interface ServerRequest extends http.IncomingMessage {
* @param latencyMilliseconds
*/
export function makeHttpRequestData(
req: ServerRequest | http.IncomingMessage,
req: ServerRequest,
res?: http.ServerResponse,
latencyMilliseconds?: number
): CloudLoggingHttpRequest {
Expand All @@ -70,7 +76,9 @@ export function makeHttpRequestData(
referer,
status,
responseSize,
latency;
latency,
customData;

// Format request properties
if (req.url) requestUrl = req.url;
// OriginalURL overwrites inferred url
Expand Down Expand Up @@ -102,6 +110,9 @@ export function makeHttpRequestData(
nanos: Math.floor((latencyMilliseconds % 1e3) * 1e6),
};
}
if (req.customRequestLoggingData) {
customData = req.customRequestLoggingData;
}
// Only include the property if its value exists
return Object.assign(
{},
Expand All @@ -112,7 +123,8 @@ export function makeHttpRequestData(
referer ? {referer} : null,
responseSize ? {responseSize} : null,
status ? {status} : null,
latency ? {latency} : null
latency ? {latency} : null,
customData ? {customData} : null
);
}

Expand Down
16 changes: 16 additions & 0 deletions test/utils/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as http from 'http';
type ServerResponse = http.ServerResponse;
import {
ServerRequest,
CustomRequestLoggingData,
CloudLoggingHttpRequest,
makeHttpRequestData,
isRawHttpRequest,
Expand Down Expand Up @@ -124,6 +125,21 @@ describe('http-request', () => {
);
assert.deepStrictEqual(h3.latency, {seconds: 0, nanos: 1e6});
});
it('should include custom data if provided', () => {
const req = {
method: 'GET',
originalUrl: 'invalid/url/',
customRequestLoggingData: {
'🏃': '👻',
} as CustomRequestLoggingData,
} as ServerRequest;
const cloudReq = makeHttpRequestData(req);
console.log(cloudReq.customData);
assert.strictEqual(cloudReq.protocol, undefined);
assert.strictEqual(cloudReq.requestUrl, 'invalid/url/');
assert.strictEqual(cloudReq.requestMethod, 'GET');
assert.deepEqual(cloudReq.customData, {'🏃': '👻'});
});
});

describe('isRawHttp', () => {
Expand Down