From 3af96acc121571718bdeaf24fa9d83321a647215 Mon Sep 17 00:00:00 2001 From: Matheus Macabu Date: Mon, 2 Dec 2024 16:25:00 +0100 Subject: [PATCH] UsageInsights: Include panelId and panelName in events when available --- packages/grafana-data/src/types/datasource.ts | 1 + .../features/dashboard-scene/scene/DashboardScene.test.tsx | 2 ++ .../app/features/dashboard-scene/scene/DashboardScene.tsx | 1 + public/app/features/dashboard/state/PanelModel.ts | 1 + public/app/features/query/state/PanelQueryRunner.test.ts | 6 ++++++ public/app/features/query/state/PanelQueryRunner.ts | 6 ++++++ public/app/features/query/state/queryAnalytics.test.ts | 6 ++++++ public/app/features/query/state/queryAnalytics.ts | 5 ++--- 8 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index 11fdd815a00..2db63544741 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -557,6 +557,7 @@ export interface DataQueryRequest { rangeRaw?: RawTimeRange; timeInfo?: string; // The query time description (blue text in the upper right) panelId?: number; + panelName?: string; panelPluginId?: string; dashboardUID?: string; headers?: Record; diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx index 19a1a023f3d..8e70170c3ff 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx @@ -638,6 +638,7 @@ describe('DashboardScene', () => { app: CoreApp.Dashboard, dashboardUID: 'dash-1', panelId: 1, + panelName: 'Panel A', panelPluginId: 'table', }); }); @@ -653,6 +654,7 @@ describe('DashboardScene', () => { app: CoreApp.Dashboard, dashboardUID: 'dash-1', panelId: 1, + panelName: 'Panel A', panelPluginId: 'table', }); }); diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 94ad83c9e3f..edb9303fa1f 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -620,6 +620,7 @@ export class DashboardScene extends SceneObjectBase { app: CoreApp.Dashboard, dashboardUID: this.state.uid, panelId, + panelName: panel?.state?.title, panelPluginId: panel?.state.pluginId, scopes: this._scopesFacade?.value, }; diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index b8ca782906c..1ad7b07c086 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -380,6 +380,7 @@ export class PanelModel implements DataConfigSource, IPanelModel { datasource: this.datasource, queries: this.targets, panelId: this.id, + panelName: this.title, panelPluginId: this.type, dashboardUID: dashboardUID, timezone: dashboardTimezone, diff --git a/public/app/features/query/state/PanelQueryRunner.test.ts b/public/app/features/query/state/PanelQueryRunner.test.ts index e57cc8ea7a1..a870bfb3d40 100644 --- a/public/app/features/query/state/PanelQueryRunner.test.ts +++ b/public/app/features/query/state/PanelQueryRunner.test.ts @@ -160,6 +160,7 @@ function describeQueryRunnerScenario( raw: { from: '1d', to: 'now' }, }, panelId: 1, + panelName: 'PanelName', queries: [{ refId: 'A' }], } as QueryRunnerOptions; @@ -198,6 +199,11 @@ describe('PanelQueryRunner', () => { expect(ctx.queryCalledWith?.scopedVars.__interval!.text).toBe('5m'); expect(ctx.queryCalledWith?.scopedVars.__interval_ms!.text).toBe('300000'); }); + + it('should pass the panel id and name', async () => { + expect(ctx.queryCalledWith?.panelId).toBe(1); + expect(ctx.queryCalledWith?.panelName).toBe('PanelName'); + }); }); describeQueryRunnerScenario('with maxDataPoints', (ctx) => { diff --git a/public/app/features/query/state/PanelQueryRunner.ts b/public/app/features/query/state/PanelQueryRunner.ts index 329d4186a8e..ffefaf7b4f1 100644 --- a/public/app/features/query/state/PanelQueryRunner.ts +++ b/public/app/features/query/state/PanelQueryRunner.ts @@ -50,6 +50,7 @@ export interface QueryRunnerOptions< datasource: DataSourceRef | DataSourceApi | null; queries: TQuery[]; panelId?: number; + panelName?: string; panelPluginId?: string; dashboardUID?: string; timezone: TimeZone; @@ -258,6 +259,7 @@ export class PanelQueryRunner { timezone, datasource, panelId, + panelName, panelPluginId, dashboardUID, timeRange, @@ -283,6 +285,7 @@ export class PanelQueryRunner { requestId: getNextRequestId(), timezone, panelId, + panelName, panelPluginId, dashboardUID, range: timeRange, @@ -328,6 +331,9 @@ export class PanelQueryRunner { request.intervalMs = norm.intervalMs; request.filters = this.templateSrv.getAdhocFilters(ds.name, true); + request.panelId = panelId; + request.panelName = panelName; + this.lastRequest = request; this.pipeToSubject(runRequest(ds, request), panelId, false, addErroDSVariable); diff --git a/public/app/features/query/state/queryAnalytics.test.ts b/public/app/features/query/state/queryAnalytics.test.ts index 3e450b21b17..2b0863132b2 100644 --- a/public/app/features/query/state/queryAnalytics.test.ts +++ b/public/app/features/query/state/queryAnalytics.test.ts @@ -118,6 +118,7 @@ describe('emitDataRequestEvent', () => { it('Should report meta analytics', () => { const data = getTestData({ panelId: 2, + panelName: 'Panel Name2', }); emitDataRequestEvent(datasource)(data); @@ -130,6 +131,7 @@ describe('emitDataRequestEvent', () => { datasourceType: datasource.type, source: CoreApp.Dashboard, panelId: 2, + panelName: 'Panel Name2', dashboardUid: 'test', // from dashboard srv dataSize: 0, duration: 1, @@ -144,6 +146,7 @@ describe('emitDataRequestEvent', () => { const data = getTestData( { panelId: 2, + panelName: 'Panel Name2', }, partiallyCachedSeries ); @@ -158,6 +161,7 @@ describe('emitDataRequestEvent', () => { datasourceType: datasource.type, source: CoreApp.Dashboard, panelId: 2, + panelName: 'Panel Name2', dashboardUid: 'test', dataSize: 2, duration: 1, @@ -172,6 +176,7 @@ describe('emitDataRequestEvent', () => { const data = getTestData( { panelId: 2, + panelName: 'Panel Name2', }, multipleDataframesWithSameRefId ); @@ -186,6 +191,7 @@ describe('emitDataRequestEvent', () => { datasourceType: datasource.type, source: CoreApp.Dashboard, panelId: 2, + panelName: 'Panel Name2', dashboardUid: 'test', // from dashboard srv dataSize: 2, duration: 1, diff --git a/public/app/features/query/state/queryAnalytics.ts b/public/app/features/query/state/queryAnalytics.ts index 337fe6d4b00..cdc6057f2db 100644 --- a/public/app/features/query/state/queryAnalytics.ts +++ b/public/app/features/query/state/queryAnalytics.ts @@ -31,6 +31,8 @@ export function emitDataRequestEvent(datasource: DataSourceApi) { panelId: 0, panelPluginId: data.request?.panelPluginId, duration: data.request.endTime! - data.request.startTime, + ...(data?.request?.panelId && Number.isInteger(data.request.panelId) && { panelId: data.request.panelId }), + ...(data?.request?.panelName && { panelName: data.request.panelName }), }; enrichWithInfo(eventData, data); @@ -62,9 +64,6 @@ export function emitDataRequestEvent(datasource: DataSourceApi) { eventData.totalQueries = Object.keys(queryCacheStatus).length; eventData.cachedQueries = Object.values(queryCacheStatus).filter((val) => val === true).length; - if (data.request && Number.isInteger(data.request.panelId)) { - eventData.panelId = data.request.panelId; - } const dashboard = getDashboardSrv().getCurrent(); if (dashboard) {