Skip to content

Commit

Permalink
Merge pull request #127 from instana/caching_fix
Browse files Browse the repository at this point in the history
INSTA-5506: Remove backend trace id for cached responses
  • Loading branch information
Argho Roy authored and GitHub Enterprise committed Oct 25, 2024
2 parents 2a0b101 + 67ef08d commit 73ee785
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Upgrade dev packages to address security issues
- Refactored code for autoPageDetection
- Fix autoPageDetection to set page name for initial page load
- Remove backendTraceId for cached responses

## 1.7.2

Expand Down
11 changes: 11 additions & 0 deletions lib/resources/timingSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cachingTypes, initiatorTypes } from './consts';
import {info} from '../debug';
import vars from '../vars';

export function serializeEntryToArray(entry: PerformanceResourceTiming) {
Expand All @@ -10,14 +11,17 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) {

// When timing data is available, we can provide additional information about
// caching and resource sizes.
let isCached = false;
if (typeof entry['transferSize'] === 'number' &&
typeof entry['encodedBodySize'] === 'number' &&
// All this information may not be available due to the timing allow origin check.
entry['encodedBodySize'] > 0) {
if (entry['transferSize'] === 0) {
result.push(cachingTypes.cached);
isCached = true;
} else if (entry['transferSize'] > 0 && (entry['encodedBodySize'] === 0 || entry['transferSize'] < entry['encodedBodySize'])) {
result.push(cachingTypes.validated);
isCached = true;
} else {
result.push(cachingTypes.fullLoad);
}
Expand Down Expand Up @@ -75,13 +79,20 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) {
const serverTiming = serverTimings[i];
if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) {
backendTraceId = serverTiming['description'];
if (isCached) {
if (DEBUG) {
info('Response is cached, removed backendTraceId from response');
}
backendTraceId = '';
}
}
}
}
} catch (e) {
// Some browsers may not grant access to the field when the Timing-Allow-Origin
// check fails. Better be safe than sorry here.
}

result.push(backendTraceId);

if (hasValidTimings) {
Expand Down
12 changes: 9 additions & 3 deletions test/unit/resources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('resources/timingSerializer', () => {
decodedBodySize: 16786,
serverTiming: []
};
expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot();
const result = toHumanReadableEntry(serializeEntry(entry));
expect(result).toMatchSnapshot();
expect(result['backendTraceId']).toBe('');
});

it('must identify full asset retrieval', () => {
Expand Down Expand Up @@ -90,7 +92,9 @@ describe('resources/timingSerializer', () => {
}
]
};
expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot();
const result = toHumanReadableEntry(serializeEntry(entry));
expect(result).toMatchSnapshot();
expect(result['backendTraceId']).not.toBeNull();
});

it('must identify cache validation', () => {
Expand Down Expand Up @@ -118,7 +122,9 @@ describe('resources/timingSerializer', () => {
decodedBodySize: 16786,
serverTiming: []
};
expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot();
const result = toHumanReadableEntry(serializeEntry(entry));
expect(result).toMatchSnapshot();
expect(result['backendTraceId']).toBe('');
});
});
});
Expand Down

0 comments on commit 73ee785

Please sign in to comment.