forked from signalfx/splunk-otel-react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
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: Capture request, response headers and body #9
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06a93a2
feat: Capture request, response headers and body
ernestii 0d368d0
remove try-catch
ernestii 809d478
max body size + try catch on stringify
ernestii a4e97ad
normalize header key and value according to otel
ernestii e062874
increase max body size
ernestii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hyperdx/otel-react-native': patch | ||
--- | ||
|
||
feat: Capture request headers, request body and response body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,14 @@ import type { PropagateTraceHeaderCorsUrls } from '@opentelemetry/sdk-trace-web/ | |
|
||
const parseUrl = (url: string) => new URL(url); | ||
|
||
const MAX_BODY_LENGTH = 5 * 1024; // 5KB | ||
|
||
interface XhrConfig { | ||
clearTimingResources?: boolean; | ||
ignoreUrls: Array<string | RegExp> | undefined; | ||
propagateTraceHeaderCorsUrls?: (string | RegExp)[]; | ||
networkHeadersCapture?: boolean; | ||
networkBodyCapture?: boolean; | ||
} | ||
|
||
class TaskCounter { | ||
|
@@ -570,6 +573,7 @@ export function instrumentXHROriginal(config: XhrConfig) { | |
export function instrumentXHR(config: XhrConfig) { | ||
const originalOpen = XMLHttpRequest.prototype.open; | ||
const originalSend = XMLHttpRequest.prototype.send; | ||
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; | ||
|
||
const tracer = api.trace.getTracer('xhr'); | ||
const taskCounter = new TaskCounter(); | ||
|
@@ -662,31 +666,47 @@ export function instrumentXHR(config: XhrConfig) { | |
} | ||
} | ||
|
||
function _normalizeHeaders( | ||
type: string, | ||
headersString: string | ||
): { [key: string]: string } { | ||
function _normalizeHeader([key, value]: [string, string]): Record< | ||
string, | ||
api.AttributeValue | ||
> { | ||
const normalizedKey = key.toLowerCase().replace(/-/g, '_').trim(); | ||
let normalizedValue: api.AttributeValue; | ||
|
||
// https://github.com/open-telemetry/opentelemetry-js/blob/82b7526b028a34a23936016768f37df05effcd59/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts#L604C1-L611C1 | ||
if (typeof value === 'string') { | ||
normalizedValue = [value]; | ||
} else if (Array.isArray(value)) { | ||
normalizedValue = value; | ||
} else { | ||
normalizedValue = [value]; | ||
} | ||
return { [normalizedKey]: normalizedValue }; | ||
} | ||
|
||
function _normalizeHeaders(headersString: string): { | ||
[key: string]: api.AttributeValue; | ||
} { | ||
const lines = headersString.trim().split('\n'); | ||
const normalizedHeaders: { [key: string]: string } = {}; | ||
const normalizedHeaders: { [key: string]: api.AttributeValue } = {}; | ||
|
||
lines.forEach((line) => { | ||
let [key, value] = line.split(/:\s*/); | ||
let [key, value] = line.trim().split(/:\s*/); | ||
if (key && value) { | ||
key = key.replace(/-/g, '_').toLowerCase(); | ||
const newKey = `http.${type}.header.${key}`; | ||
normalizedHeaders[newKey] = value.trim(); | ||
Object.assign(normalizedHeaders, _normalizeHeader([key, value])); | ||
} | ||
}); | ||
|
||
return normalizedHeaders; | ||
} | ||
|
||
function _setHeaderAttributeForSpan( | ||
normalizedHeader: { [key: string]: string }, | ||
normalizedHeaders: { [key: string]: api.AttributeValue }, | ||
type: 'request' | 'response', | ||
span: api.Span | ||
) { | ||
Object.entries(normalizedHeader).forEach(([key, value]) => { | ||
span.setAttribute(key, value as api.AttributeValue); | ||
Object.entries(normalizedHeaders).forEach(([key, value]) => { | ||
span.setAttribute(`http.${type}.header.${key}`, value); | ||
}); | ||
} | ||
|
||
|
@@ -722,12 +742,30 @@ export function instrumentXHR(config: XhrConfig) { | |
|
||
function _handleHeaderCapture(headers: string, currentSpan: api.Span) { | ||
if (config.networkHeadersCapture) { | ||
const normalizedHeaders = _normalizeHeaders('response', headers); | ||
_setHeaderAttributeForSpan(normalizedHeaders, currentSpan); | ||
const normalizedHeaders = _normalizeHeaders(headers); | ||
_setHeaderAttributeForSpan(normalizedHeaders, 'response', currentSpan); | ||
} | ||
} | ||
|
||
if (config.networkHeadersCapture) { | ||
XMLHttpRequest.prototype.setRequestHeader = function ( | ||
this: XMLHttpRequest, | ||
...args | ||
) { | ||
const [key, value] = args; | ||
const xhrMem = _xhrMem.get(this); | ||
if (xhrMem && key && value) { | ||
const normalizedHeader = _normalizeHeader([key, value]); | ||
// TODO: Store and dedupe the headers before adding them to the span | ||
_setHeaderAttributeForSpan(normalizedHeader, 'request', xhrMem.span); | ||
} | ||
originalSetRequestHeader.apply(this, args); | ||
}; | ||
} | ||
|
||
XMLHttpRequest.prototype.send = function (this: XMLHttpRequest, ...args) { | ||
const requestBody = args[0]; | ||
|
||
const xhrMem = _xhrMem.get(this); | ||
if (!xhrMem) { | ||
return originalSend.apply(this, args); | ||
|
@@ -742,6 +780,22 @@ export function instrumentXHR(config: XhrConfig) { | |
taskCounter.increment(); | ||
xhrMem.sendStartTime = hrTime(); | ||
currentSpan.addEvent(EventNames.METHOD_SEND); | ||
if (config.networkBodyCapture) { | ||
let body: string = ''; | ||
if (typeof requestBody === 'string') { | ||
body = requestBody; | ||
} else { | ||
try { | ||
body = JSON.stringify(requestBody); | ||
} catch (e) { | ||
body = '[object of type ' + typeof requestBody + ']'; | ||
} | ||
} | ||
currentSpan.setAttribute( | ||
'http.request.body', | ||
body.slice(0, MAX_BODY_LENGTH) | ||
); | ||
} | ||
this.addEventListener('readystatechange', () => { | ||
if (this.readyState === XMLHttpRequest.HEADERS_RECEIVED) { | ||
const headers = this.getAllResponseHeaders().toLowerCase(); | ||
|
@@ -753,7 +807,24 @@ export function instrumentXHR(config: XhrConfig) { | |
} | ||
} | ||
} else if (this.readyState === XMLHttpRequest.DONE) { | ||
endSpan(EventNames.EVENT_READY_STATE_CHANGE, this); | ||
if (config.networkBodyCapture && this.responseType === 'blob') { | ||
new Response(this.response) | ||
.text() | ||
.then((text) => { | ||
currentSpan.setAttribute('http.response.body', text); | ||
}) | ||
.finally(() => { | ||
endSpan(EventNames.EVENT_READY_STATE_CHANGE, this); | ||
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. just double check. the span would never end before this ? 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. it shouldn't |
||
}); | ||
} else { | ||
if (config.networkBodyCapture) { | ||
currentSpan.setAttribute( | ||
'http.response.body', | ||
this.responseText | ||
); | ||
} | ||
endSpan(EventNames.EVENT_READY_STATE_CHANGE, this); | ||
} | ||
} | ||
}); | ||
addHeaders(this, spanUrl); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
5k seems a bit too strict. maybe something like 2MB to start with is okay
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.
updated