-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
defensive commit: basic test not working
opentelemetry fetch instrumentation added, but does not appear to be working.
- Loading branch information
1 parent
84b05e1
commit 50c5c29
Showing
11 changed files
with
447 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,44 @@ | ||
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch'; | ||
import { Instrumentor } from '../instrumentor'; | ||
|
||
export default class LumigoFetchInstrumentation extends Instrumentor<FetchInstrumentation> { | ||
getInstrumentedModule(): string { | ||
return 'fetch'; | ||
} | ||
|
||
getInstrumentation(): FetchInstrumentation { | ||
return new FetchInstrumentation({ | ||
/*applyCustomAttributesOnSpan: (span: Span, request: Request| | ||
RequestInit, result: Response|FetchError) => { | ||
span.setAttribute('http.method', config.method); | ||
span.setAttribute('http.url', config.url); | ||
}*/ | ||
/*requestHook: function (span: Span, info: FastifyRequestInfo) { | ||
span.setAttribute( | ||
'http.request.headers', | ||
CommonUtils.payloadStringify( | ||
info.request.headers, | ||
ScrubContext.HTTP_REQUEST_HEADERS, | ||
getSpanAttributeMaxLength() | ||
) | ||
); | ||
span.setAttribute( | ||
'http.request.query', | ||
CommonUtils.payloadStringify( | ||
info.request.query, | ||
ScrubContext.HTTP_REQUEST_QUERY, | ||
getSpanAttributeMaxLength() | ||
) | ||
); | ||
span.setAttribute( | ||
'http.request.body', | ||
scrubHttpPayload( | ||
info.request.body, | ||
contentType(info.request.headers), | ||
ScrubContext.HTTP_REQUEST_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import LumigoFetchInstrumentation from './FetchInstrumentation'; | ||
|
||
describe('LumigoFetchInstrumentation', () => { | ||
let lumigoFetchInstrumentation = new LumigoFetchInstrumentation(); | ||
|
||
test('getInstrumentedModule should return "fetch"', () => { | ||
expect(lumigoFetchInstrumentation.getInstrumentedModule()).toEqual('fetch'); | ||
}); | ||
|
||
// should not be skipped, see https://lumigo.atlassian.net/browse/RD-11195 | ||
/*test.skip('requireIfAvailable should return required name', () => { | ||
const child_process = require('child_process'); | ||
child_process.execSync('npm install fastify', { stdio: 'inherit' }); | ||
const fastify = require('fastify'); | ||
expect(lumigoFastifyInstrumentation.requireIfAvailable()).toEqual(fastify); | ||
child_process.execSync('npm uninstall fastify', { stdio: 'inherit' }); | ||
});*/ | ||
}); |
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 @@ | ||
spans |
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 @@ | ||
package-lock.json |
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,107 @@ | ||
const http = require('http'); | ||
const url = require('url'); | ||
require('log-timestamp'); | ||
|
||
const host = 'localhost'; | ||
let httpServer; | ||
|
||
function respond(res, status, body) { | ||
console.log(`responding with ${status} ${JSON.stringify(body)}`); | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.setHeader('access-control-allow-origin', '*'); | ||
res.writeHead(status); | ||
res.end(JSON.stringify(body)); | ||
} | ||
|
||
const requestListener = async function (req, res) { | ||
console.error(`Received request: ${req.method} ${req.url}`); | ||
|
||
const requestUrl = url.parse(req.url, true); | ||
const fetchUrl = requestUrl?.query?.url; | ||
const messageBody = requestUrl?.query?.messageBody; | ||
|
||
let result; | ||
switch (requestUrl.pathname) { | ||
case '/get-json': | ||
try { | ||
result = await fetch(fetchUrl); | ||
console.error(`Received response: ${result.status} ${result.statusText}`); | ||
console.error(`Response headers:`, result.headers); | ||
console.error(`Response body:`, await result.json()); | ||
respond(res, 200, {}); | ||
} catch (err) { | ||
console.error(`Error fetching/printing json result`, err); | ||
respond(res, 500, { error: err }); | ||
} | ||
break; | ||
|
||
case '/get-text': | ||
try { | ||
result = await fetch(fetchUrl); | ||
console.error(`Received response: ${result.status} ${result.statusText}`); | ||
console.error(`Response headers:`, result.headers.raw()); | ||
console.error(`Response body:`, await result.text()); | ||
respond(res, 200, {}); | ||
} catch (err) { | ||
console.error(`Error fetching/printing text result`, err); | ||
respond(res, 500, { error: err }); | ||
} | ||
break; | ||
|
||
case '/post-json': | ||
try { | ||
result = await fetch(fetchUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ message: messageBody }), | ||
}); | ||
console.error(`Received response: ${result.status} ${result.statusText}`); | ||
console.error(`Response headers:`, result.headers.raw()); | ||
console.error(`Response body:`, await result.json()); | ||
respond(res, 200, {}); | ||
} catch (err) { | ||
console.error(`Error fetching/printing json result`, err); | ||
respond(res, 500, { error: err }); | ||
} | ||
break; | ||
|
||
case '/post-text': | ||
try { | ||
result = await fetch(fetchUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'text/plain', | ||
}, | ||
body: JSON.stringify({ message: messageBody }), | ||
}); | ||
console.error(`Received response: ${result.status} ${result.statusText}`); | ||
console.error(`Response headers:`, result.headers.raw()); | ||
console.error(`Response body:`, await result.text()); | ||
respond(res, 200, {}); | ||
} catch (err) { | ||
console.error(`Error fetching/printing text result`, err); | ||
respond(res, 500, { error: err }); | ||
} | ||
break; | ||
|
||
case '/quit': | ||
console.error('Received quit command'); | ||
respond(res, 200, {}); | ||
httpServer.close(); | ||
break; | ||
|
||
default: | ||
respond(res, 404, { error: 'Resource not found' }); | ||
} | ||
}; | ||
|
||
httpServer = http.createServer(requestListener); | ||
httpServer.listen(0, host, () => { | ||
const port = httpServer.address().port; | ||
console.error(`HTTP server listening on port ${port}`); | ||
if (process.send) { | ||
process.send(port); | ||
} | ||
}); |
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,13 @@ | ||
{ | ||
"name": "lumigo-fetch-test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"scripts": { | ||
"start": "node -r @lumigo/opentelemetry fetch_app.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@lumigo/opentelemetry": "file:../../../../distro.tgz" | ||
} | ||
} |
Oops, something went wrong.