Skip to content

Commit

Permalink
fix: semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
CircleCI committed Mar 8, 2022
1 parent 984ae19 commit 486af9f
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 157 deletions.
5 changes: 4 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
**/*.js
**/*.js
**/*.test.*s
src/**/jsonSortify.ts
src/**/xmlToJson.ts
49 changes: 39 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
// eslint-disable-next-line @typescript-eslint/ban-types
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
jest: true,
},
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
extends: [
'airbnb-base',
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
parserOptions: {
ecmaVersion: 12,
},
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'prefer-const': [
'error',
{
destructuring: 'any',
ignoreReadBeforeAssign: false,
},
],
'@typescript-eslint/ban-types': [
'error',
{
types: {
String: false,
Boolean: false,
Number: false,
Symbol: false,
'{}': false,
Object: false,
object: false,
Function: false,
},
extendDefaults: true,
},
],
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'no-console': 'off',
'ban-types': 'off',
'no-case-declarations': 'off',
},
};
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"start-example": "export LUMIGO_TOKEN=XXXXX && export LUMIGO_SERVICE_NAME=service-name && node -r lumigo-node-wrapper example/expressApp.js",
"build": "rm -rf lib && tsc --build --force",
"prepublishOnly": "npm run prettier:fix && npm run lint && npm run test",
"lint": "eslint \"src/**/*.{ts,tsx,js}\"",
"prettier:ci": "prettier --list-different \"src/**/*.[jt]s\" --parser typescript",
"prettier:fix": "prettier --write \"./src/**/*.[jt]s\" --parser typescript",
Expand Down Expand Up @@ -38,31 +39,33 @@
},
"homepage": "https://github.com/lumigo-io/lumigo-node-wrapper.git",
"devDependencies": {
"@opentelemetry/plugin-express": "^0.15.0",
"@opentelemetry/plugin-http": "^0.18.2",
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.4",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
"@babel/preset-env": "^7.4.4",
"@babel/preset-typescript": "^7.16.7",
"@opentelemetry/exporter-zipkin": "^1.0.1",
"@opentelemetry/plugin-express": "^0.15.0",
"@opentelemetry/plugin-http": "^0.18.2",
"@semantic-release/changelog": "^3.0.6",
"@semantic-release/git": "^7.0.18",
"@semantic-release/github": "^7.2.3",
"semantic-release": "^19.0.2",
"@babel/preset-typescript": "^7.16.7",
"@opentelemetry/exporter-zipkin": "^1.0.1",
"@types/express": "4.17.8",
"@types/jest": "^27.4.0",
"aws-sdk": "^2.1066.0",
"axios": "^0.21.1",
"body-parser": "^1.19.1",
"cross-env": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"express": "4.17.0",
"eslint": "^7.25.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"semantic-release": "^19.0.2",
"ts-node": "^9.1.1",
"typescript": "^4.3.4",
"webpack": "^4.35.0",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InstrumentationIfc } from './hooksIfc';
import { diag, Span } from '@opentelemetry/api';
import { safeExecute } from '../utils';

type ExpressRequestType = { req: PatchedRequest, res: express.Response };
type ExpressRequestType = { req: PatchedRequest; res: express.Response };

