-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVariableEditor.tsx
More file actions
165 lines (154 loc) · 5.4 KB
/
VariableEditor.tsx
File metadata and controls
165 lines (154 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, { useCallback, useRef } from 'react';
import { AsyncSelect, Field, InlineField, TextArea } from '@grafana/ui';
import { DataQuery } from '@grafana/schema';
import { getBackendSrv } from '@grafana/runtime';
import {
CustomVariableSupport,
DataQueryRequest,
DataQueryResponse,
DataSourceApi,
QueryEditorProps,
SelectableValue,
} from '@grafana/data';
import { Observable} from 'rxjs';
import invariant from 'tiny-invariant';
import { createRequestVariables } from 'datasource';
/**
* The _Variables_ DataSource is defined separately from the UQL query
* DataSource because we use a distinct "query" (which is actually just an
* attributeKey)
*
* Calling this out explicitly just in case it results in some type mismatches
* in the future since the Grafana assumption seems to be that the editor and
* variables queries will overlap.
*/
type VariableDataSource = DataSourceApi<VariableQuery>;
interface VariableQuery extends DataQuery {
attributeKey: string;
scopeFilterExpression?: string;
}
export class VariableEditor extends CustomVariableSupport<VariableDataSource> {
constructor(readonly url: string, readonly projectName: string) {
super();
}
/**
* Variable values fetching function that is called for each dashboard
* variable.
*/
query = (request: DataQueryRequest<VariableQuery>): Observable<DataQueryResponse> => {
const { url, projectName } = this;
const { attributeKey, scopeFilterExpression } = request.targets[0];
invariant(typeof attributeKey === 'string', 'Invalid attribute key');
return new Observable((subscriber) => {
getBackendSrv()
.post(`${url}/projects/${projectName}/telemetry/attributes`, {
data: {
'attribute-types': ['values'],
'telemetry-types': ['spans', 'metrics', 'logs'],
'scope-to-attribute-keys': [attributeKey],
'oldest-time': request.range.from,
'youngest-time': request.range.to,
'template-variables': createRequestVariables(),
'scope-to-filter-expression': scopeFilterExpression
},
})
.then((res: AttributeRes) => {
subscriber.next({
data: res.data[attributeKey].map((v) => ({
text: v.value,
})),
});
})
.catch(() => {
// todo: analytics
subscriber.next({
data: [],
});
});
});
};
/**
* Component definition for the UI editor shown to users for creating the
* query that will be used to fetch variable options.
*
* For us, we currently don't have a "query" in the traditional UQL sense,
* just an attribute key that we will fetch the values for.
*/
editor = ({ onChange, query, range }: QueryEditorProps<VariableDataSource, VariableQuery>) => {
// nb we don't have a way to scope the attribute keys request so we cache
// the values for performance
const attributeKeysCache = useRef<null | string[]>(null);
// options fetching fn called on mount and on each change of the select
// input
const loadOptions = useCallback(
async (val: string) => {
if (attributeKeysCache.current === null) {
const res: AttributeRes = await getBackendSrv().post(
`${this.url}/projects/${this.projectName}/telemetry/attributes`,
{
data: {
'attribute-types': ['keys'],
'telemetry-types': ['spans', 'metrics', 'logs'],
'oldest-time': range?.from,
'youngest-time': range?.to,
},
}
);
attributeKeysCache.current = Object.keys(res.data);
}
const options: Array<SelectableValue<string>> = attributeKeysCache.current
.filter((key) => key.includes(val))
.map((key) => ({
label: key,
value: key,
}));
return options;
},
[range]
);
return (
<div>
<div className="gf-form">
<InlineField
label="Attribute key"
tooltip="Cloud Observability uses this key to populate the selectable values for the variable when viewing the dashboard. Choose from any attributes currently on your logs, metics, or traces."
>
<AsyncSelect
defaultOptions
cacheOptions
allowCustomValue={true}
defaultValue={query ? { label: query.attributeKey, value: query.attributeKey } : undefined}
loadOptions={loadOptions}
onChange={(v) => {
if (v.value) {
onChange({...query, refId: v.value, attributeKey: v.value });
}
}}
/>
</InlineField>
</div>
<div>
<Field label="UQL Filter Expression">
<TextArea
type="text"
width="100px"
rows={2}
defaultValue={query.scopeFilterExpression}
onChange={(ev) => {
if (ev.currentTarget.value) {
onChange({...query, scopeFilterExpression: ev.currentTarget.value})
}
}} />
</Field>
</div>
</div>
);
};
}
/** attributes endpoint response shape */
type AttributeRes = {
data: Record<
string,
Array<{ type: 'string' | 'int64'; value: string; telemetry_type: 'SPANS' | 'METRICS' | 'LOGS' }>
>;
};