From b088c47812146d4b9730610d6d1596bd4456aa74 Mon Sep 17 00:00:00 2001 From: CircleCI Date: Thu, 28 Nov 2024 20:21:16 +0200 Subject: [PATCH 1/2] inline winston instrumentation --- package-lock.json | 344 +++++++++++++++-- package.json | 7 +- src/instrumentations/logsInstrumentation.ts | 67 ---- .../winston/WinstonInstrumentation.ts | 2 +- .../src/index.ts | 18 + .../src/instrumentation.ts | 358 ++++++++++++++++++ .../src/internal-types.ts | 31 ++ .../src/types.ts | 50 +++ 8 files changed, 782 insertions(+), 95 deletions(-) delete mode 100644 src/instrumentations/logsInstrumentation.ts create mode 100644 src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts create mode 100644 src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts create mode 100644 src/instrumentations/winston/opentelemetry-instrumentation-winston/src/internal-types.ts create mode 100644 src/instrumentations/winston/opentelemetry-instrumentation-winston/src/types.ts diff --git a/package-lock.json b/package-lock.json index ba39232b..2c67b6d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1292,6 +1292,17 @@ "dev": true, "optional": true }, + "@dabh/diagnostics": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@dabh/diagnostics/-/diagnostics-2.0.3.tgz", + "integrity": "sha512-hrlQOIi7hAfzsMqlGSFyVucrx38O+j6wiGOf//H2ecvIEqYN4ADBSS2iLMh5UFyDunCNniUIPk/q3riFv45xRA==", + "dev": true, + "requires": { + "colorspace": "1.1.x", + "enabled": "2.0.x", + "kuler": "^2.0.0" + } + }, "@discoveryjs/json-ext": { "version": "0.5.7", "resolved": "https://registry.npmjs.org/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz", @@ -2345,24 +2356,35 @@ } }, "@opentelemetry/instrumentation": { - "version": "0.51.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation/-/instrumentation-0.51.0.tgz", - "integrity": "sha512-Eg/+Od5bEvzpvZQGhvMyKIkrzB9S7jW+6z9LHEI2VXhl/GrqQ3oBqlzJt4tA6pGtxRmqQWKWGM1wAbwDdW/gUA==", + "version": "0.55.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/instrumentation/-/instrumentation-0.55.0.tgz", + "integrity": "sha512-YDCMlaQRZkziLL3t6TONRgmmGxDx6MyQDXRD0dknkkgUZtOK5+8MWft1OXzmNu6XfBOdT12MKN5rz+jHUkafKQ==", "requires": { - "@opentelemetry/api-logs": "0.51.0", - "@types/shimmer": "^1.0.2", - "import-in-the-middle": "1.7.1", + "@opentelemetry/api-logs": "0.55.0", + "@types/shimmer": "^1.2.0", + "import-in-the-middle": "^1.8.1", "require-in-the-middle": "^7.1.1", "semver": "^7.5.2", "shimmer": "^1.2.1" }, "dependencies": { "@opentelemetry/api-logs": { - "version": "0.51.0", - "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.51.0.tgz", - "integrity": "sha512-m/jtfBPEIXS1asltl8fPQtO3Sb1qMpuL61unQajUmM8zIxeMF1AlqzWXM3QedcYgTTFiJCew5uJjyhpmqhc0+g==", + "version": "0.55.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.55.0.tgz", + "integrity": "sha512-3cpa+qI45VHYcA5c0bHM6VHo9gicv3p5mlLHNG3rLyjQU8b7e0st1rWtrUn3JbZ3DwwCfhKop4eQ9UuYlC6Pkg==", "requires": { - "@opentelemetry/api": "^1.0.0" + "@opentelemetry/api": "^1.3.0" + } + }, + "import-in-the-middle": { + "version": "1.11.2", + "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.11.2.tgz", + "integrity": "sha512-gK6Rr6EykBcc6cVWRSBR5TWf8nn6hZMYSRYqCcHa0l0d1fPK7JSYo6+Mlmck76jIX9aL/IZ71c06U2VpFwl1zA==", + "requires": { + "acorn": "^8.8.2", + "acorn-import-attributes": "^1.9.5", + "cjs-module-lexer": "^1.2.2", + "module-details-from-path": "^1.0.3" } } } @@ -2592,6 +2614,14 @@ "shimmer": "^1.2.1" } }, + "@types/ioredis4": { + "version": "npm:@types/ioredis@4.28.10", + "resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.28.10.tgz", + "integrity": "sha512-69LyhUgrXdgcNDv7ogs1qXZomnfOEnSmrmMFqKgt1XMJxmoOSG/u3wYy13yACIfKuMJ8IhKgHafDO3sx19zVQQ==", + "requires": { + "@types/node": "*" + } + }, "import-in-the-middle": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/import-in-the-middle/-/import-in-the-middle-1.4.2.tgz", @@ -3103,6 +3133,25 @@ "@opentelemetry/core": "^1.1.0" } }, + "@opentelemetry/winston-transport": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/winston-transport/-/winston-transport-0.8.0.tgz", + "integrity": "sha512-NIUToAei4nSoZdwwSw2oK75gmdIc5iwSPOWS5S9n1vTjK+v+leja0h+55vQDnJC0QeZtkckmpoCxgBG3wJLJ8w==", + "requires": { + "@opentelemetry/api-logs": "^0.55.0", + "winston-transport": "4.*" + }, + "dependencies": { + "@opentelemetry/api-logs": { + "version": "0.55.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/api-logs/-/api-logs-0.55.0.tgz", + "integrity": "sha512-3cpa+qI45VHYcA5c0bHM6VHo9gicv3p5mlLHNG3rLyjQU8b7e0st1rWtrUn3JbZ3DwwCfhKop4eQ9UuYlC6Pkg==", + "requires": { + "@opentelemetry/api": "^1.3.0" + } + } + } + }, "@pnpm/config.env-replace": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@pnpm/config.env-replace/-/config.env-replace-1.1.0.tgz", @@ -5373,14 +5422,6 @@ "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", "dev": true }, - "@types/ioredis4": { - "version": "npm:@types/ioredis@4.28.10", - "resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.28.10.tgz", - "integrity": "sha512-69LyhUgrXdgcNDv7ogs1qXZomnfOEnSmrmMFqKgt1XMJxmoOSG/u3wYy13yACIfKuMJ8IhKgHafDO3sx19zVQQ==", - "requires": { - "@types/node": "*" - } - }, "@types/istanbul-lib-coverage": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz", @@ -5586,6 +5627,20 @@ "integrity": "sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==", "dev": true }, + "@types/triple-beam": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/triple-beam/-/triple-beam-1.3.5.tgz", + "integrity": "sha512-6WaYesThRMCl19iryMYP7/x2OVgCtbIVflDGFpWnb9irXI3UjYE4AzmYuiUKY1AJstGijoY+MgUszMgRxIYTYw==" + }, + "@types/winston": { + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/@types/winston/-/winston-2.4.4.tgz", + "integrity": "sha512-BVGCztsypW8EYwJ+Hq+QNYiT/MUyCif0ouBH+flrY66O5W+KIXAMML6E/0fJpm7VjIzgangahl5S03bJJQGrZw==", + "dev": true, + "requires": { + "winston": "*" + } + }, "@types/yargs": { "version": "17.0.33", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", @@ -6859,6 +6914,16 @@ "integrity": "sha512-lHl4d5/ONEbLlJvaJNtsF/Lz+WvB07u2ycqTYbdrq7UypDXailES4valYb2eWiJFxZlVmpGekfqoxQhzyFdT4Q==", "dev": true }, + "color": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/color/-/color-3.2.1.tgz", + "integrity": "sha512-aBl7dZI9ENN6fUGC7mWpMTPNHmWUSNan9tuWN6ahh5ZLNk9baLJOnSMlrQkHcrfFgz2/RigjUVAjdx36VcemKA==", + "dev": true, + "requires": { + "color-convert": "^1.9.3", + "color-string": "^1.6.0" + } + }, "color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -6874,12 +6939,38 @@ "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", "dev": true }, + "color-string": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.9.1.tgz", + "integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==", + "dev": true, + "requires": { + "color-name": "^1.0.0", + "simple-swizzle": "^0.2.2" + } + }, "colorette": { "version": "2.0.20", "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", "dev": true }, + "colors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", + "integrity": "sha512-pFGrxThWcWQ2MsAz6RtgeWe4NK2kUE1WfsrvvlctdII745EW9I0yflqhe7++M5LEc7bV2c/9/5zc8sFcpL0Drw==", + "dev": true + }, + "colorspace": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/colorspace/-/colorspace-1.1.4.tgz", + "integrity": "sha512-BgvKJiuVu1igBUF2kEjRCZXol6wiiGbY5ipL/oVPwm0BL9sIpMIzM8IK7vwuxIIzOXMV3Ey5w+vxhm0rR/TN8w==", + "dev": true, + "requires": { + "color": "^3.1.3", + "text-hex": "1.0.x" + } + }, "commander": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", @@ -7327,6 +7418,12 @@ "integrity": "sha512-v1plID3y9r/lPhviJ1wrXpLeyUIGAZ2SHNYTEapm7/8A9nLPoyvVp3RK/EPFqn5kEznyWgYZNsRtYYIWbuG8KA==", "dev": true }, + "cycle": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", + "integrity": "sha512-TVF6svNzeQCOpjCqsy0/CSy8VgObG3wXusJ73xW2GbG5rGx7lC8zxDSURicsXI2UsGdi2L0QNRCi745/wUDvsA==", + "dev": true + }, "data-view-buffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz", @@ -7650,6 +7747,12 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "enabled": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/enabled/-/enabled-2.0.0.tgz", + "integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==", + "dev": true + }, "encodeurl": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", @@ -8290,6 +8393,12 @@ } } }, + "eyes": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz", + "integrity": "sha512-GipyPsXO1anza0AOZdy69Im7hGFCNB7Y/NGjDlZGJ3GJJLtwNSb2vrzYrTYJRrRloVx7pl+bhUaTB8yiccPvFQ==", + "dev": true + }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -8351,6 +8460,11 @@ "bser": "2.1.1" } }, + "fecha": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/fecha/-/fecha-4.2.3.tgz", + "integrity": "sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==" + }, "figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -8457,6 +8571,12 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "fn.name": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", + "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==", + "dev": true + }, "follow-redirects": { "version": "1.15.9", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", @@ -8994,8 +9114,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ini": { "version": "1.3.8", @@ -9319,6 +9438,12 @@ "integrity": "sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==", "dev": true }, + "isstream": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", + "integrity": "sha512-Yljz7ffyPbrLpLngrMtZ7NduUgVvi6wG9RJ9IUcyCd59YQ911PBJphODUcbOVbqYfxe1wuYf/LJ8PauMRwsM/g==", + "dev": true + }, "issue-parser": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/issue-parser/-/issue-parser-6.0.0.tgz", @@ -10753,6 +10878,12 @@ "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", "dev": true }, + "kuler": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz", + "integrity": "sha512-Xq9nH7KlWZmXAtodXDDRE7vs6DU1gTU8zYDHDiWLSip45Egwq3plLHzPn27NgvzL2r1LMPC1vdqh98sQxtqj4A==", + "dev": true + }, "lazystream": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/lazystream/-/lazystream-1.0.1.tgz", @@ -10933,6 +11064,26 @@ "log-prefix": "0.1.1" } }, + "logform": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/logform/-/logform-2.7.0.tgz", + "integrity": "sha512-TFYA4jnP7PVbmlBIfhlSe+WKxs9dklXMTEGcBCIvLhE/Tn3H6Gk1norupVW7m5Cnd4bLcr08AytbyV/xj7f/kQ==", + "requires": { + "@colors/colors": "1.6.0", + "@types/triple-beam": "^1.3.2", + "fecha": "^4.2.0", + "ms": "^2.1.1", + "safe-stable-stringify": "^2.3.1", + "triple-beam": "^1.3.0" + }, + "dependencies": { + "@colors/colors": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.6.0.tgz", + "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==" + } + } + }, "long": { "version": "5.2.3", "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", @@ -15187,6 +15338,15 @@ "wrappy": "1" } }, + "one-time": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/one-time/-/one-time-1.0.0.tgz", + "integrity": "sha512-5DXOiRKwuSEcQ/l0kGCF6Q3jcADFv5tSmRaJck/OqkVFcOzutB134KRSfF0xDrL39MNnqxbHBbUUcjZIhTgb2g==", + "dev": true, + "requires": { + "fn.name": "1.x.x" + } + }, "onetime": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", @@ -15575,6 +15735,12 @@ } } }, + "pkginfo": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/pkginfo/-/pkginfo-0.3.1.tgz", + "integrity": "sha512-yO5feByMzAp96LtP58wvPKSbaKAi/1C4kV9XpTctr6EepnP6F33RBNOiVrdz9BrPA98U2BMFsTNHo44TWcbQ2A==", + "dev": true + }, "possible-typed-array-names": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz", @@ -16228,6 +16394,11 @@ "is-regex": "^1.1.4" } }, + "safe-stable-stringify": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.5.0.tgz", + "integrity": "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==" + }, "safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", @@ -16529,6 +16700,23 @@ } } }, + "simple-swizzle": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", + "integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==", + "dev": true, + "requires": { + "is-arrayish": "^0.3.1" + }, + "dependencies": { + "is-arrayish": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", + "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==", + "dev": true + } + } + }, "sisteransi": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", @@ -16672,6 +16860,12 @@ "nan": "^2.20.0" } }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==", + "dev": true + }, "stack-utils": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz", @@ -16764,7 +16958,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dev": true, "requires": { "safe-buffer": "~5.1.0" }, @@ -16772,8 +16965,7 @@ "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" } } }, @@ -17033,6 +17225,12 @@ "integrity": "sha512-wiBrwC1EhBelW12Zy26JeOUkQ5mRu+5o8rpsJk5+2t+Y5vE7e842qtZDQ2g1NpX/29HdyFeJ4nSIhI47ENSxlQ==", "dev": true }, + "text-hex": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/text-hex/-/text-hex-1.0.0.tgz", + "integrity": "sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg==", + "dev": true + }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", @@ -17118,6 +17316,11 @@ "integrity": "sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==", "dev": true }, + "triple-beam": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.4.1.tgz", + "integrity": "sha512-aZbgViZrg1QNcG+LULa7nhZpJTZSLm/mXnHXnbAbjmN5aSa0y7V+wvv6+4WaBtpISJzThKy+PIPxc1Nq1EJ9mg==" + }, "ts-node": { "version": "9.1.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.1.1.tgz", @@ -17450,8 +17653,7 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", - "dev": true + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, "utils-merge": { "version": "1.0.1", @@ -17676,6 +17878,98 @@ "integrity": "sha512-CC1bOL87PIWSBhDcTrdeLo6eGT7mCFtrg0uIJtqJUFyK+eJnzl8A1niH56uu7KMa5XFrtiV+AQuHO3n7DsHnLQ==", "dev": true }, + "winston": { + "version": "3.17.0", + "resolved": "https://registry.npmjs.org/winston/-/winston-3.17.0.tgz", + "integrity": "sha512-DLiFIXYC5fMPxaRg832S6F5mJYvePtmO5G9v9IgUFPhXm9/GkXarH/TUrBAVzhTCzAj9anE/+GjrgXp/54nOgw==", + "dev": true, + "requires": { + "@colors/colors": "^1.6.0", + "@dabh/diagnostics": "^2.0.2", + "async": "^3.2.3", + "is-stream": "^2.0.0", + "logform": "^2.7.0", + "one-time": "^1.0.0", + "readable-stream": "^3.4.0", + "safe-stable-stringify": "^2.3.1", + "stack-trace": "0.0.x", + "triple-beam": "^1.3.0", + "winston-transport": "^4.9.0" + }, + "dependencies": { + "@colors/colors": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.6.0.tgz", + "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==", + "dev": true + }, + "readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dev": true, + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + } + } + }, + "winston-transport": { + "version": "4.9.0", + "resolved": "https://registry.npmjs.org/winston-transport/-/winston-transport-4.9.0.tgz", + "integrity": "sha512-8drMJ4rkgaPo1Me4zD/3WLfI/zPdA9o2IipKODunnGDcuqbHwjsbB79ylv04LCGGzU0xQ6vTznOMpQGaLhhm6A==", + "requires": { + "logform": "^2.7.0", + "readable-stream": "^3.6.2", + "triple-beam": "^1.3.0" + }, + "dependencies": { + "readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + } + } + }, + "winston2": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/winston2/-/winston2-1.0.3.tgz", + "integrity": "sha512-Nowj/XHYjUXIk6cm5u83S0ENkv+pf2TyDPk42pntH6Vx5ywKg1hUL2KBaybwYeIVvy/G3bwMLHSNOj3VmrEqEw==", + "dev": true, + "requires": { + "winston": "^1.0.1" + }, + "dependencies": { + "async": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz", + "integrity": "sha512-5mO7DX4CbJzp9zjaFXusQQ4tzKJARjNB1Ih1pVBi8wkbmXy/xzIDgEMXxWePLzt2OdFwaxfneIlT1nCiXubrPQ==", + "dev": true + }, + "winston": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/winston/-/winston-1.1.2.tgz", + "integrity": "sha512-rl9hA8se2gjdYI6nP1f+kjjSCFCZrObIJB/eXOcMdzWxxcYp7exyc5Bs248fwLT+wHA/+aK0VtBlPHL8qO0T0w==", + "dev": true, + "requires": { + "async": "~1.0.0", + "colors": "1.0.x", + "cycle": "1.0.x", + "eyes": "0.1.x", + "isstream": "0.1.x", + "pkginfo": "0.3.x", + "stack-trace": "0.0.x" + } + } + } + }, "word-wrap": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", diff --git a/package.json b/package.json index 0a221954..c7637855 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "@opentelemetry/core": "1.17.1", "@opentelemetry/exporter-logs-otlp-http": "0.51.0", "@opentelemetry/exporter-trace-otlp-http": "0.51.0", - "@opentelemetry/instrumentation": "0.51.0", + "@opentelemetry/instrumentation": "0.55.0", "@opentelemetry/instrumentation-amqplib": "0.33.2", "@opentelemetry/instrumentation-aws-sdk": "^0.38.1", "@opentelemetry/instrumentation-bunyan": "0.37.0", @@ -57,6 +57,7 @@ "@opentelemetry/sdk-trace-base": "1.9.1", "@opentelemetry/sdk-trace-node": "1.9.1", "@opentelemetry/semantic-conventions": "1.17.1", + "@opentelemetry/winston-transport": "^0.8.0", "@prisma/instrumentation": "^4.14.0", "deasync": "^0.1.30", "opentelemetry-instrumentation-express": "0.39.1", @@ -82,6 +83,7 @@ "@types/jest": "^29.5.1", "@types/kafkajs": "^1.9.0", "@types/node": "^20.5.7", + "@types/winston": "^2.4.4", "@typescript-eslint/eslint-plugin": "^5.59.2", "@typescript-eslint/parser": "^5.59.2", "aws-config": "^1.4.0", @@ -123,7 +125,8 @@ "utf-8-validate": "^5.0.2", "wait-on": "^6.0.1", "webpack": "^5.88.2", - "webpack-cli": "^4.10.0" + "webpack-cli": "^4.10.0", + "winston2": "^1.0.3" }, "release": { "branches": [ diff --git a/src/instrumentations/logsInstrumentation.ts b/src/instrumentations/logsInstrumentation.ts deleted file mode 100644 index 0a89261a..00000000 --- a/src/instrumentations/logsInstrumentation.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { - InstrumentationBase, - InstrumentationModuleDefinition, - InstrumentationNodeModuleDefinition, -} from '@opentelemetry/instrumentation'; -import { SpanKind } from '@opentelemetry/api'; - -import { logger } from '../logging'; - -export default class LogsInstrumentation extends InstrumentationBase { - instrumentationDescription: string; - supportedVersions: string[]; - - constructor() { - super('logs-instrumentation', '0.0.1'); - } - - protected init(): InstrumentationModuleDefinition | InstrumentationModuleDefinition[] | void { - logger.debug('in console instrumentation'); - - return [ - new InstrumentationNodeModuleDefinition( - 'console', - ['*'], - (moduleExports, moduleVersion) => { - const instrumentation = this; - this._wrap(moduleExports, 'warn', this.logsWrapper(instrumentation, 'warn')); - this._wrap(moduleExports, 'error', this.logsWrapper(instrumentation, 'error')); - return moduleExports; - }, - (exports) => {}, - [] - ), - ]; - } - - private logsWrapper = (instrumentation, level) => { - return (original) => { - return ( - // eslint-disable-next-line node/no-unsupported-features/node-builtins - message, - options - ) => { - let result; - let stringifyOptions; - try { - stringifyOptions = JSON.stringify(options); - } catch (e) {} - instrumentation.tracer.startActiveSpan( - 'logs-span', - { - kind: SpanKind.INTERNAL, - attributes: { log: message, level, options: stringifyOptions }, - }, - (span) => { - span.end(); - return span; - } - ); - result = options - ? original.apply(this, [message, options]) - : original.apply(this, [message]); - return result; - }; - }; - }; -} diff --git a/src/instrumentations/winston/WinstonInstrumentation.ts b/src/instrumentations/winston/WinstonInstrumentation.ts index 606269ce..8c8cc085 100644 --- a/src/instrumentations/winston/WinstonInstrumentation.ts +++ b/src/instrumentations/winston/WinstonInstrumentation.ts @@ -1,4 +1,4 @@ -import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston'; +import { WinstonInstrumentation } from './opentelemetry-instrumentation-winston/src'; import { LoggingInstrumentor } from '../instrumentor'; export default class LumigoWinstonInstrumentation extends LoggingInstrumentor { diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts new file mode 100644 index 00000000..439a3da6 --- /dev/null +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './instrumentation'; +export * from './types'; \ No newline at end of file diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts new file mode 100644 index 00000000..6e114688 --- /dev/null +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts @@ -0,0 +1,358 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { context, trace, isSpanContextValid, Span } from '@opentelemetry/api'; +import { SeverityNumber } from '@opentelemetry/api-logs'; +import { + InstrumentationBase, + InstrumentationNodeModuleDefinition, + InstrumentationNodeModuleFile, + isWrapped, + safeExecuteInTheMiddle, +} from '@opentelemetry/instrumentation'; +import type { WinstonInstrumentationConfig } from './types' +import type { + Winston2LoggerModule, + Winston2LogMethod, + Winston3ConfigureMethod, + Winston3LogMethod, + Winston3Logger, +} from './internal-types' +import { OpenTelemetryTransportV3 } from '@opentelemetry/winston-transport'; + +const winston3Versions = ['>=3 <4']; +const winstonPre3Versions = ['>=1 <3']; + +export class WinstonInstrumentation extends InstrumentationBase { + constructor(config: WinstonInstrumentationConfig = {}) { + super('@lumigo/winston-custom-instrumentation', '0.0.0', config); + } + + protected init() { + const winstons3instrumentationNodeModuleDefinition = + new InstrumentationNodeModuleDefinition( + 'winston', + winston3Versions, + moduleExports => moduleExports, + () => {}, + [ + new InstrumentationNodeModuleFile( + 'winston/lib/winston/logger.js', + winston3Versions, + (logger: Winston3Logger) => { + const loggerCtor = (logger as unknown as Function) + if (isWrapped(loggerCtor.prototype['write'])) { + this._unwrap(loggerCtor.prototype, 'write'); + } + this._wrap(loggerCtor.prototype, 'write', this._getPatchedWrite()); + + // Wrap configure + if (isWrapped(loggerCtor.prototype['configure'])) { + this._unwrap(loggerCtor.prototype, 'configure'); + } + this._wrap( + loggerCtor.prototype, + 'configure', + this._getPatchedConfigure() + ); + + return logger; + }, + (logger: Winston3Logger) => { + const loggerCtor = (logger as unknown as Function) + if (logger === undefined) return; + this._unwrap(loggerCtor.prototype, 'write'); + this._unwrap(loggerCtor.prototype, 'configure'); + } + ), + ] + ); + + const winstons2instrumentationNodeModuleDefinition = + new InstrumentationNodeModuleDefinition( + 'winston', + winstonPre3Versions, + moduleExports => moduleExports, + () => {}, + [ + new InstrumentationNodeModuleFile( + 'winston/lib/winston/logger.js', + winstonPre3Versions, + (fileExports: Winston2LoggerModule) => { + const proto = fileExports.Logger.prototype; + + if (isWrapped(proto.log)) { + this._unwrap(proto, 'log'); + } + this._wrap(proto, 'log', this._getPatchedLog()); + + return fileExports; + }, + (fileExports: Winston2LoggerModule) => { + if (fileExports === undefined) return; + this._unwrap(fileExports.Logger.prototype, 'log'); + } + ), + ] + ); + return [ + winstons3instrumentationNodeModuleDefinition, + winstons2instrumentationNodeModuleDefinition, + ]; + } + + private _callHook(span: Span, record: Record) { + const { logHook } = this.getConfig(); + + if (!logHook) { + return; + } + + safeExecuteInTheMiddle( + () => logHook(span, record), + err => { + if (err) { + this._diag.error('error calling logHook', err); + } + }, + true + ); + } + + private _getPatchedWrite() { + return (original: Winston3LogMethod) => { + const instrumentation = this; + return function patchedWrite( + this: never, + ...args: Parameters + ) { + const record = args[0]; + instrumentation._handleLogCorrelation(record); + return original.apply(this, args); + }; + }; + } + + private _getPatchedLog() { + return (original: Winston2LogMethod) => { + const instrumentation = this; + return function patchedLog( + this: never, + ...args: Parameters + ) { + const record: Record = {}; + instrumentation._handleLogCorrelation(record); + // Inject in metadata argument + let isDataInjected = false; + for (let i = args.length - 1; i >= 0; i--) { + if (typeof args[i] === 'object') { + args[i] = Object.assign(args[i], record); + isDataInjected = true; + break; + } + } + if (!isDataInjected) { + const insertAt = + typeof args[args.length - 1] === 'function' + ? args.length - 1 + : args.length; + + args.splice(insertAt, 0, record); + } + + return original.apply(this, args); + }; + }; + } + + private _getPatchedConfigure() { + return (original: Winston3ConfigureMethod) => { + const instrumentation = this; + return function patchedConfigure( + this: never, + ...args: Parameters + ) { + const config = instrumentation.getConfig(); + if (!config.disableLogSending) { + if (args && args.length > 0) { + // Try to load Winston transport + try { + const originalTransports = args[0].transports; + let newTransports = Array.isArray(originalTransports) + ? originalTransports + : []; + let transportOptions = {}; + if (config.logSeverity) { + const winstonLevel = instrumentation._winstonLevelFromSeverity( + config.logSeverity, + args[0].levels + ); + transportOptions = { level: winstonLevel }; + } + const openTelemetryTransport = new OpenTelemetryTransportV3( + transportOptions + ); + if (originalTransports && !Array.isArray(originalTransports)) { + newTransports = [originalTransports]; + } + newTransports.push(openTelemetryTransport); + args[0].transports = newTransports; + } catch (err) { + instrumentation._diag.warn( + '@opentelemetry/winston-transport is not available, log records will not be automatically sent.' + ); + } + } + } + return original.apply(this, args); + }; + }; + } + + private _handleLogCorrelation(record: Record) { + if (!this.getConfig().disableLogCorrelation) { + const span = trace.getSpan(context.active()); + if (span) { + const spanContext = span.spanContext(); + if (isSpanContextValid(spanContext)) { + const fields = { + trace_id: spanContext.traceId, + span_id: spanContext.spanId, + trace_flags: `0${spanContext.traceFlags.toString(16)}`, + }; + const enhancedRecord = Object.assign(record, fields); + this._callHook(span, enhancedRecord); + return enhancedRecord; + } + } + } + return record; + } + + private _winstonLevelFromSeverity( + severity: SeverityNumber, + winstonLevels: { [key: string]: number } | undefined + ): string | undefined { + if (winstonLevels) { + if (isNpmLevels(winstonLevels)) { + if (severity >= SeverityNumber.ERROR) { + return 'error'; + } else if (severity >= SeverityNumber.WARN) { + return 'warn'; + } else if (severity >= SeverityNumber.INFO) { + return 'info'; + } else if (severity >= SeverityNumber.DEBUG3) { + return 'http'; + } else if (severity >= SeverityNumber.DEBUG2) { + return 'verbose'; + } else if (severity >= SeverityNumber.DEBUG) { + return 'debug'; + } else if (severity >= SeverityNumber.TRACE) { + return 'silly'; + } + } else if (isCliLevels(winstonLevels)) { + if (severity >= SeverityNumber.ERROR) { + return 'error'; + } else if (severity >= SeverityNumber.WARN) { + return 'warn'; + } else if (severity >= SeverityNumber.INFO3) { + return 'help'; + } else if (severity >= SeverityNumber.INFO2) { + return 'data'; + } else if (severity >= SeverityNumber.INFO) { + return 'info'; + } else if (severity >= SeverityNumber.DEBUG) { + return 'debug'; + } else if (severity >= SeverityNumber.TRACE4) { + return 'prompt'; + } else if (severity >= SeverityNumber.TRACE3) { + return 'verbose'; + } else if (severity >= SeverityNumber.TRACE2) { + return 'input'; + } else if (severity >= SeverityNumber.TRACE) { + return 'silly'; + } + } else if (isSyslogLevels(winstonLevels)) { + if (severity >= SeverityNumber.FATAL2) { + return 'emerg'; + } else if (severity >= SeverityNumber.FATAL) { + return 'alert'; + } else if (severity >= SeverityNumber.ERROR2) { + return 'crit'; + } else if (severity >= SeverityNumber.ERROR) { + return 'error'; + } else if (severity >= SeverityNumber.WARN) { + return 'warning'; + } else if (severity >= SeverityNumber.INFO2) { + return 'notice'; + } else if (severity >= SeverityNumber.INFO) { + return 'info'; + } else if (severity >= SeverityNumber.TRACE) { + return 'debug'; + } + } + // Unknown level + this._diag.warn( + 'failed to configure severity with existing winston levels' + ); + } + + function isCliLevels(arg: any): boolean { + return ( + arg && + arg.error !== undefined && + arg.warn && + arg.help && + arg.data && + arg.info && + arg.debug && + arg.prompt && + arg.verbose && + arg.input && + arg.silly + ); + } + + function isNpmLevels(arg: any): boolean { + return ( + arg && + arg.error !== undefined && + arg.warn && + arg.info && + arg.http && + arg.verbose && + arg.debug && + arg.silly + ); + } + + function isSyslogLevels(arg: any): boolean { + return ( + arg && + arg.emerg !== undefined && + arg.alert && + arg.crit && + arg.error && + arg.warning && + arg.notice && + arg.info && + arg.debug + ); + } + + return; + } +} \ No newline at end of file diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/internal-types.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/internal-types.ts new file mode 100644 index 00000000..5a448873 --- /dev/null +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/internal-types.ts @@ -0,0 +1,31 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { Logger as Winston3Logger } from 'winston'; +import type { + LoggerInstance as Winston2Logger, + LogMethod as Winston2LogMethod, +} from 'winston2'; +export type Winston3LogMethod = Winston3Logger['write']; +export type Winston3ConfigureMethod = Winston3Logger['configure']; +export type { Winston3Logger }; +export type { Winston2LogMethod }; +export type Winston2LoggerModule = { + Logger: Winston2Logger & { + prototype: { log: Winston2LogMethod }; + }; +}; +export type { Winston2Logger }; \ No newline at end of file diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/types.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/types.ts new file mode 100644 index 00000000..08a39fd5 --- /dev/null +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/types.ts @@ -0,0 +1,50 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { Span } from '@opentelemetry/api'; +import type { SeverityNumber } from '@opentelemetry/api-logs'; +import type { InstrumentationConfig } from '@opentelemetry/instrumentation'; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type LogHookFunction = (span: Span, record: Record) => void; + +export interface WinstonInstrumentationConfig extends InstrumentationConfig { + /** + * Whether to disable the automatic sending of log records to the + * OpenTelemetry Logs SDK. + * @default false + */ + disableLogSending?: boolean; + + /** + * Control Log sending severity level, logs will be sent for specified severity and higher. + */ + logSeverity?: SeverityNumber; + + /** + * Whether to disable the injection trace-context fields, and possibly other + * fields from `logHook()`, into log records for log correlation. + * @default false + */ + disableLogCorrelation?: boolean; + + /** + * A function that allows injecting additional fields in log records. It is + * called, as `logHook(span, record)`, for each log record emitted in a valid + * span context. It requires `disableLogCorrelation` to be false. + */ + logHook?: LogHookFunction; +} \ No newline at end of file From 019eda590ee79b18e077ef67cf2bc6d375c1f450 Mon Sep 17 00:00:00 2001 From: CircleCI Date: Thu, 28 Nov 2024 20:26:07 +0200 Subject: [PATCH 2/2] lint fixes --- .../src/index.ts | 2 +- .../src/instrumentation.ts | 151 ++++++++---------- .../src/internal-types.ts | 7 +- .../src/types.ts | 2 +- 4 files changed, 68 insertions(+), 94 deletions(-) diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts index 439a3da6..c26f998c 100644 --- a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/index.ts @@ -15,4 +15,4 @@ */ export * from './instrumentation'; -export * from './types'; \ No newline at end of file +export * from './types'; diff --git a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts index 6e114688..09571981 100644 --- a/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts +++ b/src/instrumentations/winston/opentelemetry-instrumentation-winston/src/instrumentation.ts @@ -23,14 +23,14 @@ import { isWrapped, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; -import type { WinstonInstrumentationConfig } from './types' +import type { WinstonInstrumentationConfig } from './types'; import type { Winston2LoggerModule, Winston2LogMethod, Winston3ConfigureMethod, Winston3LogMethod, Winston3Logger, -} from './internal-types' +} from './internal-types'; import { OpenTelemetryTransportV3 } from '@opentelemetry/winston-transport'; const winston3Versions = ['>=3 <4']; @@ -42,72 +42,66 @@ export class WinstonInstrumentation extends InstrumentationBase moduleExports, - () => {}, - [ - new InstrumentationNodeModuleFile( - 'winston/lib/winston/logger.js', - winston3Versions, - (logger: Winston3Logger) => { - const loggerCtor = (logger as unknown as Function) - if (isWrapped(loggerCtor.prototype['write'])) { - this._unwrap(loggerCtor.prototype, 'write'); - } - this._wrap(loggerCtor.prototype, 'write', this._getPatchedWrite()); - - // Wrap configure - if (isWrapped(loggerCtor.prototype['configure'])) { - this._unwrap(loggerCtor.prototype, 'configure'); - } - this._wrap( - loggerCtor.prototype, - 'configure', - this._getPatchedConfigure() - ); - - return logger; - }, - (logger: Winston3Logger) => { - const loggerCtor = (logger as unknown as Function) - if (logger === undefined) return; + const winstons3instrumentationNodeModuleDefinition = new InstrumentationNodeModuleDefinition( + 'winston', + winston3Versions, + (moduleExports) => moduleExports, + () => {}, + [ + new InstrumentationNodeModuleFile( + 'winston/lib/winston/logger.js', + winston3Versions, + (logger: Winston3Logger) => { + const loggerCtor = logger as unknown as Function; + if (isWrapped(loggerCtor.prototype['write'])) { this._unwrap(loggerCtor.prototype, 'write'); + } + this._wrap(loggerCtor.prototype, 'write', this._getPatchedWrite()); + + // Wrap configure + if (isWrapped(loggerCtor.prototype['configure'])) { this._unwrap(loggerCtor.prototype, 'configure'); } - ), - ] - ); + this._wrap(loggerCtor.prototype, 'configure', this._getPatchedConfigure()); - const winstons2instrumentationNodeModuleDefinition = - new InstrumentationNodeModuleDefinition( - 'winston', - winstonPre3Versions, - moduleExports => moduleExports, - () => {}, - [ - new InstrumentationNodeModuleFile( - 'winston/lib/winston/logger.js', - winstonPre3Versions, - (fileExports: Winston2LoggerModule) => { - const proto = fileExports.Logger.prototype; + return logger; + }, + (logger: Winston3Logger) => { + const loggerCtor = logger as unknown as Function; + if (logger === undefined) return; + this._unwrap(loggerCtor.prototype, 'write'); + this._unwrap(loggerCtor.prototype, 'configure'); + } + ), + ] + ); - if (isWrapped(proto.log)) { - this._unwrap(proto, 'log'); - } - this._wrap(proto, 'log', this._getPatchedLog()); + const winstons2instrumentationNodeModuleDefinition = new InstrumentationNodeModuleDefinition( + 'winston', + winstonPre3Versions, + (moduleExports) => moduleExports, + () => {}, + [ + new InstrumentationNodeModuleFile( + 'winston/lib/winston/logger.js', + winstonPre3Versions, + (fileExports: Winston2LoggerModule) => { + const proto = fileExports.Logger.prototype; - return fileExports; - }, - (fileExports: Winston2LoggerModule) => { - if (fileExports === undefined) return; - this._unwrap(fileExports.Logger.prototype, 'log'); + if (isWrapped(proto.log)) { + this._unwrap(proto, 'log'); } - ), - ] - ); + this._wrap(proto, 'log', this._getPatchedLog()); + + return fileExports; + }, + (fileExports: Winston2LoggerModule) => { + if (fileExports === undefined) return; + this._unwrap(fileExports.Logger.prototype, 'log'); + } + ), + ] + ); return [ winstons3instrumentationNodeModuleDefinition, winstons2instrumentationNodeModuleDefinition, @@ -123,7 +117,7 @@ export class WinstonInstrumentation extends InstrumentationBase logHook(span, record), - err => { + (err) => { if (err) { this._diag.error('error calling logHook', err); } @@ -135,10 +129,7 @@ export class WinstonInstrumentation extends InstrumentationBase { const instrumentation = this; - return function patchedWrite( - this: never, - ...args: Parameters - ) { + return function patchedWrite(this: never, ...args: Parameters) { const record = args[0]; instrumentation._handleLogCorrelation(record); return original.apply(this, args); @@ -149,10 +140,7 @@ export class WinstonInstrumentation extends InstrumentationBase { const instrumentation = this; - return function patchedLog( - this: never, - ...args: Parameters - ) { + return function patchedLog(this: never, ...args: Parameters) { const record: Record = {}; instrumentation._handleLogCorrelation(record); // Inject in metadata argument @@ -166,9 +154,7 @@ export class WinstonInstrumentation extends InstrumentationBase { const instrumentation = this; - return function patchedConfigure( - this: never, - ...args: Parameters - ) { + return function patchedConfigure(this: never, ...args: Parameters) { const config = instrumentation.getConfig(); if (!config.disableLogSending) { if (args && args.length > 0) { // Try to load Winston transport try { const originalTransports = args[0].transports; - let newTransports = Array.isArray(originalTransports) - ? originalTransports - : []; + let newTransports = Array.isArray(originalTransports) ? originalTransports : []; let transportOptions = {}; if (config.logSeverity) { const winstonLevel = instrumentation._winstonLevelFromSeverity( @@ -202,9 +183,7 @@ export class WinstonInstrumentation extends InstrumentationBase