export const ExpressHooks: InstrumentationIfc<ExpressRequestType, any> = {
requestHook(span: Span, { req, res }: ExpressRequestType): void {
Expand Down
98 changes: 58 additions & 40 deletions src/hooks/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as shimmer from 'shimmer';
import {diag, Span} from '@opentelemetry/api';
import { diag, Span } from '@opentelemetry/api';
import { ClientRequest, IncomingMessage, ServerResponse } from 'http';
import { InstrumentationIfc } from './hooksIfc';
import { isAwsService, runOneTimeWrapper, safeExecute } from '../utils';
Expand All @@ -10,8 +10,8 @@ const noop = () => {};
const isFunctionAlreadyWrapped = (fn) => fn && fn.__wrapped;

export type HookOptions = {
beforeHook?: Function,
afterHook?: Function,
beforeHook?: Function;
afterHook?: Function;
};

const hook = (module, funcName, options: HookOptions = {}, shimmerLib = shimmer) => {
Expand All @@ -37,7 +37,11 @@ const hook = (module, funcName, options: HookOptions = {}, shimmerLib = shimmer)

const MAX_SIZE = 4084;

const createEmitResponseOnEmitBeforeHookHandler = (requestData: RequestData,response: any, span: Span & {attributes: Record<string, string>}) => {
const createEmitResponseOnEmitBeforeHookHandler = (
requestData: RequestData,
response: any,
span: Span & { attributes: Record<string, string> }
) => {
let body = '';
let maxPayloadSize = MAX_SIZE;
return function (args) {
Expand Down Expand Up @@ -82,20 +86,29 @@ export const isEncodingType = (encodingType): boolean =>
);

export const extractBodyFromEmitSocketEvent = (socketEventArgs) => {
return safeExecute(() => {
if (socketEventArgs && socketEventArgs._httpMessage && socketEventArgs._httpMessage._hasBody) {
const httpMessage = socketEventArgs._httpMessage;
let lines = [];
if (httpMessage.hasOwnProperty('outputData')) {
lines = httpMessage.outputData[0]?.data.split('\n') || [];
} else if (httpMessage.hasOwnProperty('output')) {
lines = httpMessage.output[0]?.split('\n') || [];
}
if (lines.length > 0) {
return lines[lines.length - 1];
return safeExecute(
() => {
if (
socketEventArgs &&
socketEventArgs._httpMessage &&
socketEventArgs._httpMessage._hasBody
) {
const httpMessage = socketEventArgs._httpMessage;
let lines = [];
if (httpMessage.hasOwnProperty('outputData')) {
lines = httpMessage.outputData[0]?.data.split('\n') || [];
} else if (httpMessage.hasOwnProperty('output')) {
lines = httpMessage.output[0]?.split('\n') || [];
}
if (lines.length > 0) {
return lines[lines.length - 1];
}
}
}
},"failed to extractBodyFromEmitSocketEvent", "warn", "")();
},
'failed to extractBodyFromEmitSocketEvent',
'warn',
''
)();
};

export const isEmptyString = (str): boolean =>
Expand All @@ -112,7 +125,10 @@ export const extractBodyFromWriteOrEndFunc = (writeEventArgs) => {
})();
};

const createEmitResponseHandler = (requestData: RequestData, span: Span & {attributes: Record<string, string>}) => {
const createEmitResponseHandler = (
requestData: RequestData,
span: Span & { attributes: Record<string, string> }
) => {
return (response) => {
const onHandler = createEmitResponseOnEmitBeforeHookHandler(requestData, response, span);
hook(response, 'emit', {
Expand All @@ -131,7 +147,10 @@ const httpRequestWriteBeforeHookWrapper = (requestData: RequestData, span: Span)
};
};

const httpRequestEmitBeforeHookWrapper = (requestData, span: Span & {attributes: Record<string, string>}) => {
const httpRequestEmitBeforeHookWrapper = (
requestData,
span: Span & { attributes: Record<string, string> }
) => {
const emitResponseHandler = createEmitResponseHandler(requestData, span);
const oneTimerEmitResponseHandler = runOneTimeWrapper(emitResponseHandler, {});
return function (args) {
Expand All @@ -149,24 +168,24 @@ const httpRequestEmitBeforeHookWrapper = (requestData, span: Span & {attributes:
};

export type HttpRequest = {
host?: string,
body?: string,
path?: string
headers?: Record<string, string>,
}
host?: string;
body?: string;
path?: string;
headers?: Record<string, string>;
};

export type HttpResponse = {
statusCode?: number
body?: string,
headers?: Record<string, string>,
}
statusCode?: number;
body?: string;
headers?: Record<string, string>;
};

export type RequestData = {
request: HttpRequest,
response: HttpResponse,
request: HttpRequest;
response: HttpResponse;
};

type RequestType = (ClientRequest | IncomingMessage) & { headers?: any, getHeaders: () => any };
type RequestType = (ClientRequest | IncomingMessage) & { headers?: any; getHeaders: () => any };

function getRequestHeaders(request: RequestType) {
return request.headers || request.getHeaders();
Expand All @@ -176,17 +195,17 @@ export const HttpHooks: InstrumentationIfc<
ClientRequest | IncomingMessage,
IncomingMessage | ServerResponse
> = {
requestHook(span: Span & {attributes: Record<string, string>}, request: RequestType) {
diag.debug("@opentelemetry/instrumentation-http on requestHook()")
safeExecute(()=>{
requestHook(span: Span & { attributes: Record<string, string> }, request: RequestType) {
diag.debug('@opentelemetry/instrumentation-http on requestHook()');
safeExecute(() => {
const requestData: RequestData = {
request: {
body: '',
headers: {}
headers: {},
},
response: {
body: '',
headers: {}
headers: {},
},
};
let headers = getRequestHeaders(request);
Expand All @@ -213,13 +232,12 @@ export const HttpHooks: InstrumentationIfc<
hook(request, 'end', { beforeHook: endWrapper });
hook(request, 'emit', { beforeHook: emitWrapper });
hook(request, 'write', { beforeHook: writeWrapper });
})()
})();
},
responseHook(span: Span, response: IncomingMessage | (ServerResponse & { headers?: any })) {
diag.debug("@opentelemetry/instrumentation-http on responseHook()")
if (response.headers){
diag.debug('@opentelemetry/instrumentation-http on responseHook()');
if (response.headers) {
span.setAttribute('http.response.headers', JSON.stringify(response.headers));
}

},
};
4 changes: 2 additions & 2 deletions src/instrumentros/LumigoExpressInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ExpressHooks } from '../hooks/express';
import {ExpressInstrumentation} from "opentelemetry-instrumentation-express/dist/src";
import { ExpressInstrumentation } from 'opentelemetry-instrumentation-express/dist/src';

export default class LumigoExpressInstrumentation {
constructor(lumigoToken?: string) {
constructor() {
new ExpressInstrumentation({
requestHook: ExpressHooks.requestHook,
includeHttpAttributes: true,
Expand Down
4 changes: 2 additions & 2 deletions src/instrumentros/LumigoHttpInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpHooks } from '../hooks/http';

const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
import axios, { AxiosResponse } from 'axios';
import {LUMIGO_ENDPOINT} from "../wrapper";
import { LUMIGO_ENDPOINT } from '../wrapper';

let metadata;

Expand All @@ -29,7 +29,7 @@ export default class LumigoHttpInstrumentation {
ignoreOutgoingUrls: process.env['ECS_CONTAINER_METADATA_URI']
? [process.env['ECS_CONTAINER_METADATA_URI'], endPoint, LUMIGO_ENDPOINT]
: [endPoint, LUMIGO_ENDPOINT],
applyCustomAttributesOnSpan: (span, request, response) => {
applyCustomAttributesOnSpan: (span) => {
if (metadata) span.setAttribute('metadata', JSON.stringify(metadata));
if (lumigoToken) span.setAttribute('lumigoToken', lumigoToken);
},
Expand Down
Loading

0 comments on commit 486af9f

Please sign in to comment.