-
Notifications
You must be signed in to change notification settings - Fork 824
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(http-instrumentation): add content size attributes to spans #1771
Changes from 2 commits
2fff599
4a60254
40c2d2d
3d3295e
513f7c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,66 @@ export const setSpanWithError = ( | |
span.setStatus(status); | ||
}; | ||
|
||
/** | ||
* Adds attributes for request content-length and content-encoding HTTP headers | ||
* @param { IncomingMessage } Request object whose headers will be analyzed | ||
* @param { Attributes } Attributes object to be modified | ||
*/ | ||
export const setRequestContentLengthAttribute = ( | ||
request: IncomingMessage, | ||
attributes: Attributes | ||
) => { | ||
const length = getContentLength(request.headers); | ||
if (length === null) return; | ||
|
||
if (isCompressed(request.headers)) { | ||
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH] = length; | ||
} else { | ||
attributes[HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED] = length; | ||
} | ||
}; | ||
|
||
/** | ||
* Adds attributes for response content-length and content-encoding HTTP headers | ||
* @param { IncomingMessage } Response object whose headers will be analyzed | ||
* @param { Attributes } Attributes object to be modified | ||
*/ | ||
export const setResponseContentLengthAttribute = ( | ||
response: IncomingMessage, | ||
attributes: Attributes | ||
) => { | ||
const length = getContentLength(response.headers); | ||
if (length === null) return; | ||
|
||
if (isCompressed(response.headers)) { | ||
attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH] = length; | ||
} else { | ||
attributes[ | ||
HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED | ||
] = length; | ||
} | ||
}; | ||
|
||
function getContentLength( | ||
headers: OutgoingHttpHeaders | IncomingHttpHeaders | ||
): number | null { | ||
const contentLengthHeader = headers['content-length']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least for outgoing HTTP users may specify headers with any casing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They should be lower-cased by node when set: https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L572 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You refer to the object used by node http internally on symbol But it seems this is no problem because this plugin captures content-size only for server requests (incoming) and client response (also incoming) where node parses and prepares the lowercased headers. |
||
if (contentLengthHeader === undefined) return null; | ||
|
||
const contentLength = parseInt(contentLengthHeader as string, 10); | ||
if (isNaN(contentLength)) return null; | ||
|
||
return contentLength; | ||
} | ||
|
||
export const isCompressed = ( | ||
headers: OutgoingHttpHeaders | IncomingHttpHeaders | ||
): boolean => { | ||
const encoding = headers['content-encoding']; | ||
|
||
return !!encoding && encoding !== 'identity'; | ||
}; | ||
|
||
/** | ||
* Makes sure options is an url object | ||
* return an object with default value and parsed options | ||
|
@@ -326,6 +386,7 @@ export const getOutgoingRequestAttributesOnResponse = ( | |
[GeneralAttribute.NET_PEER_PORT]: remotePort, | ||
[HttpAttribute.HTTP_HOST]: `${options.hostname}:${remotePort}`, | ||
}; | ||
setResponseContentLengthAttribute(response, attributes); | ||
|
||
if (statusCode) { | ||
attributes[HttpAttribute.HTTP_STATUS_CODE] = statusCode; | ||
|
@@ -386,6 +447,7 @@ export const getIncomingRequestAttributes = ( | |
if (userAgent !== undefined) { | ||
attributes[HttpAttribute.HTTP_USER_AGENT] = userAgent; | ||
} | ||
setResponseContentLengthAttribute(request, attributes); | ||
vmarchaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const httpKindAttributes = getAttributesFromHttpKind(httpVersion); | ||
return Object.assign(attributes, httpKindAttributes); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the benefit of these functions?
It seems they are only called from tests but not from the plugin code itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are called just under by
getOutgoingRequestAttributesOnResponse
andgetIncomingRequestAttributes