|
1 | 1 | import {AgentForTest} from "./AgentForTest";
|
2 | 2 | import {expect} from "chai";
|
3 | 3 | import {LambdaRequestHook, LambdaResponseHook} from "../../src/instrumentation/LambdaInstrumentationWrapper";
|
| 4 | +import {error} from "loglevel"; |
4 | 5 | const agentTestWrapper = AgentForTest.getInstance();
|
5 | 6 | agentTestWrapper.instrument()
|
6 | 7 |
|
7 |
| -describe('Lambda test', () => { |
8 |
| - let apiGatewayEventV1 = { |
9 |
| - // "version": "1.0", version isn't always present so cant be relied on |
10 |
| - "resource": "/my/path", |
11 |
| - "path": "/my/path", |
12 |
| - "httpMethod": "PUT", |
13 |
| - "headers": { |
14 |
| - "header1": "value1", |
15 |
| - "header2": "value2", |
16 |
| - 'x-forwarded-proto': 'https', |
17 |
| - 'content-type': 'application/json', |
18 |
| - }, |
19 |
| - "queryStringParameters": { |
20 |
| - "parameter1": "value1", |
21 |
| - "parameter2": "value" |
| 8 | +let apiGatewayEventV1 = { |
| 9 | + // "version": "1.0", version isn't always present so cant be relied on |
| 10 | + "resource": "/my/path", |
| 11 | + "path": "/my/path", |
| 12 | + "httpMethod": "PUT", |
| 13 | + "headers": { |
| 14 | + "header1": "value1", |
| 15 | + "header2": "value2", |
| 16 | + 'x-forwarded-proto': 'https', |
| 17 | + 'content-type': 'application/json', |
| 18 | + }, |
| 19 | + "queryStringParameters": { |
| 20 | + "parameter1": "value1", |
| 21 | + "parameter2": "value" |
| 22 | + }, |
| 23 | + "multiValueQueryStringParameters": { |
| 24 | + "parameter1": [ |
| 25 | + "value1", |
| 26 | + "value2" |
| 27 | + ], |
| 28 | + "parameter2": [ |
| 29 | + "value" |
| 30 | + ] |
| 31 | + }, |
| 32 | + "requestContext": { |
| 33 | + "accountId": "123456789012", |
| 34 | + "apiId": "id", |
| 35 | + "authorizer": { |
| 36 | + "claims": null, |
| 37 | + "scopes": null |
22 | 38 | },
|
23 |
| - "multiValueQueryStringParameters": { |
24 |
| - "parameter1": [ |
25 |
| - "value1", |
26 |
| - "value2" |
27 |
| - ], |
28 |
| - "parameter2": [ |
29 |
| - "value" |
30 |
| - ] |
31 |
| - }, |
32 |
| - "requestContext": { |
33 |
| - "accountId": "123456789012", |
34 |
| - "apiId": "id", |
35 |
| - "authorizer": { |
36 |
| - "claims": null, |
37 |
| - "scopes": null |
38 |
| - }, |
39 |
| - "domainName": "id.execute-api.us-east-1.amazonaws.com", |
40 |
| - "domainPrefix": "id", |
41 |
| - "extendedRequestId": "request-id", |
42 |
| - "httpMethod": "GET", |
43 |
| - "identity": { |
44 |
| - "accessKey": null, |
45 |
| - "accountId": null, |
46 |
| - "caller": null, |
47 |
| - "cognitoAuthenticationProvider": null, |
48 |
| - "cognitoAuthenticationType": null, |
49 |
| - "cognitoIdentityId": null, |
50 |
| - "cognitoIdentityPoolId": null, |
51 |
| - "principalOrgId": null, |
52 |
| - "sourceIp": "192.0.2.1", |
53 |
| - "user": null, |
54 |
| - "userAgent": "user-agent", |
55 |
| - "userArn": null, |
56 |
| - "clientCert": { |
57 |
| - "clientCertPem": "CERT_CONTENT", |
58 |
| - "subjectDN": "www.example.com", |
59 |
| - "issuerDN": "Example issuer", |
60 |
| - "serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", |
61 |
| - "validity": { |
62 |
| - "notBefore": "May 28 12:30:02 2019 GMT", |
63 |
| - "notAfter": "Aug 5 09:36:04 2021 GMT" |
64 |
| - } |
| 39 | + "domainName": "id.execute-api.us-east-1.amazonaws.com", |
| 40 | + "domainPrefix": "id", |
| 41 | + "extendedRequestId": "request-id", |
| 42 | + "httpMethod": "GET", |
| 43 | + "identity": { |
| 44 | + "accessKey": null, |
| 45 | + "accountId": null, |
| 46 | + "caller": null, |
| 47 | + "cognitoAuthenticationProvider": null, |
| 48 | + "cognitoAuthenticationType": null, |
| 49 | + "cognitoIdentityId": null, |
| 50 | + "cognitoIdentityPoolId": null, |
| 51 | + "principalOrgId": null, |
| 52 | + "sourceIp": "192.0.2.1", |
| 53 | + "user": null, |
| 54 | + "userAgent": "user-agent", |
| 55 | + "userArn": null, |
| 56 | + "clientCert": { |
| 57 | + "clientCertPem": "CERT_CONTENT", |
| 58 | + "subjectDN": "www.example.com", |
| 59 | + "issuerDN": "Example issuer", |
| 60 | + "serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", |
| 61 | + "validity": { |
| 62 | + "notBefore": "May 28 12:30:02 2019 GMT", |
| 63 | + "notAfter": "Aug 5 09:36:04 2021 GMT" |
65 | 64 | }
|
66 |
| - }, |
67 |
| - "path": "/my/path", |
68 |
| - "protocol": "HTTP/1.1", |
69 |
| - "requestId": "id=", |
70 |
| - "requestTime": "04/Mar/2020:19:15:17 +0000", |
71 |
| - "requestTimeEpoch": 1583349317135, |
72 |
| - "resourceId": null, |
73 |
| - "resourcePath": "/my/path", |
74 |
| - "stage": "$default" |
| 65 | + } |
75 | 66 | },
|
76 |
| - "pathParameters": null, |
77 |
| - "stageVariables": null, |
78 |
| - "body": '{\n\t"req-body": "some-data"\n}', |
79 |
| - "isBase64Encoded": false |
80 |
| - } |
| 67 | + "path": "/my/path", |
| 68 | + "protocol": "HTTP/1.1", |
| 69 | + "requestId": "id=", |
| 70 | + "requestTime": "04/Mar/2020:19:15:17 +0000", |
| 71 | + "requestTimeEpoch": 1583349317135, |
| 72 | + "resourceId": null, |
| 73 | + "resourcePath": "/my/path", |
| 74 | + "stage": "$default" |
| 75 | + }, |
| 76 | + "pathParameters": null, |
| 77 | + "stageVariables": null, |
| 78 | + "body": '{\n\t"req-body": "some-data"\n}', |
| 79 | + "isBase64Encoded": false |
| 80 | +} |
| 81 | + |
| 82 | +let response = { |
| 83 | + "statusCode": "200", |
| 84 | + "headers": { |
| 85 | + "a-Header": "some_VALUE", |
| 86 | + "Content-Type": "application/json" |
| 87 | + }, |
| 88 | + "body": JSON.stringify({"some_body_data": "response-data"}) |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +describe('Lambda test', () => { |
81 | 93 |
|
82 | 94 | let apiGatewayEventV2 = {
|
83 | 95 | // "version": "2.0", version isn't always present so cant be relied on
|
@@ -114,14 +126,6 @@ describe('Lambda test', () => {
|
114 | 126 | body: '{\n\t"req-body": "some-data"\n}',
|
115 | 127 | isBase64Encoded: false
|
116 | 128 | }
|
117 |
| - let response = { |
118 |
| - "statusCode": "200", |
119 |
| - "headers": { |
120 |
| - "a-Header": "some_VALUE", |
121 |
| - "Content-Type": "application/json" |
122 |
| - }, |
123 |
| - "body": JSON.stringify({"some_body_data": "response-data"}) |
124 |
| - } |
125 | 129 |
|
126 | 130 | beforeEach(() => {
|
127 | 131 | agentTestWrapper.stop()
|
@@ -200,3 +204,63 @@ describe('Lambda test', () => {
|
200 | 204 | expect(lambdaSpan.attributes['http.response.body']).to.equal('{"some_body_data":"response-data"}')
|
201 | 205 | })
|
202 | 206 | })
|
| 207 | + |
| 208 | +describe("manually instrument lambda function", () => { |
| 209 | + beforeEach(() => { |
| 210 | + agentTestWrapper.stop() |
| 211 | + }) |
| 212 | + |
| 213 | + afterEach( ()=> { |
| 214 | + agentTestWrapper.stop() |
| 215 | + }) |
| 216 | + it('can be manually instrumented', async () => { |
| 217 | + async function myHandler(event, context, callback){ |
| 218 | + return response |
| 219 | + } |
| 220 | + |
| 221 | + let wrappedHandler = agentTestWrapper.instrumentLambda(myHandler) |
| 222 | + await wrappedHandler(apiGatewayEventV1, {}, () => {}) |
| 223 | + |
| 224 | + let spans = agentTestWrapper.getSpans() |
| 225 | + expect(spans.length).to.equal(1) |
| 226 | + let lambdaSpan = spans[0] |
| 227 | + expect(lambdaSpan.attributes['http.method']).to.equal('PUT') |
| 228 | + expect(lambdaSpan.attributes['http.scheme']).to.equal('https') |
| 229 | + expect(lambdaSpan.attributes['http.host']).to.equal('id.execute-api.us-east-1.amazonaws.com') |
| 230 | + expect(lambdaSpan.attributes['http.target']).to.equal('/my/path') |
| 231 | + expect(lambdaSpan.attributes['http.request.header.content-type']).to.equal('application/json') |
| 232 | + expect(lambdaSpan.attributes['http.request.header.header1']).to.equal('value1') |
| 233 | + expect(lambdaSpan.attributes['http.request.body']).to.equal('{\n' + |
| 234 | + '\t"req-body": "some-data"\n' + |
| 235 | + '}') |
| 236 | + expect(lambdaSpan.attributes['http.status_code']).to.equal('200') |
| 237 | + expect(lambdaSpan.attributes['http.response.header.a-header']).to.equal('some_VALUE') |
| 238 | + expect(lambdaSpan.attributes['http.response.header.content-type']).to.equal('application/json') |
| 239 | + expect(lambdaSpan.attributes['http.response.body']).to.equal('{"some_body_data":"response-data"}') |
| 240 | + }) |
| 241 | + |
| 242 | + it('can be manually instrumented and handle error', async () => { |
| 243 | + async function myHandler(event, context, callback){ |
| 244 | + throw new Error("some error") |
| 245 | + } |
| 246 | + |
| 247 | + let wrappedHandler = agentTestWrapper.instrumentLambda(myHandler) |
| 248 | + try { |
| 249 | + await wrappedHandler(apiGatewayEventV1, {}, () => {}) |
| 250 | + } catch(_){} |
| 251 | + |
| 252 | + |
| 253 | + let spans = agentTestWrapper.getSpans() |
| 254 | + expect(spans.length).to.equal(1) |
| 255 | + let lambdaSpan = spans[0] |
| 256 | + expect(lambdaSpan.attributes['http.method']).to.equal('PUT') |
| 257 | + expect(lambdaSpan.attributes['http.scheme']).to.equal('https') |
| 258 | + expect(lambdaSpan.attributes['http.host']).to.equal('id.execute-api.us-east-1.amazonaws.com') |
| 259 | + expect(lambdaSpan.attributes['http.target']).to.equal('/my/path') |
| 260 | + expect(lambdaSpan.attributes['http.request.header.content-type']).to.equal('application/json') |
| 261 | + expect(lambdaSpan.attributes['http.request.header.header1']).to.equal('value1') |
| 262 | + expect(lambdaSpan.attributes['http.request.body']).to.equal('{\n' + |
| 263 | + '\t"req-body": "some-data"\n' + |
| 264 | + '}') |
| 265 | + }) |
| 266 | +}) |
0 commit comments