Skip to content

Commit

Permalink
Limit the size of API Gateway trigger's body attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalSumislawski committed Oct 16, 2023
1 parent ef8ce13 commit cc4b184
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
// the handler happened to both call the callback and complete a returned Promise, whichever happens first will
// win and the latter will be ignored.
const wrappedCallback = plugin._wrapCallback(
config,
callback,
lambdaSpan,
triggerSpan
Expand Down Expand Up @@ -309,13 +310,13 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
value => {
strict(triggerSpan);

void plugin._endWrapperSpan(triggerSpan, value, undefined);
void plugin._endWrapperSpan(config, triggerSpan, value, undefined);

return value;
},
async error => {
strict(triggerSpan);
await plugin._endWrapperSpan(triggerSpan, undefined, error);
await plugin._endWrapperSpan(config, triggerSpan, undefined, error);
throw error; // We don't want the instrumentation to hide the error from AWS
}
);
Expand All @@ -325,6 +326,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {

//if (hasLambdaSynchronouslyThrown) {
void plugin._endWrapperSpan(
config,
triggerSpan,
innerResult,
undefined
Expand All @@ -338,7 +340,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
error => {
if (error) {
strict(triggerSpan);
void plugin._endWrapperSpan(triggerSpan, undefined, error);
void plugin._endWrapperSpan(config, triggerSpan, undefined, error);
void plugin._flush();
}
}
Expand Down Expand Up @@ -389,9 +391,10 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
}

private async _endWrapperSpan(
config: AwsLambdaInstrumentationConfig,
span: Span,
lambdaResponse: any,
errorFromLambda: string | Error | null | undefined
errorFromLambda: string | Error | null | undefined,
) {
if (errorFromLambda) {
span.recordException(errorFromLambda);
Expand All @@ -411,12 +414,13 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
this.triggerOrigin
)
) {
finalizeSpan(this.triggerOrigin, span, lambdaResponse);
finalizeSpan(config, this.triggerOrigin, span, lambdaResponse);
}
span.end();
}

private _wrapCallback(
config: AwsLambdaInstrumentationConfig,
originalAWSLambdaCallback: Callback,
span: Span,
wrapperSpan?: Span
Expand All @@ -428,7 +432,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {

plugin._endSpan(span, err);
if (wrapperSpan) {
void plugin._endWrapperSpan(wrapperSpan, res, err);
void plugin._endWrapperSpan(config, wrapperSpan, res, err);
}

void this._flush().then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { SpanStatusCode } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { APIGatewayProxyResult } from 'aws-lambda';

const DEFAULT_OTEL_PAYLOAD_SIZE_LIMIT = 50 * 1024;

export function isGatewayResult(result: any): result is APIGatewayProxyResult {
return (
result &&
Expand All @@ -27,7 +29,7 @@ export function isGatewayResult(result: any): result is APIGatewayProxyResult {
);
}

export const finalizeApiGatewaySpan: SpanFinalizer = (span, response) => {
export const finalizeApiGatewaySpan: SpanFinalizer = (span, response, config) => {
if (!isGatewayResult(response)) {
return;
}
Expand All @@ -51,9 +53,11 @@ export const finalizeApiGatewaySpan: SpanFinalizer = (span, response) => {
const { body } = response;

if (body) {
const bodyStr = typeof body === 'object' ? JSON.stringify(body) : body;
const payloadSizeLimit = config?.payloadSizeLimit ?? DEFAULT_OTEL_PAYLOAD_SIZE_LIMIT;
span.setAttribute(
'http.response.body',
typeof body === 'object' ? JSON.stringify(body) : body
bodyStr.substring(0, payloadSizeLimit)
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { SpanOptions } from '@opentelemetry/api/build/src/trace/SpanOptions';
import { Span } from '@opentelemetry/api';
import { TriggerOrigin } from './index';
import { AwsLambdaInstrumentationConfig } from '../types';

export type TriggerValidator<T> = (event: any) => event is T;
export interface TriggerSpanInitializerResult {
Expand All @@ -27,7 +28,7 @@ export interface TriggerSpanInitializerResult {
export type TriggerSpanInitializer<T> = (
event: T
) => TriggerSpanInitializerResult;
export type SpanFinalizer = (span: Span, response?: any) => void;
export type SpanFinalizer = (span: Span, response?: any, config?: AwsLambdaInstrumentationConfig) => void;

export interface LambdaTrigger<T> {
validator: TriggerValidator<T>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EventBridgeTrigger } from './event-bridge';
import { DynamoDBTrigger } from './dynamodb-stream';
import { Span } from '@opentelemetry/api';
import { CloudWatchLogsTrigger } from './cloudwatch-logs';
import { AwsLambdaInstrumentationConfig } from '../types';

export const LambdaAttributes = {
TRIGGER_SERVICE: 'faas.trigger.type',
Expand Down Expand Up @@ -86,12 +87,13 @@ export const getEventTrigger = (
};

export const finalizeSpan = (
config: AwsLambdaInstrumentationConfig,
origin: TriggerOrigin,
triggerSpan: Span,
response: any
response: any,
) => {
const trigger = lambdaTriggers[origin];
if (trigger.finalizer) {
trigger.finalizer(triggerSpan, response);
trigger.finalizer(triggerSpan, response, config);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface AwsLambdaInstrumentationConfig extends InstrumentationConfig {
disableAwsContextPropagation?: boolean;
detectTrigger?: boolean;
eventContextExtractor?: EventContextExtractor;
payloadSizeLimit?: number;
}

0 comments on commit cc4b184

Please sign in to comment.