Skip to content

Commit

Permalink
[Synthetics] Remove legacy screenshot image codepath (#172684)
Browse files Browse the repository at this point in the history
## Summary

We have not used full screenshot image data since early alpha versions
of Synthetics, and there is no reason to _not_ use screenshot blocks
instead as they make far more efficient storage performance.

Removes the route that returns full image data. I may later include
changes to remove references to this on the client as well, or do that
in a follow-up.
  • Loading branch information
justinkambic committed Dec 8, 2023
1 parent 2c0e988 commit 8d3e8cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 115 deletions.
16 changes: 5 additions & 11 deletions x-pack/plugins/synthetics/server/queries/journey_screenshots.ts
Expand Up @@ -9,10 +9,12 @@ import {
getJourneyScreenshot,
ScreenshotReturnTypesUnion,
} from '../legacy_uptime/lib/requests/get_journey_screenshot';
import { isFullScreenshot, isRefResult, RefResult } from '../../common/runtime_types';
import { isRefResult, RefResult } from '../../common/runtime_types';
import { RouteContext, UptimeRouteContext } from '../routes/types';

export type ClientContract = Buffer | { screenshotRef: RefResult };
export interface ClientContract {
screenshotRef: RefResult;
}

function getSharedHeaders(stepName: string, totalSteps: number) {
return {
Expand All @@ -35,15 +37,7 @@ export const journeyScreenshotHandler = async ({
stepIndex,
});

if (isFullScreenshot(result) && typeof result.synthetics?.blob !== 'undefined') {
return response.ok({
body: Buffer.from(result.synthetics.blob, 'base64'),
headers: {
'content-type': result.synthetics.blob_mime || 'image/png', // falls back to 'image/png' for earlier versions of synthetics
...getSharedHeaders(result.synthetics.step.name, result.totalSteps),
},
});
} else if (isRefResult(result)) {
if (isRefResult(result)) {
return response.ok({
body: {
screenshotRef: result,
Expand Down
Expand Up @@ -105,94 +105,6 @@ describe('journey screenshot route', () => {
expect(response.body.screenshotRef).toEqual(mock);
});

it('returns full screenshot blob', async () => {
const mock = {
synthetics: {
blob: 'a blob',
blob_mime: 'image/jpeg',
step: {
name: 'a step name',
},
type: 'step/screenshot',
},
};

handlerContext.uptimeEsClient.search = jest.fn().mockResolvedValue({
body: {
hits: {
total: {
value: 3,
},
hits: [],
},
aggregations: { step: { image: { hits: { hits: [{ _source: mock }] } } } },
},
});

const route = createJourneyScreenshotRoute({
requests: {
getJourneyScreenshot: jest.fn().mockReturnValue(mock),
},
} as unknown as UMServerLibs);

expect(await route.handler(handlerContext as any)).toMatchInlineSnapshot(`
Object {
"body": Object {
"data": Array [
105,
185,
104,
],
"type": "Buffer",
},
"headers": Object {
"cache-control": "max-age=600",
"caption-name": "a step name",
"content-type": "image/jpeg",
"max-steps": "3",
},
"message": "Ok",
"status": 200,
}
`);
});

it('defaults to png when mime is undefined', async () => {
const mock = {
synthetics: {
blob: 'a blob',
step: {
name: 'a step name',
},
type: 'step/screenshot',
},
};
handlerContext.uptimeEsClient.search = jest.fn().mockResolvedValue({
body: {
hits: {
total: {
value: 3,
},
hits: [],
},
aggregations: { step: { image: { hits: { hits: [{ _source: mock }] } } } },
},
});
const route = createJourneyScreenshotRoute({
requests: {
getJourneyScreenshot: jest.fn().mockReturnValue(mock),
},
} as unknown as UMServerLibs);

const response = (await route.handler(
handlerContext as any
)) as IKibanaResponse<ClientContract>;

expect(response.status).toBe(200);
// @ts-expect-error incomplete implementation for testing
expect(response.headers['content-type']).toBe('image/png');
});

it('returns 404 for screenshot missing blob', async () => {
const route = createJourneyScreenshotRoute({
requests: {
Expand Down
Expand Up @@ -6,11 +6,7 @@
*/
import { IKibanaResponse } from '@kbn/core-http-server';
import { schema } from '@kbn/config-schema';
import {
isRefResult,
isFullScreenshot,
RefResult,
} from '../../../../common/runtime_types/ping/synthetics';
import { isRefResult, RefResult } from '../../../../common/runtime_types/ping/synthetics';
import { UMServerLibs } from '../../lib/lib';
import {
getJourneyScreenshot,
Expand All @@ -19,7 +15,9 @@ import {
import { RouteContext, UMRestApiRouteFactory, UptimeRouteContext } from '../types';
import { API_URLS } from '../../../../common/constants';

export type ClientContract = Buffer | { screenshotRef: RefResult };
export interface ClientContract {
screenshotRef: RefResult;
}

function getSharedHeaders(stepName: string, totalSteps: number) {
return {
Expand All @@ -30,7 +28,7 @@ function getSharedHeaders(stepName: string, totalSteps: number) {
}

export const createJourneyScreenshotRoute: UMRestApiRouteFactory<ClientContract> = (
libs: UMServerLibs
_libs: UMServerLibs
) => ({
method: 'GET',
path: API_URLS.JOURNEY_SCREENSHOT,
Expand Down Expand Up @@ -58,15 +56,7 @@ export const journeyScreenshotHandler = async ({
stepIndex,
});

if (isFullScreenshot(result) && typeof result.synthetics?.blob !== 'undefined') {
return response.ok({
body: Buffer.from(result.synthetics.blob, 'base64'),
headers: {
'content-type': result.synthetics.blob_mime || 'image/png', // falls back to 'image/png' for earlier versions of synthetics
...getSharedHeaders(result.synthetics.step.name, result.totalSteps),
},
});
} else if (isRefResult(result)) {
if (isRefResult(result)) {
return response.ok({
body: {
screenshotRef: result,
Expand Down

0 comments on commit 8d3e8cd

Please sign in to comment.