Skip to content

Commit

Permalink
feat: error handling + types
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 committed Aug 11, 2023
1 parent 0644508 commit c456ccf
Showing 1 changed file with 61 additions and 36 deletions.
97 changes: 61 additions & 36 deletions packages/node-opentelemetry/src/instrumentations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { ClientRequest, IncomingMessage, ServerResponse } from 'http';

import * as shimmer from 'shimmer';
import _ from 'lodash';
import { Span as ISpan } from '@opentelemetry/api';

import {
Logger,
LoggerOptions,
parseWinstonLog,
} from '@hyperdx/node-logger/build/src/logger';

import hdx from './debug';

export const _parseConsoleArgs = (args: any[]) => {
const stringifiedArgs = [];
let firstJson;
Expand Down Expand Up @@ -84,43 +89,63 @@ export const HyperDXHTTPInstrumentationConfig = {
responseHeaders: ALL_HTTP_RESPONSE_HEADERS,
},
},
requestHook: (span, request: any) => {
const chunks = [];
const oldWrite = request.write.bind(request);
request.write = (data: any) => {
chunks.push(Buffer.from(data));
return oldWrite(data);
};
const oldEnd = request.end.bind(request);
request.end = (data: any) => {
if (data) {
chunks.push(Buffer.from(data));
}
if (chunks.length > 0) {
const body = Buffer.concat(chunks).toString('utf8');
span.setAttribute('http.request.body', body);
}
return oldEnd(data);
};
requestHook: (span: ISpan, request: ClientRequest | IncomingMessage) => {
if (request instanceof ClientRequest) {
const chunks = [];
const oldWrite = request.write.bind(request);
request.write = (data: any) => {
try {
chunks.push(Buffer.from(data));
} catch (e) {
hdx(`error in request.write: ${e}`);
}
return oldWrite(data);
};
const oldEnd = request.end.bind(request);
request.end = (data: any) => {
try {
if (data) {
chunks.push(Buffer.from(data));
}
if (chunks.length > 0) {
const body = Buffer.concat(chunks).toString('utf8');
span.setAttribute('http.request.body', body);
}
} catch (e) {
hdx(`error in request.end: ${e}`);
}
return oldEnd(data);
};
}
},
responseHook: (span, response: any) => {
const chunks = [];
const oldWrite = response.write.bind(response);
response.write = (data: any) => {
chunks.push(Buffer.from(data));
return oldWrite(data);
};
const oldEnd = response.end.bind(response);
response.end = (data: any) => {
if (data) {
chunks.push(Buffer.from(data));
}
if (chunks.length > 0) {
const body = Buffer.concat(chunks).toString('utf8');
span.setAttribute('http.response.body', body);
}
return oldEnd(data);
};
responseHook: (span: ISpan, response: ServerResponse | IncomingMessage) => {
if (response instanceof ServerResponse) {
const chunks = [];
const oldWrite = response.write.bind(response);
response.write = (data: any) => {
try {
chunks.push(Buffer.from(data));
} catch (e) {
hdx(`error in response.write: ${e}`);
}
return oldWrite(data);
};
const oldEnd = response.end.bind(response);
response.end = (data: any) => {
try {
if (data) {
chunks.push(Buffer.from(data));
}
if (chunks.length > 0) {
const body = Buffer.concat(chunks).toString('utf8');
span.setAttribute('http.response.body', body);
}
} catch (e) {
hdx(`error in response.end: ${e}`);
}
return oldEnd(data);
};
}
},
};

Expand Down

0 comments on commit c456ccf

Please sign in to comment.