Skip to content

Commit

Permalink
Teach riviere to use custom host field name PROD-43442 (#108)
Browse files Browse the repository at this point in the history
* Change host to riviereHost

* Rename host to riviereHost

* Remove unused

* Add option for custom host field name

* Fix default value

* Set host as optional

* Fix getOpts
  • Loading branch information
manoskouvarakis authored Jun 21, 2024
1 parent b386218 commit 5222397
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ const httpsProxy = require('./lib/proxies/https').proxy;
const { EVENT } = Loggable;

function buildRiviere(options = {}) {
const { errors = {}, logger, inbound, outbound, traceHeaderName, headersRegex, headerValueCallback } = defaultsDeep(
options,
defaultOptions(options)
);
const {
errors = {},
logger,
inbound,
outbound,
traceHeaderName,
headersRegex,
headerValueCallback,
hostFieldName
} = defaultsDeep(options, defaultOptions(options));

const loggable = new Loggable(options);

Expand All @@ -26,6 +32,7 @@ function buildRiviere(options = {}) {
bodyKeys: options.bodyKeys,
bodyKeysRegex: options.bodyKeysRegex,
bodyKeysCallback: options.bodyKeysCallback,
hostFieldName,
headerValueCallback,
...outbound
}
Expand Down
1 change: 1 addition & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = (options = {}) => {
bodyKeysCallback: undefined,
headersRegex: new RegExp('^X-.*', 'i'),
headerValueCallback: undefined,
hostFieldName: 'host',
traceHeaderName: 'X-Riviere-Id',
forceIds: true
};
Expand Down
11 changes: 8 additions & 3 deletions lib/transformers/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ const mapInRes = (res, req, startedAt, reqId, opts) => {
let userAgent = getUserAgent(res.headers);

const headers = extractHeaders(opts.headersRegex, opts.headerValueCallback, res.headers);
const hostFieldName = opts.hostFieldName || 'host';

return {
...pick(req, ['method', 'protocol', 'host', 'path', 'query', 'href']),
...pick(req, ['method', 'protocol', hostFieldName, 'path', 'query', 'href']),
status,
duration,
requestId: reqId,
Expand All @@ -143,6 +145,7 @@ const mapOutReq = (requestOptions, reqId, opts = {}) => {
const requestId = reqId;
const headersRegex = opts.headersRegex;
const headerValueCallback = opts.headerValueCallback;
const hostFieldName = opts.hostFieldName || 'host';

const { protocol, host, path, query, href } = getUrlParameters(requestOptions);

Expand Down Expand Up @@ -189,11 +192,10 @@ const mapOutReq = (requestOptions, reqId, opts = {}) => {

let contentLength = getContentLength(requestOptions.headers);

return {
const returnObject = {
metaHeaders,
method,
protocol,
host,
port,
metaBody,
path: slicedPath,
Expand All @@ -203,6 +205,9 @@ const mapOutReq = (requestOptions, reqId, opts = {}) => {
contentLength,
log_tag: logTags.CATEGORY.OUTBOUND_REQUEST.TAG
};
returnObject[hostFieldName] = host;

return returnObject;
};

function extractRequestMeta(
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/getOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const getOpts = sandbox => {
level: 'info',
enabled: true
},
hostFieldName: 'host',
traceHeaderName: 'X-Ap-ID'
};
};
Expand Down
54 changes: 32 additions & 22 deletions test/lib/adapters/default/requestProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('#defaultAdapter', () => {
request: {
enabled: true
},
headersRegex: new RegExp('^X-.*', 'i')
headersRegex: new RegExp('^X-.*', 'i'),
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('#defaultAdapter', () => {
query: 'some=something',
requestId: 'ok',
href: 'http://some-host:8080/some',
host: 'some-host',
myHost: 'some-host',
metaBody: {},
metaHeaders: {},
contentLength: 0,
Expand All @@ -80,7 +81,7 @@ describe('#defaultAdapter', () => {
logger.info.args[1][0].should.eql({
method: 'GET',
path: '/some',
host: 'some-host',
myHost: 'some-host',
duration: 0,
query: 'some=something',
href: 'http://some-host:8080/some',
Expand Down Expand Up @@ -114,7 +115,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -177,7 +179,8 @@ describe('#defaultAdapter', () => {
blacklistedPathRegex: new RegExp('^/v4.0/traces$'),
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -221,7 +224,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -255,7 +259,7 @@ describe('#defaultAdapter', () => {
path: '/some?somequery=query',
query: undefined,
requestId: 'ok',
host: 'test-host',
myHost: 'test-host',
protocol: 'https',
href: 'https://test-host:8080/some%3Fsomequery=query',
metaBody: {},
Expand All @@ -267,7 +271,7 @@ describe('#defaultAdapter', () => {
method: 'GET',
path: '/some?somequery=query',
status: 200,
host: 'test-host',
myHost: 'test-host',
protocol: 'https',
href: 'https://test-host:8080/some%3Fsomequery=query',
duration: 0,
Expand Down Expand Up @@ -295,7 +299,8 @@ describe('#defaultAdapter', () => {
headersRegex: new RegExp('test', 'i'),
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -327,7 +332,7 @@ describe('#defaultAdapter', () => {
logger.info.args[0][0].should.containEql({
method: 'GET',
protocol: 'https',
host: 'test-host',
myHost: 'test-host',
port: '8080',
path: '/some?somequery=query',
query: undefined,
Expand All @@ -337,7 +342,7 @@ describe('#defaultAdapter', () => {
});
logger.info.args[1][0].should.containEql({
method: 'GET',
host: 'test-host',
myHost: 'test-host',
path: '/some?somequery=query',
status: 200,
duration: 0,
Expand Down Expand Up @@ -434,7 +439,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -465,7 +471,7 @@ describe('#defaultAdapter', () => {
logger.info.args[0][0].should.eql({
method: 'GET',
protocol: 'https',
host: 'test-host',
myHost: 'test-host',
port: '8080',
path: '/some?somequery=query',
query: undefined,
Expand All @@ -482,7 +488,7 @@ describe('#defaultAdapter', () => {
status: 200,
duration: 0,
href: 'https://test-host/some%3Fsomequery=query',
host: 'test-host',
myHost: 'test-host',
query: undefined,
protocol: 'https',
requestId: 'cff07fc2-4ef6-42b6-9a74-ba3abf8b31a2',
Expand All @@ -509,7 +515,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = null;
Expand Down Expand Up @@ -542,7 +549,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -572,7 +580,7 @@ describe('#defaultAdapter', () => {
logger.info.args[0][0].should.eql({
method: 'GET',
protocol: 'http',
host: 'some-host',
myHost: 'some-host',
href: 'http://some-host:8080/some',
port: '8080',
path: '/some',
Expand All @@ -599,7 +607,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: true
}
},
hostFieldName: 'myHost'
}
});
const http = {
Expand Down Expand Up @@ -635,14 +644,14 @@ describe('#defaultAdapter', () => {
path: '/some',
query: 'some=something',
href: 'http://some-host/some',
host: 'some-host',
myHost: 'some-host',
metaBody: {},
log_tag: 'outbound_request'
});
logger.info.args[1][0].should.containEql({
method: 'GET',
path: '/some',
host: 'some-host',
myHost: 'some-host',
duration: 0,
query: 'some=something',
status: undefined,
Expand Down Expand Up @@ -670,7 +679,8 @@ describe('#defaultAdapter', () => {
opts: {
request: {
enabled: false
}
},
hostFieldName: 'myHost'
}
});
const options = {
Expand Down Expand Up @@ -705,7 +715,7 @@ describe('#defaultAdapter', () => {
logger.info.args[0][0].should.eql({
method: 'GET',
path: '/some',
host: 'some-host',
myHost: 'some-host',
duration: 0,
query: 'some=something',
href: 'http://some-host:8080/some',
Expand Down
5 changes: 3 additions & 2 deletions test/lib/transformers/transformersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ describe('mapOutReq', () => {
}
};
const result = mapOutReq(inMsg, undefined, {
headersRegex: new RegExp('^X-.*', 'i')
headersRegex: new RegExp('^X-.*', 'i'),
hostFieldName: 'test_host'
});
result.should.eql({
method: undefined,
protocol: 'http',
host: 'hostname',
test_host: 'hostname',
port: undefined,
path: 'path',
query: 'query',
Expand Down
1 change: 1 addition & 0 deletions types/riviere.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function riviere(options?: {
bodyKeysCallback?: (body: any, ctx?: any) => any,
headersRegex?: RegExp,
headerValueCallback?: (key: string, value: any) => any,
hostFieldName?: string,
traceHeaderName?: string,
forceIds?: boolean
}): any;

0 comments on commit 5222397

Please sign in to comment.