From f3fd2de9dd22d35e721787534b22edcf5ad685ef Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:57:18 +0200 Subject: [PATCH] Loki: Remove tracking `grafana_loki_query_executed` and add `grafana_explore_loki_query_executed` (#86484) * Loki: Remove and add * Update public/app/plugins/datasource/loki/tracking.ts Co-authored-by: Sven Grossmann --------- Co-authored-by: Sven Grossmann --- .../datasource/loki/datasource.test.ts | 15 ++--- .../plugins/datasource/loki/tracking.test.ts | 67 ++++++++++--------- .../app/plugins/datasource/loki/tracking.ts | 16 +++-- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/public/app/plugins/datasource/loki/datasource.test.ts b/public/app/plugins/datasource/loki/datasource.test.ts index b2f1e1328b0..665f8d89c8c 100644 --- a/public/app/plugins/datasource/loki/datasource.test.ts +++ b/public/app/plugins/datasource/loki/datasource.test.ts @@ -205,7 +205,7 @@ describe('LokiDatasource', () => { it('should report query interaction', async () => { await runTest(80, '40', 80, CoreApp.Explore); expect(reportInteraction).toHaveBeenCalledWith( - 'grafana_loki_query_executed', + 'grafana_explore_loki_query_executed', expect.objectContaining({ query_type: 'logs', line_limit: 80, @@ -219,16 +219,9 @@ describe('LokiDatasource', () => { expect(reportInteraction).not.toBeCalled(); }); - it('should not report query interaction for panel edit query', async () => { - await runTest(80, '40', 80, CoreApp.PanelEditor); - expect(reportInteraction).toHaveBeenCalledWith( - 'grafana_loki_query_executed', - expect.objectContaining({ - query_type: 'logs', - line_limit: 80, - obfuscated_query: '{Identifier=String}', - }) - ); + it('should not report query interaction for unknown app query', async () => { + await runTest(80, '40', 80, CoreApp.Unknown); + expect(reportInteraction).not.toBeCalled(); }); }); diff --git a/public/app/plugins/datasource/loki/tracking.test.ts b/public/app/plugins/datasource/loki/tracking.test.ts index 6dbd998592b..7048692c7f1 100644 --- a/public/app/plugins/datasource/loki/tracking.test.ts +++ b/public/app/plugins/datasource/loki/tracking.test.ts @@ -1,4 +1,4 @@ -import { DashboardLoadedEvent, DataQueryRequest, dateTime } from '@grafana/data'; +import { CoreApp, DashboardLoadedEvent, DataQueryRequest, dateTime } from '@grafana/data'; import { QueryEditorMode } from '@grafana/experimental'; import { reportInteraction } from '@grafana/runtime'; @@ -31,7 +31,7 @@ const originalRequest = { { expr: 'count_over_time({hidden="true"}[1m])', refId: 'C', ...baseTarget, hide: true }, ], range, - app: 'explore', + app: CoreApp.Explore, } as DataQueryRequest; const requests: LokiGroupedRequest[] = [ @@ -64,37 +64,46 @@ beforeEach(() => { jest.mocked(reportInteraction).mockClear(); }); -test('Tracks queries', () => { - trackQuery({ data: [] }, originalRequest, new Date()); +describe('Tracks queries', () => { + it('should track queries in Explore', () => { + trackQuery({ data: [] }, originalRequest, new Date()); + expect(reportInteraction).toHaveBeenCalledWith('grafana_explore_loki_query_executed', { + bytes_processed: 0, + editor_mode: 'builder', + grafana_version: '1.0', + has_data: false, + has_error: false, + is_split: false, + legend: undefined, + line_limit: undefined, + obfuscated_query: 'count_over_time({Identifier=String}[1m])', + query_type: 'metric', + query_vector_type: undefined, + resolution: 1, + simultaneously_executed_query_count: 2, + simultaneously_hidden_query_count: 1, + time_range_from: '2023-02-08T05:00:00.000Z', + time_range_to: '2023-02-10T06:00:00.000Z', + time_taken: 0, + predefined_operations_applied: 'n/a', + }); + }); - expect(reportInteraction).toHaveBeenCalledWith('grafana_loki_query_executed', { - app: 'explore', - bytes_processed: 0, - editor_mode: 'builder', - grafana_version: '1.0', - has_data: false, - has_error: false, - is_split: false, - legend: undefined, - line_limit: undefined, - obfuscated_query: 'count_over_time({Identifier=String}[1m])', - query_type: 'metric', - query_vector_type: undefined, - resolution: 1, - simultaneously_executed_query_count: 2, - simultaneously_hidden_query_count: 1, - time_range_from: '2023-02-08T05:00:00.000Z', - time_range_to: '2023-02-10T06:00:00.000Z', - time_taken: 0, - predefined_operations_applied: 'n/a', + it('should not track queries if app is not Explore', () => { + trackQuery({ data: [] }, { ...originalRequest, app: CoreApp.PanelViewer }, new Date()); + expect(reportInteraction).not.toHaveBeenCalled(); + }); + + it('should not track queries if no app', () => { + trackQuery({ data: [] }, { ...originalRequest, app: '' }, new Date()); + expect(reportInteraction).not.toHaveBeenCalled(); }); }); test('Tracks predefined operations', () => { trackQuery({ data: [] }, originalRequest, new Date(), { predefinedOperations: 'count_over_time' }); - expect(reportInteraction).toHaveBeenCalledWith('grafana_loki_query_executed', { - app: 'explore', + expect(reportInteraction).toHaveBeenCalledWith('grafana_explore_loki_query_executed', { bytes_processed: 0, editor_mode: 'builder', grafana_version: '1.0', @@ -119,8 +128,7 @@ test('Tracks predefined operations', () => { test('Tracks grouped queries', () => { trackGroupedQueries({ data: [] }, requests, originalRequest, new Date()); - expect(reportInteraction).toHaveBeenCalledWith('grafana_loki_query_executed', { - app: 'explore', + expect(reportInteraction).toHaveBeenCalledWith('grafana_explore_loki_query_executed', { bytes_processed: 0, editor_mode: 'builder', grafana_version: '1.0', @@ -145,8 +153,7 @@ test('Tracks grouped queries', () => { predefined_operations_applied: 'n/a', }); - expect(reportInteraction).toHaveBeenCalledWith('grafana_loki_query_executed', { - app: 'explore', + expect(reportInteraction).toHaveBeenCalledWith('grafana_explore_loki_query_executed', { bytes_processed: 0, editor_mode: 'builder', grafana_version: '1.0', diff --git a/public/app/plugins/datasource/loki/tracking.ts b/public/app/plugins/datasource/loki/tracking.ts index b48f636bc40..1b1ebfcc7cc 100644 --- a/public/app/plugins/datasource/loki/tracking.ts +++ b/public/app/plugins/datasource/loki/tracking.ts @@ -7,6 +7,7 @@ import { REF_ID_DATA_SAMPLES, REF_ID_STARTER_LOG_ROW_CONTEXT, REF_ID_STARTER_LOG_VOLUME, + REF_ID_STARTER_LOG_SAMPLE, } from './datasource'; import pluginJson from './plugin.json'; import { getNormalizedLokiQuery, isLogsQuery, obfuscate } from './queryUtils'; @@ -127,9 +128,15 @@ const isQueryWithChangedLegend = (query: LokiQuery): boolean => { }; const shouldNotReportBasedOnRefId = (refId: string): boolean => { - const starters = [REF_ID_STARTER_ANNOTATION, REF_ID_STARTER_LOG_ROW_CONTEXT, REF_ID_STARTER_LOG_VOLUME]; + const starters = [ + REF_ID_STARTER_ANNOTATION, + REF_ID_STARTER_LOG_ROW_CONTEXT, + REF_ID_STARTER_LOG_VOLUME, + REF_ID_STARTER_LOG_SAMPLE, + REF_ID_DATA_SAMPLES, + ]; - if (refId === REF_ID_DATA_SAMPLES || starters.some((starter) => refId.startsWith(starter))) { + if (starters.some((starter) => refId.startsWith(starter))) { return true; } return false; @@ -157,7 +164,7 @@ export function trackQuery( // We only want to track usage for these specific apps const { app, targets: queries } = request; - if (app === CoreApp.Dashboard || app === CoreApp.PanelViewer) { + if (app !== CoreApp.Explore) { return; } @@ -168,8 +175,7 @@ export function trackQuery( return; } - reportInteraction('grafana_loki_query_executed', { - app, + reportInteraction('grafana_explore_loki_query_executed', { grafana_version: config.buildInfo.version, editor_mode: query.editorMode, has_data: response.data.some((frame) => frame.length > 0),