Skip to content

Commit

Permalink
feat(nodejs): configureAwsInstrumentation for downstream (#1375)
Browse files Browse the repository at this point in the history
* feat(nodejs): configureAwsInstrumentation for downstream

* test: Unittest
  • Loading branch information
mzl-md committed Jun 13, 2024
1 parent 03c6acb commit e6ef326
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions nodejs/packages/layer/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"require": "ts-node/register",
"spec": ["test/**/*.spec.ts"]
}
10 changes: 9 additions & 1 deletion nodejs/packages/layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
11 changes: 7 additions & 4 deletions nodejs/packages/layer/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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)()
];
Expand Down
23 changes: 23 additions & 0 deletions nodejs/packages/layer/test/wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit e6ef326

Please sign in to comment.