Skip to content

Commit

Permalink
module export
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-friedman committed Feb 16, 2023
1 parent 0d48888 commit a751fbd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/paymentservice/otel-custom/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const HttpExtendedAttribute = {
const HttpExtendedAttribute = {
HTTP_REQUEST_HEADERS: 'http.request.headers',
HTTP_REQUEST_BODY: 'http.request.body',
HTTP_RESPONSE_HEADERS: 'http.response.headers',
HTTP_RESPONSE_BODY: 'http.response.body',
HTTP_PATH: 'http.path',
}

module.exports = {
HttpExtendedAttribute,
};
10 changes: 7 additions & 3 deletions src/paymentservice/otel-custom/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { HttpExtendedAttribute } = require('./constants');
const { shouldCaptureBodyByMimeType } = require('./mime-type');
const { StreamChunks } = require('./stream-chunks');

export const requestHook = (span, req, res) => {
const requestHook = (span, req, res) => {
span.setAttributes({
[HttpExtendedAttribute.HTTP_PATH]: req.path,
[HttpExtendedAttribute.HTTP_REQUEST_HEADERS]: JSON.stringify(req.headers),
Expand All @@ -24,7 +24,7 @@ export const requestHook = (span, req, res) => {
responseStreamChunks.addChunk(chunk);
originalResWrite.apply(res, arguments);
};


const oldResEnd = res.end;
res.end = function (chunk) {
Expand All @@ -50,6 +50,10 @@ export const requestHook = (span, req, res) => {
};
};

export const expressInstrumentationConfig = {
const expressInstrumentationConfig = {
requestHook,
};

module.exports = {
expressInstrumentationConfig
};
6 changes: 5 additions & 1 deletion src/paymentservice/otel-custom/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ const httpCustomAttributesOnResponse = (span, response) => {
}
};

export const httpInstrumentationConfig = {
const httpInstrumentationConfig = {
applyCustomAttributesOnSpan: httpCustomAttributes,
requestHook: httpCustomAttributesOnRequest,
responseHook: httpCustomAttributesOnResponse,
};

module.exports = {
httpInstrumentationConfig
};
6 changes: 5 additions & 1 deletion src/paymentservice/otel-custom/mime-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const allowedMimeTypePrefix = [
'application/xhtml',
];

export const shouldCaptureBodyByMimeType = (mimeType) => {
const shouldCaptureBodyByMimeType = (mimeType) => {
try {
return !mimeType || allowedMimeTypePrefix.some((prefix) => mimeType.startsWith(prefix));
} catch {
return true;
}
};

module.exports = {
shouldCaptureBodyByMimeType,
};
9 changes: 7 additions & 2 deletions src/paymentservice/otel-custom/stream-chunks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// for body with at most this length, full body will be captured.
// for large body with more than this amount of bytes, we will
// collect at least this amount of bytes, but might truncate after it
export const MIN_COLLECTED_BODY_LENGTH = 524288;
const MIN_COLLECTED_BODY_LENGTH = 524288;

export class StreamChunks {
class StreamChunks {
chunks;
length;

Expand All @@ -26,3 +26,8 @@ export class StreamChunks {
return this.chunks.join('');
}
}

module.exports = {
StreamChunks,
MIN_COLLECTED_BODY_LENGTH,
};

0 comments on commit a751fbd

Please sign in to comment.