diff --git a/public/app/plugins/datasource/cloud-monitoring/module.ts b/public/app/plugins/datasource/cloud-monitoring/module.ts index 42ea96502cf..72d6cbc5386 100644 --- a/public/app/plugins/datasource/cloud-monitoring/module.ts +++ b/public/app/plugins/datasource/cloud-monitoring/module.ts @@ -1,14 +1,64 @@ -import { DataSourcePlugin } from '@grafana/data'; +import { get } from 'lodash'; + +import { DataSourcePlugin, DashboardLoadedEvent } from '@grafana/data'; +import { getAppEvents } from '@grafana/runtime'; import CloudMonitoringCheatSheet from './components/CloudMonitoringCheatSheet'; import { ConfigEditor } from './components/ConfigEditor/ConfigEditor'; import { QueryEditor } from './components/QueryEditor'; import { CloudMonitoringVariableQueryEditor } from './components/VariableQueryEditor'; import CloudMonitoringDatasource from './datasource'; -import { CloudMonitoringQuery } from './types'; +import pluginJson from './plugin.json'; +import { trackCloudMonitoringDashboardLoaded } from './tracking'; +import { CloudMonitoringQuery, QueryType } from './types'; export const plugin = new DataSourcePlugin(CloudMonitoringDatasource) .setQueryEditorHelp(CloudMonitoringCheatSheet) .setQueryEditor(QueryEditor) .setConfigEditor(ConfigEditor) .setVariableQueryEditor(CloudMonitoringVariableQueryEditor); + +// Track dashboard loads to RudderStack +getAppEvents().subscribe>( + DashboardLoadedEvent, + ({ payload: { dashboardId, orgId, grafanaVersion, queries } }) => { + const cloudmonitorQueries = queries[pluginJson.id]; + let stats = { + [QueryType.TIME_SERIES_QUERY]: 0, + [QueryType.TIME_SERIES_LIST]: 0, + [QueryType.SLO]: 0, + [QueryType.ANNOTATION]: 0, + }; + cloudmonitorQueries.forEach((query) => { + if ( + query.queryType === QueryType.TIME_SERIES_QUERY || + query.queryType === QueryType.TIME_SERIES_LIST || + query.queryType === QueryType.SLO || + query.queryType === QueryType.ANNOTATION + ) { + stats[query.queryType]++; + } else if (query.queryType === 'metrics') { + if (query.hasOwnProperty('type') && get(query, 'type') === 'annotationQuery') { + stats.annotation++; + } + if (get(query, 'metricQuery.editorMode') === 'mql') { + stats.timeSeriesQuery++; + } else { + stats.timeSeriesList++; + } + } + }); + + if (cloudmonitorQueries && cloudmonitorQueries.length > 0) { + trackCloudMonitoringDashboardLoaded({ + grafana_version: grafanaVersion, + dashboard_id: dashboardId, + org_id: orgId, + mql_queries: stats[QueryType.TIME_SERIES_QUERY], + time_series_filter_queries: stats[QueryType.TIME_SERIES_LIST], + slo_queries: stats[QueryType.SLO], + annotation_queries: stats[QueryType.ANNOTATION], + }); + } + } +); diff --git a/public/app/plugins/datasource/cloud-monitoring/tracking.ts b/public/app/plugins/datasource/cloud-monitoring/tracking.ts new file mode 100644 index 00000000000..abec28f86b3 --- /dev/null +++ b/public/app/plugins/datasource/cloud-monitoring/tracking.ts @@ -0,0 +1,23 @@ +import { reportInteraction } from '@grafana/runtime'; + +/** + * Loaded the first time a dashboard containing Cloudmonitoring queries is loaded (not on every render) + * Note: The queries used here are the ones pre-migration and pre-filterQuery + */ +export const trackCloudMonitoringDashboardLoaded = (props: CloudMonitoringDashboardLoadedProps) => { + reportInteraction('grafana_ds_cloudmonitoring_dashboard_loaded', props); +}; + +export type CloudMonitoringDashboardLoadedProps = { + grafana_version?: string; + dashboard_id: string; + org_id?: number; + /** number of non hidden queries of type TimeSeriesQuery (MQL) if any */ + mql_queries: number; + /** number of non hidden queries of type TimeSeriesFilter (Builder) if any */ + time_series_filter_queries: number; + /** number of non hidden queries of type SLO if any */ + slo_queries: number; + /** number of non hidden queries of type annotation if any */ + annotation_queries: number; +};