-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracer): instrument fetch requests (#2293)
* feat(tracer): capture fetch requests * chore: clean up code * chore: swap dummy url in tests * chore: use undici-types * chore: integration tests * tests: update tests * docs: update docs to mention fetch * tests: update integration tests for fetch * improv: handle failed connection case * chore: removed leftover file * Trigger Build --------- Co-authored-by: Alexander Schueren <[email protected]>
- Loading branch information
1 parent
082b626
commit cc34400
Showing
11 changed files
with
548 additions
and
20 deletions.
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
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||
import axios from 'axios'; // (1) | ||
|
||
new Tracer({ serviceName: 'serverlessAirline' }); | ||
|
||
export const handler = async ( | ||
_event: unknown, | ||
_context: unknown | ||
): Promise<void> => { | ||
await axios.get('https://httpbin.org/status/200'); | ||
await fetch('https://httpbin.org/status/200'); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { HttpSubsegment } from '../types/ProviderService.js'; | ||
import type { Segment, Subsegment } from 'aws-xray-sdk-core'; | ||
import { URL } from 'node:url'; | ||
|
||
const decoder = new TextDecoder(); | ||
|
||
/** | ||
* The `fetch` implementation based on `undici` includes the headers as an array of encoded key-value pairs. | ||
* This function finds the header with the given key and decodes the value. | ||
* | ||
* The function walks through the array of encoded headers and decodes the key of each pair. | ||
* If the key matches the given key, the function returns the decoded value of the next element in the array. | ||
* | ||
* @param encodedHeaders The array of encoded headers | ||
* @param key The key to search for | ||
*/ | ||
const findHeaderAndDecode = ( | ||
encodedHeaders: Uint8Array[], | ||
key: string | ||
): string | null => { | ||
let foundIndex = -1; | ||
for (let i = 0; i < encodedHeaders.length; i += 2) { | ||
const header = decoder.decode(encodedHeaders[i]); | ||
if (header.toLowerCase() === key) { | ||
foundIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
if (foundIndex === -1) { | ||
return null; | ||
} | ||
|
||
return decoder.decode(encodedHeaders[foundIndex + 1]); | ||
}; | ||
|
||
/** | ||
* Type guard to check if the given subsegment is an `HttpSubsegment` | ||
* | ||
* @param subsegment The subsegment to check | ||
*/ | ||
const isHttpSubsegment = ( | ||
subsegment: Segment | Subsegment | undefined | ||
): subsegment is HttpSubsegment => { | ||
return ( | ||
subsegment !== undefined && | ||
'http' in subsegment && | ||
'parent' in subsegment && | ||
'namespace' in subsegment && | ||
subsegment.namespace === 'remote' | ||
); | ||
}; | ||
|
||
/** | ||
* Convert the origin url to a URL object when it is a string | ||
* | ||
* @param origin The origin url | ||
*/ | ||
const getOriginURL = (origin: string | URL): URL => { | ||
return origin instanceof URL ? origin : new URL(origin); | ||
}; | ||
|
||
export { findHeaderAndDecode, isHttpSubsegment, getOriginURL }; |
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
Oops, something went wrong.