Skip to content

Commit

Permalink
Tracking: Add query execution tracking (#89)
Browse files Browse the repository at this point in the history
* add query tracking

* add missing imports
  • Loading branch information
svennergr authored Dec 7, 2022
1 parent 8cbff4a commit 075a702
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { from, merge, of, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { map, tap } from 'rxjs/operators';
import {
DataSourceApi,
DataSourceInstanceSettings,
Expand Down Expand Up @@ -31,6 +31,7 @@ import { bucketAggregationConfig } from './components/QueryEditor/BucketAggregat
import { isBucketAggregationWithField } from './components/QueryEditor/BucketAggregationsEditor/aggregations';
import { gte, lt, satisfies } from 'semver';
import { OpenSearchAnnotationsQueryEditor } from './components/QueryEditor/AnnotationQueryEditor';
import { trackQuery } from 'tracking';

// Those are metadata fields as defined in https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-fields.html#_identity_metadata_fields.
// custom fields can start with underscores, therefore is not safe to exclude anything that starts with one.
Expand Down Expand Up @@ -471,7 +472,16 @@ export class OpenSearchDatasource extends DataSourceApi<OpenSearchQuery, OpenSea
state: LoadingState.Done,
});
}
return merge(...subQueries);
return merge(...subQueries).pipe(
tap({
next: response => {
trackQuery(response, [...pplTargets, ...luceneTargets], options.app);
},
error: error => {
trackQuery({ error, data: [] }, [...pplTargets, ...luceneTargets], options.app);
},
})
);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DataQueryResponse } from '@grafana/data';
import { reportInteraction } from '@grafana/runtime';
import { OpenSearchQuery, QueryType } from 'types';

export function trackQuery(response: DataQueryResponse, queries: OpenSearchQuery[], app: string): void {
for (const query of queries) {
try {
reportInteraction('grafana_opensearch_query_executed', {
app,
with_lucene_query: query.queryType === QueryType.Lucene,
with_ppl_query: query.queryType === QueryType.PPL,
query_type: getQueryType(query),
has_data: response.data.some(frame => frame.datapoints.length > 0),
has_error: response.error !== undefined,
simultaneously_sent_query_count: queries.length,
alias: query.alias,
});
} catch (error) {
console.error('error while reporting opensearch query', error);
}
}
}

function getQueryType(query: OpenSearchQuery) {
if (query.isLogsQuery) {
return 'logs';
}

// PPL queries are a bit special, as they can be either raw_data or metric, depending on the format
if (query.queryType === QueryType.PPL) {
return query.format === 'table' ? 'raw_data' : 'metric';
}

const types = ['raw_data', 'raw_document'];
if (types.includes(query.metrics[0].type)) {
return query.metrics[0].type;
}
return 'metric';
}

0 comments on commit 075a702

Please sign in to comment.