Skip to content

Commit

Permalink
[Lens] Add ES-request time telemetry to Lens embeddable (elastic#192743)
Browse files Browse the repository at this point in the history
## Summary

Similar to elastic#192245, this adds
request-time to the Lens Embeddable. This would allow to track these
metrics anywhere where Lens is embedded.

This is particularly important for Lens embedded in a Dashboard since
all performance journeys are written against Dashboards.

### Checklist

Delete any items that are not applicable to this PR.

- [x] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Marco Vettorello <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2024
1 parent 8a79173 commit 682afb7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { DropIllustration } from '@kbn/chart-icons';
import { useDragDropContext, DragDropIdentifier, Droppable } from '@kbn/dom-drag-drop';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import { ChartSizeSpec, isChartSizeEvent } from '@kbn/chart-expressions-common';
import { estypes } from '@elastic/elasticsearch';
import { getSuccessfulRequestTimings } from '../../../report_performance_metric_util';
import { trackUiCounterEvents } from '../../../lens_ui_telemetry';
import { getSearchWarningMessages } from '../../../utils';
import {
Expand Down Expand Up @@ -205,8 +205,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
eventName: 'lensVisualizationRenderTime',
duration: currentTime - visualizationRenderStartTime.current,
key1: 'time_to_data',
value1:
dataReceivedTime.current - visualizationRenderStartTime.current - esTookTime.current,
value1: dataReceivedTime.current - visualizationRenderStartTime.current,
key2: 'time_to_render',
value2: currentTime - dataReceivedTime.current,
key3: 'es_took',
Expand Down Expand Up @@ -268,13 +267,9 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
searchService: plugins.data.search,
}
);
esTookTime.current = adapters.requests.getRequests().reduce((maxTime, { response }) => {
const took =
(response?.json as { rawResponse: estypes.SearchResponse | undefined } | undefined)
?.rawResponse?.took ?? 0;

return Math.max(maxTime, took);
}, 0);
const timings = getSuccessfulRequestTimings(adapters);
esTookTime.current = timings ? timings.esTookTotal : 0;
}

if (requestWarnings.length) {
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { css } from '@emotion/react';
import { i18n } from '@kbn/i18n';
import { render, unmountComponentAtNode } from 'react-dom';
import { ENABLE_ESQL } from '@kbn/esql-utils';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import {
DataViewBase,
EsQueryConfig,
Expand Down Expand Up @@ -86,6 +87,7 @@ import { DataViewSpec } from '@kbn/data-views-plugin/common';
import { FormattedMessage } from '@kbn/i18n-react';
import { useEuiFontSize, useEuiTheme, EuiEmptyPrompt } from '@elastic/eui';
import { canTrackContentfulRender } from '@kbn/presentation-containers';
import { getSuccessfulRequestTimings } from '../report_performance_metric_util';
import { getExecutionContextEvents, trackUiCounterEvents } from '../lens_ui_telemetry';
import { Document } from '../persistence';
import { ExpressionWrapper, ExpressionWrapperProps } from './expression_wrapper';
Expand Down Expand Up @@ -1076,6 +1078,18 @@ export class Embeddable
...this.getOutput(),
rendered: true,
});

const inspectorAdapters = this.getInspectorAdapters();
const timings = getSuccessfulRequestTimings(inspectorAdapters);
if (timings) {
const esRequestMetrics = {
eventName: 'lens_chart_es_request_totals',
duration: timings.requestTimeTotal,
key1: 'es_took_total',
value1: timings.esTookTotal,
};
reportPerformanceMetricEvent(this.deps.coreStart.analytics, esRequestMetrics);
}
};

getExecutionContext() {
Expand Down
36 changes: 36 additions & 0 deletions x-pack/plugins/lens/public/report_performance_metric_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RequestStatus } from '@kbn/inspector-plugin/common';
import type { Adapters } from '@kbn/inspector-plugin/public';
import { estypes } from '@elastic/elasticsearch';

export interface ILensRequestPerformance {
requestTimeTotal: number;
esTookTotal: number;
}

export function getSuccessfulRequestTimings(
inspectorAdapters: Adapters
): ILensRequestPerformance | null {
const requests = inspectorAdapters.requests?.getRequests() || [];

let esTookTotal = 0;
let requestTimeTotal = 0;
for (let i = 0; i < requests.length; i++) {
const request = requests[i];
if (request.status !== RequestStatus.OK) {
return null;
}
esTookTotal +=
(request.response?.json as { rawResponse: estypes.SearchResponse | undefined } | undefined)
?.rawResponse?.took ?? 0;
requestTimeTotal += request.time || 0;
}

return { requestTimeTotal, esTookTotal };
}

0 comments on commit 682afb7

Please sign in to comment.