Skip to content

Commit 8f7e006

Browse files
committed
test: Tests
1 parent aa60fae commit 8f7e006

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// We access through node_modules to allow it to be patched.
18+
/* eslint-disable node/no-extraneous-require */
19+
20+
import * as path from 'path';
21+
22+
import {
23+
AwsLambdaInstrumentation,
24+
AwsLambdaInstrumentationConfig,
25+
} from '../../src';
26+
import {
27+
BatchSpanProcessor,
28+
InMemorySpanExporter,
29+
} from '@opentelemetry/sdk-trace-base';
30+
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
31+
import { Context } from 'aws-lambda';
32+
import * as assert from 'assert';
33+
34+
const memoryExporter = new InMemorySpanExporter();
35+
36+
describe('lambda handler', () => {
37+
let instrumentation: AwsLambdaInstrumentation;
38+
39+
let oldEnv: NodeJS.ProcessEnv;
40+
41+
const ctx = {
42+
functionName: 'my_function',
43+
invokedFunctionArn: 'my_arn',
44+
awsRequestId: 'aws_request_id',
45+
} as Context;
46+
47+
const initializeHandler = (
48+
handler: string,
49+
config: AwsLambdaInstrumentationConfig = {}
50+
) => {
51+
process.env._HANDLER = handler;
52+
53+
const provider = new NodeTracerProvider({
54+
spanProcessors: [new BatchSpanProcessor(memoryExporter)],
55+
});
56+
provider.register();
57+
58+
instrumentation = new AwsLambdaInstrumentation(config);
59+
instrumentation.setTracerProvider(provider);
60+
61+
return provider;
62+
};
63+
64+
const lambdaRequire = (module: string) =>
65+
require(path.resolve(__dirname, '..', module));
66+
67+
beforeEach(() => {
68+
oldEnv = { ...process.env };
69+
process.env.LAMBDA_TASK_ROOT = path.resolve(__dirname, '..');
70+
});
71+
72+
afterEach(() => {
73+
process.env = oldEnv;
74+
instrumentation.disable();
75+
76+
memoryExporter.reset();
77+
});
78+
79+
describe('handler with module.exports syntax', () => {
80+
it('should not fail', async () => {
81+
initializeHandler('lambda-test/instrumentationError.handler');
82+
83+
const result = await new Promise((resolve, reject) => {
84+
lambdaRequire('lambda-test/instrumentationError').handler(
85+
'arg',
86+
ctx,
87+
(err: Error, res: any) => {
88+
if (err) {
89+
reject(err);
90+
} else {
91+
resolve(res);
92+
}
93+
}
94+
);
95+
});
96+
assert.strictEqual(result, 'ok');
97+
});
98+
});
99+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const obj = {};
2+
Object.defineProperty(obj, 'handler', {
3+
get: () => handler,
4+
enumerable: true,
5+
configurable: false, // ❗️Nicht konfigurierbar = Problem für Shimmer
6+
});
7+
8+
module.exports = obj;
9+
10+
async function handler(event) {
11+
return 'ok';
12+
}

0 commit comments

Comments
 (0)