From 8a383c0ed7a8d0f45e8f1a3f538043f8e8d50bcb Mon Sep 17 00:00:00 2001 From: Martin Schumacher Date: Tue, 11 Jun 2024 09:39:03 +0000 Subject: [PATCH 1/2] feat(nodejs): configureAwsInstrumentation for downstream --- nodejs/packages/layer/src/wrapper.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/nodejs/packages/layer/src/wrapper.ts b/nodejs/packages/layer/src/wrapper.ts index 5325c8e4c9..26f9b5d71d 100644 --- a/nodejs/packages/layer/src/wrapper.ts +++ b/nodejs/packages/layer/src/wrapper.ts @@ -12,7 +12,7 @@ import { envDetector, processDetector, } from '@opentelemetry/resources'; -import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk'; +import { AwsInstrumentation, AwsSdkInstrumentationConfig } from '@opentelemetry/instrumentation-aws-sdk'; import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda'; import { diag, @@ -58,6 +58,7 @@ function defaultConfigureInstrumentations() { declare global { // in case of downstream configuring span processors etc + function configureAwsInstrumentation(defaultConfig: AwsSdkInstrumentationConfig): AwsSdkInstrumentationConfig function configureTracerProvider(tracerProvider: NodeTracerProvider): void function configureTracer(defaultConfig: NodeTracerConfig): NodeTracerConfig; function configureSdkRegistration( @@ -72,9 +73,11 @@ declare global { console.log('Registering OpenTelemetry'); const instrumentations = [ - new AwsInstrumentation({ - suppressInternalInstrumentation: true, - }), + new AwsInstrumentation( + typeof configureAwsInstrumentation === 'function' + ? configureAwsInstrumentation({suppressInternalInstrumentation: true}) + : {suppressInternalInstrumentation: true,}, + ), new AwsLambdaInstrumentation(typeof configureLambdaInstrumentation === 'function' ? configureLambdaInstrumentation({}) : {}), ...(typeof configureInstrumentations === 'function' ? configureInstrumentations: defaultConfigureInstrumentations)() ]; From 6ada9e2778b1c070d5b4a07196e9ef77ad4a2a1a Mon Sep 17 00:00:00 2001 From: Martin Schumacher Date: Wed, 12 Jun 2024 09:37:04 +0000 Subject: [PATCH 2/2] test: Unittest --- nodejs/packages/layer/.mocharc.json | 4 ++++ nodejs/packages/layer/package.json | 10 +++++++++- nodejs/packages/layer/test/wrapper.spec.ts | 23 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 nodejs/packages/layer/.mocharc.json create mode 100644 nodejs/packages/layer/test/wrapper.spec.ts diff --git a/nodejs/packages/layer/.mocharc.json b/nodejs/packages/layer/.mocharc.json new file mode 100644 index 0000000000..4ddf3176c9 --- /dev/null +++ b/nodejs/packages/layer/.mocharc.json @@ -0,0 +1,4 @@ +{ + "require": "ts-node/register", + "spec": ["test/**/*.spec.ts"] +} diff --git a/nodejs/packages/layer/package.json b/nodejs/packages/layer/package.json index 3abbf3bbad..28aa29eb5c 100644 --- a/nodejs/packages/layer/package.json +++ b/nodejs/packages/layer/package.json @@ -10,7 +10,8 @@ "lint:fix": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts --fix", "prepare": "npm run compile", "compile": "tsc -p .", - "postcompile": "copyfiles 'node_modules/**' build/workspace/nodejs && copyfiles -f 'scripts/*' build/workspace && copyfiles -f 'build/src/*' build/workspace && cd build/workspace && bestzip ../layer.zip *" + "postcompile": "copyfiles 'node_modules/**' build/workspace/nodejs && copyfiles -f 'scripts/*' build/workspace && copyfiles -f 'build/src/*' build/workspace && cd build/workspace && bestzip ../layer.zip *", + "test": "mocha" }, "keywords": [ "opentelemetry", @@ -51,5 +52,12 @@ "@opentelemetry/sdk-metrics": "^1.18.1", "@opentelemetry/sdk-trace-base": "^1.18.1", "@opentelemetry/sdk-trace-node": "^1.18.1" + }, + "devDependencies": { + "@types/mocha": "^10.0.6", + "@types/sinon": "^17.0.3", + "mocha": "^10.4.0", + "sinon": "^18.0.0", + "ts-node": "^10.9.2" } } diff --git a/nodejs/packages/layer/test/wrapper.spec.ts b/nodejs/packages/layer/test/wrapper.spec.ts new file mode 100644 index 0000000000..0bb3e3c702 --- /dev/null +++ b/nodejs/packages/layer/test/wrapper.spec.ts @@ -0,0 +1,23 @@ +import type { AwsSdkInstrumentationConfig } from "@opentelemetry/instrumentation-aws-sdk"; +import { stub } from "sinon"; + +declare global { + function configureAwsInstrumentation( + defaultConfig: AwsSdkInstrumentationConfig, + ): AwsSdkInstrumentationConfig; +} + +const assert = require("assert"); + +describe("wrapper", () => { + describe("configureAwsInstrumentation", () => { + it("is used if defined", () => { + const configureAwsInstrumentationStub = stub().returns({ + suppressInternalInstrumentation: true, + }); + global.configureAwsInstrumentation = configureAwsInstrumentationStub; + require("../src/wrapper"); + assert(configureAwsInstrumentationStub.calledOnce); + }).timeout(10000); + }); +});