From 8f10bb320ab3f0165ba2359737ef486a9320a5b2 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Mon, 17 Jun 2024 17:36:53 +0200 Subject: [PATCH 1/6] added perf metrics --- .../embeddable/embeddable.tsx | 14 +++++- .../exploratory_view/embeddable/index.tsx | 6 ++- .../hooks/use_ebt_telemetry.ts | 50 +++++++++++++++++++ .../exploratory_view/public/plugin.ts | 10 +++- 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx index ed06d786124f39..ef023feb14c576 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx @@ -18,6 +18,8 @@ import { import { ViewMode } from '@kbn/embeddable-plugin/common'; import { observabilityFeatureId } from '@kbn/observability-shared-plugin/public'; import styled from 'styled-components'; +import { AnalyticsServiceSetup } from '@kbn/core-analytics-browser'; +import { useEBTTelemetry } from '../hooks/use_ebt_telemetry'; import { AllSeries } from '../../../..'; import { AppDataType, ReportViewType } from '../types'; import { OperationTypeComponent } from '../series_editor/columns/operation_type_select'; @@ -61,6 +63,7 @@ export interface ExploratoryEmbeddableComponentProps extends ExploratoryEmbeddab lens: LensPublicStart; dataViewState: DataViewState; lensFormulaHelper?: FormulaPublicApi; + analytics?: AnalyticsServiceSetup; } // eslint-disable-next-line import/no-default-export @@ -88,6 +91,7 @@ export default function Embeddable(props: ExploratoryEmbeddableComponentProps) { lineHeight = 32, searchSessionId, onLoad, + analytics, } = props; const LensComponent = lens?.EmbeddableComponent; const LensSaveModalComponent = lens?.SaveModalComponent; @@ -103,6 +107,11 @@ export default function Embeddable(props: ExploratoryEmbeddableComponentProps) { const timeRange = customTimeRange ?? series?.time; + const { reportEvent } = useEBTTelemetry({ + analytics, + queryName: `${series.dataType}_${series.name}`, + }); + const actions = useActions({ withActions, attributes, @@ -198,7 +207,10 @@ export default function Embeddable(props: ExploratoryEmbeddableComponentProps) { extraActions={actions} viewMode={ViewMode.VIEW} searchSessionId={searchSessionId} - onLoad={onLoad} + onLoad={(loading, inspectorAdapters) => { + reportEvent(inspectorAdapters); + onLoad?.(loading); + }} /> {isSaveOpen && attributesJSON && ( diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts new file mode 100644 index 00000000000000..93f1b2fe7bc630 --- /dev/null +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts @@ -0,0 +1,50 @@ +/* + * 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 { DefaultInspectorAdapters } from '@kbn/expressions-plugin/common'; +import { reportPerformanceMetricEvent } from '@kbn/ebt-tools'; +import { AnalyticsServiceSetup } from '@kbn/core-analytics-browser'; + +export const useEBTTelemetry = ({ + analytics, + queryName, +}: { + analytics?: AnalyticsServiceSetup; + queryName?: string; +}) => { + const reportEvent = (inspectorAdapters?: Partial) => { + if (inspectorAdapters) { + const { requests } = inspectorAdapters; + if (requests && analytics) { + const listReq = requests.getRequests(); + if (listReq.length > 0) { + // find the highest query time + const duration = listReq.reduce((acc, req) => { + const queryTime = Number(req.stats?.queryTime.value.split('ms')?.[0]); + return queryTime > acc ? queryTime : acc; + }, 0); + + if (duration) { + reportPerformanceMetricEvent(analytics, { + eventName: 'exploratory_view_query_time', + duration, + meta: + (queryName && { + queryName, + }) || + undefined, + }); + } + } + } + } + }; + + return { + reportEvent, + }; +}; diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/plugin.ts b/x-pack/plugins/observability_solution/exploratory_view/public/plugin.ts index 182884d42a51ac..a1101e0fb4e4ac 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/plugin.ts +++ b/x-pack/plugins/observability_solution/exploratory_view/public/plugin.ts @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n'; import { BehaviorSubject } from 'rxjs'; import { SharePluginSetup, SharePluginStart } from '@kbn/share-plugin/public'; import { + AnalyticsServiceSetup, AppMountParameters, AppUpdater, CoreSetup, @@ -85,6 +86,8 @@ export class Plugin { private readonly appUpdater$ = new BehaviorSubject(() => ({})); + private analyticsService?: AnalyticsServiceSetup; + constructor(private readonly initContext: PluginInitializerContext) {} public setup( @@ -129,6 +132,8 @@ export class Plugin ], }); + this.analyticsService = core.analytics; + return { register: registerDataHandler, }; @@ -138,7 +143,10 @@ export class Plugin return { createExploratoryViewUrl, getAppDataView: getAppDataView(pluginsStart.dataViews), - ExploratoryViewEmbeddable: getExploratoryViewEmbeddable({ ...coreStart, ...pluginsStart }), + ExploratoryViewEmbeddable: getExploratoryViewEmbeddable( + { ...coreStart, ...pluginsStart }, + this.analyticsService + ), }; } } From e9bb5ac3eca3c4eea006cae277a2a006185bc8f3 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 17 Jun 2024 16:01:49 +0000 Subject: [PATCH 2/6] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- .../observability_solution/exploratory_view/tsconfig.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/exploratory_view/tsconfig.json b/x-pack/plugins/observability_solution/exploratory_view/tsconfig.json index 87002a070158a7..3aca08a23f4ef0 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/tsconfig.json +++ b/x-pack/plugins/observability_solution/exploratory_view/tsconfig.json @@ -41,7 +41,10 @@ "@kbn/observability-ai-assistant-plugin", "@kbn/shared-ux-link-redirect-app", "@kbn/react-kibana-context-render", - "@kbn/react-kibana-mount" + "@kbn/react-kibana-mount", + "@kbn/core-analytics-browser", + "@kbn/expressions-plugin", + "@kbn/ebt-tools" ], "exclude": ["target/**/*"] } From dbd002c70ec4e5cc87ac1069254b997647cc06fe Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Thu, 20 Jun 2024 13:41:40 +0200 Subject: [PATCH 3/6] fix tests --- .../shared/exploratory_view/embeddable/embeddable.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx index ef023feb14c576..bc587908857671 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx @@ -109,7 +109,7 @@ export default function Embeddable(props: ExploratoryEmbeddableComponentProps) { const { reportEvent } = useEBTTelemetry({ analytics, - queryName: `${series.dataType}_${series.name}`, + queryName: series ? `${series.dataType}_${series.name}` : title, }); const actions = useActions({ From 411b42f7fb1a3e6f02546d06bed13b67f79bc289 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Thu, 20 Jun 2024 14:16:18 +0200 Subject: [PATCH 4/6] Update x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts --- .../shared/exploratory_view/hooks/use_ebt_telemetry.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts index 93f1b2fe7bc630..f38fc58dcd2075 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts @@ -22,7 +22,8 @@ export const useEBTTelemetry = ({ if (requests && analytics) { const listReq = requests.getRequests(); if (listReq.length > 0) { - // find the highest query time + // find the highest query time, since a lens chart can contains more than on query, it doesn't make sense to + // report all of them , only the query with highest latency needs to be reported const duration = listReq.reduce((acc, req) => { const queryTime = Number(req.stats?.queryTime.value.split('ms')?.[0]); return queryTime > acc ? queryTime : acc; From 0c92702b3aa62c2bfe225cfef8a925c16c9d362d Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Thu, 20 Jun 2024 17:06:46 +0200 Subject: [PATCH 5/6] lint --- .../shared/exploratory_view/hooks/use_ebt_telemetry.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts index f38fc58dcd2075..639b6332ab6122 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/hooks/use_ebt_telemetry.ts @@ -21,9 +21,10 @@ export const useEBTTelemetry = ({ const { requests } = inspectorAdapters; if (requests && analytics) { const listReq = requests.getRequests(); + if (listReq.length > 0) { - // find the highest query time, since a lens chart can contains more than on query, it doesn't make sense to - // report all of them , only the query with highest latency needs to be reported + // find the highest query time, since a lens chart can contains more than on query, it doesn't make sense to + // report all of them , only the query with highest latency needs to be reported const duration = listReq.reduce((acc, req) => { const queryTime = Number(req.stats?.queryTime.value.split('ms')?.[0]); return queryTime > acc ? queryTime : acc; From 6fbf1fce61799981a83046850ce72b92c0a2ca53 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Fri, 21 Jun 2024 06:47:39 +0200 Subject: [PATCH 6/6] fix types --- .../shared/exploratory_view/embeddable/embeddable.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx index bc587908857671..a0079568803b66 100644 --- a/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx +++ b/x-pack/plugins/observability_solution/exploratory_view/public/components/shared/exploratory_view/embeddable/embeddable.tsx @@ -109,7 +109,11 @@ export default function Embeddable(props: ExploratoryEmbeddableComponentProps) { const { reportEvent } = useEBTTelemetry({ analytics, - queryName: series ? `${series.dataType}_${series.name}` : title, + queryName: series + ? `${series.dataType}_${series.name}` + : typeof title === 'string' + ? title + : 'Exp View embeddable query', }); const actions = useActions({