All files / shared usage-entrypoint.js

100% Statements 8/8
100% Branches 4/4
100% Functions 0/0
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34            5x   5x 5x 5x             5x 5x       5x                   5x  
// Generic Lambda entry point — every lambda.Function in api-stack.ts points
// its `handler` here instead of directly at the real handler file, passing
// the real target via environment variables. This is the instrumentation
// seam described in docs/architecture/USAGE_METERING.md "Lambda
// instrumentation: CDK-level, not per-handler": it lets every lambda get
// usage tracking without editing a single business-logic file.
const path = require('path')
// First, so every module required below reads the full table names
require('./storage-names').expandStorageNames()
const { withUsageTracking } = require('./usage-tracking')
const { withLambdaStats } = require('./lambda-stats')
 
// The target is `${resource}/${operation}` unless USAGE_TARGET_MODULE overrides
// it — api-stack.ts only sets USAGE_RESOURCE/USAGE_OPERATION, so the module
// path isn't repeated as a third env var on every function (each variable is
// rendered once per function in the stack's CloudFormation template).
const targetModule =
  process.env.USAGE_TARGET_MODULE || `${process.env.USAGE_RESOURCE}/${process.env.USAGE_OPERATION}`
const target = require(path.join(__dirname, '..', targetModule))[
  process.env.USAGE_TARGET_EXPORT || 'handler'
]
 
const meta = {
  resource: process.env.USAGE_RESOURCE,
  operation: process.env.USAGE_OPERATION
}
 
// Two independent, decoupled instrumentation wrappers around the same
// target: withUsageTracking (billing/credits, via CloudWatch Logs Insights)
// and withLambdaStats (ops invocation-count/avg-duration, written directly
// to DynamoDB — see docs comment there). Composing them here costs one
// extra Date.now() pair, negligible next to an API Gateway round trip.
exports.handler = withUsageTracking(withLambdaStats(target, meta), meta)