Skip to content

Commit

Permalink
defensive commit: basic test not working
Browse files Browse the repository at this point in the history
opentelemetry fetch instrumentation added, but does not appear to be
working.
  • Loading branch information
therightstuff committed Nov 6, 2023
1 parent 84b05e1 commit 50c5c29
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 0 deletions.
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@opentelemetry/instrumentation": "0.38.0",
"@opentelemetry/instrumentation-amqplib": "0.33.2",
"@opentelemetry/instrumentation-fastify": "^0.32.3",
"@opentelemetry/instrumentation-fetch": "^0.44.0",
"@opentelemetry/instrumentation-grpc": "^0.41.0",
"@opentelemetry/instrumentation-http": "0.38.0",
"@opentelemetry/instrumentation-ioredis": "^0.35.2",
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import LumigoGrpcInstrumentation from './instrumentations/@grpc/grpc-js/GrpcInst
import LumigoAmqplibInstrumentation from './instrumentations/amqplib/AmqplibInstrumentation';
import LumigoExpressInstrumentation from './instrumentations/express/ExpressInstrumentation';
import LumigoFastifyInstrumentation from './instrumentations/fastify/FastifyInstrumentation';
import LumigoFetchInstrumentation from './instrumentations/fetch/FetchInstrumentation';
import LumigoHttpInstrumentation from './instrumentations/https/HttpInstrumentation';
import LumigoIORedisInstrumentation from './instrumentations/ioredis/IORedisInstrumentation';
import LumigoKafkaJsInstrumentation from './instrumentations/kafkajs/KafkaJsInstrumentation';
Expand Down Expand Up @@ -97,6 +98,7 @@ export const init = async (): Promise<LumigoSdkInitialization> => {
new LumigoExpressInstrumentation(),
new LumigoGrpcInstrumentation(),
new LumigoFastifyInstrumentation(),
new LumigoFetchInstrumentation(),
new LumigoHttpInstrumentation(...ignoredHostnames),
new LumigoIORedisInstrumentation(),
new LumigoKafkaJsInstrumentation(),
Expand Down
44 changes: 44 additions & 0 deletions src/instrumentations/fetch/FetchInstrumentation.ts
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
)
);
},*/
});
}
}
19 changes: 19 additions & 0 deletions src/instrumentations/fetch/fetchInstrumentation.test.ts
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' });
});*/
});
1 change: 1 addition & 0 deletions test/instrumentations/fetch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spans
1 change: 1 addition & 0 deletions test/instrumentations/fetch/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
107 changes: 107 additions & 0 deletions test/instrumentations/fetch/app/fetch_app.js
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);
}
});
13 changes: 13 additions & 0 deletions test/instrumentations/fetch/app/package.json
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"
}
}
Loading

0 comments on commit 50c5c29

Please sign in to comment.