Skip to content

Commit

Permalink
add custom data to request log when provided
Browse files Browse the repository at this point in the history
  • Loading branch information
quixoten committed Jul 12, 2023
1 parent 14f9853 commit ba80b29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit ba80b29

Please sign in to comment.