From c456ccff62f82d144b070a361291059f70fdab72 Mon Sep 17 00:00:00 2001 From: Warren <5959690+wrn14897@users.noreply.github.com> Date: Fri, 11 Aug 2023 16:48:15 -0700 Subject: [PATCH] feat: error handling + types --- .../src/instrumentations.ts | 97 ++++++++++++------- 1 file changed, 61 insertions(+), 36 deletions(-) diff --git a/packages/node-opentelemetry/src/instrumentations.ts b/packages/node-opentelemetry/src/instrumentations.ts index 8a73cbfe..c89b3056 100644 --- a/packages/node-opentelemetry/src/instrumentations.ts +++ b/packages/node-opentelemetry/src/instrumentations.ts @@ -1,5 +1,8 @@ +import { ClientRequest, IncomingMessage, ServerResponse } from 'http'; + import * as shimmer from 'shimmer'; import _ from 'lodash'; +import { Span as ISpan } from '@opentelemetry/api'; import { Logger, @@ -7,6 +10,8 @@ import { parseWinstonLog, } from '@hyperdx/node-logger/build/src/logger'; +import hdx from './debug'; + export const _parseConsoleArgs = (args: any[]) => { const stringifiedArgs = []; let firstJson; @@ -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); + }; + } }, };