Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav3396 committed Jan 16, 2024
1 parent e6aa0f4 commit 1f74771
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getAutoTagKeys,
getJSONBase64Size,
getMaxRequestSize,
getMaxRequestSizeOnError,
isLambdaTraced,
spanHasErrors,
} from './utils';
Expand All @@ -26,8 +27,8 @@ export class SpansContainer {
private static currentSpansSize: number = 0;

static addSpan(span: BasicSpan): boolean {
// Memory optimization
if (spanHasErrors(span) || getMaxRequestSize() > this.currentSpansSize) {
// Memory optimization, take up to 10x maxSize because of smart span selection logic
if (spanHasErrors(span) || getMaxRequestSizeOnError() * 10 > this.currentSpansSize) {
this.spans[span.id] = span;
this.currentSpansSize += getJSONBase64Size(span);
logger.debug('Span created', span);
Expand Down
5 changes: 2 additions & 3 deletions src/reporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ describe('reporter', () => {
},
};

const spans = [error, end];
const expectedSpans = [error, end];
const spans = [end, error];
const expectedSpans = [end, error];
const size = getJSONBase64Size(expectedSpans);
TracerGlobals.setTracerInputs({ maxSizeForRequest: size - 30, maxSizeForRequestOnError: size });

Expand Down Expand Up @@ -974,7 +974,6 @@ describe('reporter', () => {

const spy = jest.spyOn(utils, 'getJSONBase64Size');

expect(reporter.shouldTrim(spans, 1)).toEqual(true);
expect(spy).not.toBeCalled();
});

Expand Down
16 changes: 6 additions & 10 deletions src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ export const sendSpans = async (spans: any[]): Promise<void> => {
safeExecute(logSpans)(rtt, spans);
};

export const shouldTrim = (spans, maxSendBytes: number): boolean => {
return (
spans.length > NUMBER_OF_SPANS_IN_REPORT_OPTIMIZATION || getJSONBase64Size(spans) > maxSendBytes
);
};

const isJsonContent = (payload: any, headers: Object) => {
return isString(payload) && headers['content-type'] && headers['content-type'].includes('json');
};
Expand Down Expand Up @@ -165,14 +159,16 @@ export const forgeAndScrubRequestBody = (
const start = new Date().getTime();
const beforeLength = spans.length;
const originalSize = spans.length;
const size = getJSONBase64Size(spans);

if (!isPruneTraceOff() && shouldTrim(spans, maxRequestSize)) {
if (
(!isPruneTraceOff() && spans.length > NUMBER_OF_SPANS_IN_REPORT_OPTIMIZATION) ||
size > maxSendBytes
) {
logger.debug(
`Starting trim spans [${spans.length}] bigger than: [${maxRequestSize}] before send`
);
if (getJSONBase64Size(spans) > maxRequestSize && spans.length > 0) {
spans = getPrioritizedSpans(spans, maxRequestSize);
}
spans = getPrioritizedSpans(spans, maxRequestSize);
}
spans = scrubSpans(spans);
if (originalSize - spans.length > 0) {
Expand Down

0 comments on commit 1f74771

Please sign in to comment.