2020-10-13 12:12:49 -05:00
|
|
|
import {
|
2021-03-22 13:09:15 -05:00
|
|
|
ArrayVector,
|
2021-03-05 07:28:17 -06:00
|
|
|
DataFrame,
|
|
|
|
DataQuery,
|
2020-10-13 12:12:49 -05:00
|
|
|
DataQueryRequest,
|
|
|
|
DataQueryResponse,
|
2021-03-05 07:28:17 -06:00
|
|
|
DataSourceInstanceSettings,
|
2021-03-22 13:09:15 -05:00
|
|
|
Field,
|
2020-10-13 12:12:49 -05:00
|
|
|
FieldType,
|
|
|
|
} from '@grafana/data';
|
2021-03-05 07:28:17 -06:00
|
|
|
import { DataSourceWithBackend } from '@grafana/runtime';
|
|
|
|
import { Observable } from 'rxjs';
|
2020-10-13 12:12:49 -05:00
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
|
|
|
export type TempoQuery = {
|
|
|
|
query: string;
|
|
|
|
} & DataQuery;
|
|
|
|
|
2021-03-05 07:28:17 -06:00
|
|
|
export class TempoDatasource extends DataSourceWithBackend<TempoQuery> {
|
|
|
|
constructor(instanceSettings: DataSourceInstanceSettings) {
|
2020-10-13 12:12:49 -05:00
|
|
|
super(instanceSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
query(options: DataQueryRequest<TempoQuery>): Observable<DataQueryResponse> {
|
2021-03-05 07:28:17 -06:00
|
|
|
return super.query(options).pipe(
|
|
|
|
map((response) => {
|
|
|
|
if (response.error) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2021-03-22 13:09:15 -05:00
|
|
|
// We need to parse some of the fields which contain stringified json.
|
|
|
|
// Seems like we can't just map the values as the frame we got from backend has some default processing
|
|
|
|
// and will stringify the json back when we try to set it. So we create a new field and swap it instead.
|
|
|
|
const frame: DataFrame = response.data[0];
|
|
|
|
for (const fieldName of ['serviceTags', 'logs', 'tags']) {
|
|
|
|
const field = frame.fields.find((f) => f.name === fieldName);
|
|
|
|
if (field) {
|
|
|
|
const fieldIndex = frame.fields.indexOf(field);
|
|
|
|
const values = new ArrayVector();
|
|
|
|
const newField: Field = {
|
|
|
|
...field,
|
|
|
|
values,
|
|
|
|
type: FieldType.other,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < field.values.length; i++) {
|
|
|
|
const value = field.values.get(i);
|
|
|
|
values.set(i, value === '' ? undefined : JSON.parse(value));
|
|
|
|
}
|
|
|
|
frame.fields[fieldIndex] = newField;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2021-03-05 07:28:17 -06:00
|
|
|
})
|
|
|
|
);
|
2020-10-13 12:12:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async testDatasource(): Promise<any> {
|
2021-03-05 07:28:17 -06:00
|
|
|
const response = await super.query({ targets: [{ query: '', refId: 'A' }] } as any).toPromise();
|
|
|
|
|
|
|
|
if (!response.error?.message?.startsWith('failed to get trace')) {
|
|
|
|
return { status: 'error', message: 'Data source is not working' };
|
2020-10-13 12:12:49 -05:00
|
|
|
}
|
|
|
|
|
2021-03-05 07:28:17 -06:00
|
|
|
return { status: 'success', message: 'Data source is working' };
|
2021-03-04 14:20:26 -06:00
|
|
|
}
|
|
|
|
|
2020-10-13 12:12:49 -05:00
|
|
|
getQueryDisplayText(query: TempoQuery) {
|
|
|
|
return query.query;
|
|
|
|
}
|
|
|
|
}
|