Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add httpEvent.operationId to the request context #1325

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ export default class HttpServer {
}
}

const additionalRequestContext = {}
if (httpEvent.operationId) {
additionalRequestContext.operationName = httpEvent.operationId
}

hapiOptions.tags = ['api']

const hapiHandler = async (request, h) => {
Expand Down Expand Up @@ -740,6 +745,7 @@ export default class HttpServer {
stage,
endpoint.routeKey,
stageVariables,
additionalRequestContext,
this.v3Utils,
)
: new LambdaProxyIntegrationEvent(
Expand All @@ -748,6 +754,7 @@ export default class HttpServer {
requestPath,
stageVariables,
endpoint.isHttpApi ? endpoint.routeKey : null,
additionalRequestContext,
this.v3Utils,
)

Expand Down
15 changes: 13 additions & 2 deletions src/events/http/lambda-events/LambdaProxyIntegrationEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ export default class LambdaProxyIntegrationEvent {
#request = null
#stage = null
#stageVariables = null

constructor(request, stage, path, stageVariables, routeKey = null, v3Utils) {
#additionalRequestContext = null

constructor(
request,
stage,
path,
stageVariables,
routeKey = null,
additionalRequestContext = null,
v3Utils,
) {
this.#path = path
this.#routeKey = routeKey
this.#request = request
this.#stage = stage
this.#stageVariables = stageVariables
this.#additionalRequestContext = additionalRequestContext || {}
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
Expand Down Expand Up @@ -202,6 +212,7 @@ export default class LambdaProxyIntegrationEvent {
userAgent: _headers['user-agent'] || '',
userArn: 'offlineContext_userArn',
},
operationName: this.#additionalRequestContext.operationName,
path: this.#path,
protocol: 'HTTP/1.1',
requestId: createUniqueId(),
Expand Down
14 changes: 12 additions & 2 deletions src/events/http/lambda-events/LambdaProxyIntegrationEventV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ export default class LambdaProxyIntegrationEventV2 {
#request = null
#stage = null
#stageVariables = null

constructor(request, stage, routeKey, stageVariables, v3Utils) {
#additionalRequestContext = null

constructor(
request,
stage,
routeKey,
stageVariables,
additionalRequestContext,
v3Utils,
) {
this.#routeKey = routeKey
this.#request = request
this.#stage = stage
this.#stageVariables = stageVariables
this.#additionalRequestContext = additionalRequestContext || {}
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
Expand Down Expand Up @@ -164,6 +173,7 @@ export default class LambdaProxyIntegrationEventV2 {
sourceIp: remoteAddress,
userAgent: _headers['user-agent'] || '',
},
operationName: this.#additionalRequestContext.operationName,
requestId: 'offlineContext_resourceId',
routeKey: this.#routeKey,
stage: this.#stage,
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/lambda-integration/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ exports.lambdaIntegrationStringified =
foo: 'bar',
})
}

exports.lambdaIntegrationWithOperationName =
async function lambdaIntegrationWithOperationName(event) {
return {
body: stringify({
operationName: event.requestContext.operationName,
}),
statusCode: 200,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ describe('lambda integration tests', () => {
path: '/dev/lambda-integration-stringified',
status: 200,
},
{
description: 'should return operation name from request context',
expected: {
operationName: 'getIntegrationWithOperationName',
},
path: '/dev/lambda-integration-with-operation-name',
status: 200,
},
].forEach(({ description, expected, path, status }) => {
test(description, async () => {
const url = joinUrl(TEST_BASE_URL, path)
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/lambda-integration/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ functions:
method: get
path: '/lambda-integration-stringified'
handler: handler.lambdaIntegrationStringified

integrationWithOperationName:
events:
- http:
method: get
operationId: getIntegrationWithOperationName
path: '/lambda-integration-with-operation-name'
handler: handler.lambdaIntegrationWithOperationName
30 changes: 30 additions & 0 deletions tests/old-unit/LambdaProxyIntegrationEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ describe('LambdaProxyIntegrationEvent', () => {
test('should match fixed attributes', () => {
expectFixedAttributes(lambdaProxyIntegrationEvent)
})

test('should not have operation name', () => {
expect(lambdaProxyIntegrationEvent.requestContext.operationName).toEqual(
undefined,
)
})
})

describe('with a GET /fn1 request with headers', () => {
Expand Down Expand Up @@ -698,4 +704,28 @@ describe('LambdaProxyIntegrationEvent', () => {
)
})
})

describe('with operation name', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1')
const request = requestBuilder.toObject()

let lambdaProxyIntegrationEvent

beforeEach(() => {
lambdaProxyIntegrationEvent = new LambdaProxyIntegrationEvent(
request,
stage,
null,
null,
null,
{ operationName: 'getFunctionOne' },
).create()
})

test('should have operation name', () => {
expect(lambdaProxyIntegrationEvent.requestContext.operationName).toEqual(
'getFunctionOne',
)
})
})
})