Elasticsearch: add tracking events for query editor type (#117281)

* tracking event for query editor type

* prettier
This commit is contained in:
Andrew Hackmann 2026-02-03 10:24:57 -06:00 committed by GitHub
parent e34341472c
commit 24cb03c591
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 90 additions and 2 deletions

View file

@ -1,9 +1,9 @@
import { DashboardLoadedEvent } from '@grafana/data';
import { CoreApp, DataQueryRequest, DataQueryResponse, DashboardLoadedEvent } from '@grafana/data';
import { reportInteraction } from '@grafana/runtime';
import { ElasticsearchDataQuery } from './dataquery.gen';
import pluginJson from './plugin.json';
import { onDashboardLoadedHandler } from './tracking';
import { onDashboardLoadedHandler, trackQuery } from './tracking';
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
@ -61,3 +61,90 @@ describe('onDashboardLoadedHandler', () => {
expect(console.error).not.toHaveBeenCalled();
});
});
describe('trackQuery', () => {
beforeEach(() => {
jest.mocked(reportInteraction).mockClear();
});
test('tracks editor_type for code editor queries', () => {
const query: ElasticsearchDataQuery = {
refId: 'A',
editorType: 'code',
metrics: [{ id: '1', type: 'count' }],
bucketAggs: [],
};
const request: DataQueryRequest<ElasticsearchDataQuery> & { targets: ElasticsearchDataQuery[] } = {
app: CoreApp.Explore,
targets: [query],
} as DataQueryRequest<ElasticsearchDataQuery>;
const response: DataQueryResponse = {
data: [{ length: 1 }],
};
trackQuery(response, request, new Date());
expect(reportInteraction).toHaveBeenCalledWith(
'grafana_elasticsearch_query_executed',
expect.objectContaining({
editor_type: 'code',
})
);
});
test('tracks editor_type as builder for builder queries', () => {
const query: ElasticsearchDataQuery = {
refId: 'A',
editorType: 'builder',
metrics: [{ id: '1', type: 'count' }],
bucketAggs: [],
};
const request: DataQueryRequest<ElasticsearchDataQuery> & { targets: ElasticsearchDataQuery[] } = {
app: CoreApp.Explore,
targets: [query],
} as DataQueryRequest<ElasticsearchDataQuery>;
const response: DataQueryResponse = {
data: [{ length: 1 }],
};
trackQuery(response, request, new Date());
expect(reportInteraction).toHaveBeenCalledWith(
'grafana_elasticsearch_query_executed',
expect.objectContaining({
editor_type: 'builder',
})
);
});
test('defaults to builder when editor_type is not specified', () => {
const query: ElasticsearchDataQuery = {
refId: 'A',
query: 'test query',
metrics: [{ id: '1', type: 'count' }],
bucketAggs: [],
};
const request: DataQueryRequest<ElasticsearchDataQuery> & { targets: ElasticsearchDataQuery[] } = {
app: CoreApp.Explore,
targets: [query],
} as DataQueryRequest<ElasticsearchDataQuery>;
const response: DataQueryResponse = {
data: [{ length: 1 }],
};
trackQuery(response, request, new Date());
expect(reportInteraction).toHaveBeenCalledWith(
'grafana_elasticsearch_query_executed',
expect.objectContaining({
editor_type: 'builder',
})
);
});
});

View file

@ -138,6 +138,7 @@ export function trackQuery(
time_range_from: request?.range?.from?.toISOString(),
time_range_to: request?.range?.to?.toISOString(),
time_taken: Date.now() - startTime.getTime(),
editor_type: query.editorType || 'builder',
});
}
}