-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
datasource.ts
652 lines (557 loc) · 19.3 KB
/
datasource.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import { ComponentType } from 'react';
import { Observable } from 'rxjs';
import { makeClassES5Compatible } from '../utils/makeClassES5Compatible';
import { ScopedVars } from './ScopedVars';
import { AnnotationEvent, AnnotationQuery, AnnotationSupport } from './annotations';
import { CoreApp } from './app';
import { KeyValue, LoadingState, TableData, TimeSeries } from './data';
import { DataFrame, DataFrameDTO } from './dataFrame';
import { PanelData } from './panel';
import { GrafanaPlugin, PluginMeta } from './plugin';
import { DataQuery } from './query';
import { RawTimeRange, TimeRange } from './time';
import { CustomVariableSupport, DataSourceVariableSupport, StandardVariableSupport } from './variables';
import { DataSourceRef, WithAccessControlMetadata } from '.';
export interface DataSourcePluginOptionsEditorProps<
JSONData extends DataSourceJsonData = DataSourceJsonData,
SecureJSONData = {}
> {
options: DataSourceSettings<JSONData, SecureJSONData>;
onOptionsChange: (options: DataSourceSettings<JSONData, SecureJSONData>) => void;
}
// Utility type to extract the query type TQuery from a class extending DataSourceApi<TQuery, TOptions>
export type DataSourceQueryType<DSType> = DSType extends DataSourceApi<infer TQuery, any> ? TQuery : never;
// Utility type to extract the options type TOptions from a class extending DataSourceApi<TQuery, TOptions>
export type DataSourceOptionsType<DSType> = DSType extends DataSourceApi<any, infer TOptions> ? TOptions : never;
export class DataSourcePlugin<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataSourceQueryType<DSType>,
TOptions extends DataSourceJsonData = DataSourceOptionsType<DSType>,
TSecureOptions = {}
> extends GrafanaPlugin<DataSourcePluginMeta<TOptions>> {
components: DataSourcePluginComponents<DSType, TQuery, TOptions, TSecureOptions> = {};
constructor(public DataSourceClass: DataSourceConstructor<DSType, TQuery, TOptions>) {
super();
}
setConfigEditor(editor: ComponentType<DataSourcePluginOptionsEditorProps<TOptions, TSecureOptions>>) {
this.components.ConfigEditor = editor;
return this;
}
setConfigCtrl(ConfigCtrl: any) {
this.angularConfigCtrl = ConfigCtrl;
return this;
}
setQueryCtrl(QueryCtrl: any) {
this.components.QueryCtrl = QueryCtrl;
return this;
}
setAnnotationQueryCtrl(AnnotationsQueryCtrl: any) {
this.components.AnnotationsQueryCtrl = AnnotationsQueryCtrl;
return this;
}
setQueryEditor(QueryEditor: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>) {
this.components.QueryEditor = QueryEditor;
return this;
}
/** @deprecated Use `setQueryEditor` instead. When using Explore `props.app` is equal to `CoreApp.Explore` */
setExploreQueryField(ExploreQueryField: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>) {
this.components.ExploreQueryField = ExploreQueryField;
return this;
}
/** @deprecated Use `setQueryEditor` instead. */
setExploreMetricsQueryField(ExploreQueryField: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>) {
this.components.ExploreMetricsQueryField = ExploreQueryField;
return this;
}
/** @deprecated Use `setQueryEditor` instead. */
setExploreLogsQueryField(ExploreQueryField: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>) {
this.components.ExploreLogsQueryField = ExploreQueryField;
return this;
}
setQueryEditorHelp(QueryEditorHelp: ComponentType<QueryEditorHelpProps<TQuery>>) {
this.components.QueryEditorHelp = QueryEditorHelp;
return this;
}
/**
* @deprecated prefer using `setQueryEditorHelp`
*/
setExploreStartPage(ExploreStartPage: ComponentType<QueryEditorHelpProps<TQuery>>) {
return this.setQueryEditorHelp(ExploreStartPage);
}
/**
* @deprecated -- prefer using {@link StandardVariableSupport} or {@link CustomVariableSupport} or {@link DataSourceVariableSupport} in data source instead
*/
setVariableQueryEditor(VariableQueryEditor: any) {
this.components.VariableQueryEditor = VariableQueryEditor;
return this;
}
setMetadataInspector(MetadataInspector: ComponentType<MetadataInspectorProps<DSType, TQuery, TOptions>>) {
this.components.MetadataInspector = MetadataInspector;
return this;
}
setComponentsFromLegacyExports(pluginExports: any) {
this.angularConfigCtrl = pluginExports.ConfigCtrl;
this.components.QueryCtrl = pluginExports.QueryCtrl;
this.components.AnnotationsQueryCtrl = pluginExports.AnnotationsQueryCtrl;
this.components.ExploreQueryField = pluginExports.ExploreQueryField;
this.components.QueryEditor = pluginExports.QueryEditor;
this.components.QueryEditorHelp = pluginExports.QueryEditorHelp;
this.components.VariableQueryEditor = pluginExports.VariableQueryEditor;
}
}
export interface DataSourcePluginMeta<T extends KeyValue = {}> extends PluginMeta<T> {
builtIn?: boolean; // Is this for all
metrics?: boolean;
logs?: boolean;
annotations?: boolean;
alerting?: boolean;
tracing?: boolean;
mixed?: boolean;
hasQueryHelp?: boolean;
category?: string;
queryOptions?: PluginMetaQueryOptions;
sort?: number;
streaming?: boolean;
unlicensed?: boolean;
backend?: boolean;
isBackend?: boolean;
}
interface PluginMetaQueryOptions {
cacheTimeout?: boolean;
maxDataPoints?: boolean;
minInterval?: boolean;
}
export interface DataSourcePluginComponents<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData,
TSecureOptions = {}
> {
QueryCtrl?: any;
AnnotationsQueryCtrl?: any;
VariableQueryEditor?: any;
QueryEditor?: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>;
/** @deprecated it will be removed in a future release and `QueryEditor` will be used instead. */
ExploreQueryField?: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>;
/** @deprecated it will be removed in a future release and `QueryEditor` will be used instead. */
ExploreMetricsQueryField?: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>;
/** @deprecated it will be removed in a future release and `QueryEditor` will be used instead. */
ExploreLogsQueryField?: ComponentType<QueryEditorProps<DSType, TQuery, TOptions>>;
QueryEditorHelp?: ComponentType<QueryEditorHelpProps<TQuery>>;
ConfigEditor?: ComponentType<DataSourcePluginOptionsEditorProps<TOptions, TSecureOptions>>;
MetadataInspector?: ComponentType<MetadataInspectorProps<DSType, TQuery, TOptions>>;
}
// Only exported for tests
export interface DataSourceConstructor<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData
> {
new (instanceSettings: DataSourceInstanceSettings<TOptions>, ...args: any[]): DSType;
}
// VariableSupport is hoisted up to its own type to fix the wonky intermittent
// 'variables is references directly or indirectly' error
type VariableSupport<TQuery extends DataQuery, TOptions extends DataSourceJsonData> =
| StandardVariableSupport<DataSourceApi<TQuery, TOptions>>
| CustomVariableSupport<DataSourceApi<TQuery, TOptions>>
| DataSourceVariableSupport<DataSourceApi<TQuery, TOptions>>;
/**
* The main data source abstraction interface, represents an instance of a data source
*
* Although this is a class, datasource implementations do not *yet* need to extend it.
* As such, we can not yet add functions with default implementations.
*/
abstract class DataSourceApi<
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData,
TQueryImportConfiguration extends Record<string, object> = {}
> {
/**
* Set in constructor
*/
readonly name: string;
/**
* Set in constructor
*/
readonly id: number;
/**
* Set in constructor
*/
readonly type: string;
/**
* Set in constructor
*/
readonly uid: string;
/**
* min interval range
*/
interval?: string;
constructor(instanceSettings: DataSourceInstanceSettings<TOptions>) {
this.name = instanceSettings.name;
this.id = instanceSettings.id;
this.type = instanceSettings.type;
this.meta = instanceSettings.meta;
this.uid = instanceSettings.uid;
}
/**
* @deprecated use DataSourceWithQueryImportSupport and DataSourceWithQueryExportSupport
*/
async importQueries?(queries: DataQuery[], originDataSource: DataSourceApi<DataQuery>): Promise<TQuery[]>;
/**
* Returns configuration for importing queries from other data sources
*/
getImportQueryConfiguration?(): TQueryImportConfiguration;
/**
* Initializes a datasource after instantiation
*/
init?: () => void;
/**
* Query for data, and optionally stream results
*/
abstract query(request: DataQueryRequest<TQuery>): Promise<DataQueryResponse> | Observable<DataQueryResponse>;
/**
* Test & verify datasource settings & connection details (returning TestingStatus)
*
* When verification fails - errors specific to the data source should be handled here and converted to
* a TestingStatus object. Unknown errors and HTTP errors can be re-thrown and will be handled here:
* public/app/features/datasources/state/actions.ts
*/
abstract testDatasource(): Promise<any>;
/**
* Override to skip executing a query
*
* @returns false if the query should be skipped
*
* @virtual
*/
filterQuery?(query: TQuery): boolean;
/**
* Get hints for query improvements
*/
getQueryHints?(query: TQuery, results: any[], ...rest: any): QueryHint[];
/**
* Convert a query to a simple text string
*/
getQueryDisplayText?(query: TQuery): string;
/**
* Variable query action.
*/
metricFindQuery?(query: any, options?: any): Promise<MetricFindValue[]>;
/**
* Get tag keys for adhoc filters
*/
getTagKeys?(options?: any): Promise<MetricFindValue[]>;
/**
* Get tag values for adhoc filters
*/
getTagValues?(options: any): Promise<MetricFindValue[]>;
/**
* Set after constructor call, as the data source instance is the most common thing to pass around
* we attach the components to this instance for easy access
*/
components?: DataSourcePluginComponents<DataSourceApi<TQuery, TOptions>, TQuery, TOptions>;
/**
* static information about the datasource
*/
meta: DataSourcePluginMeta;
/**
* Used by alerting to check if query contains template variables
*/
targetContainsTemplate?(query: TQuery): boolean;
/**
* Used in explore
*/
modifyQuery?(query: TQuery, action: QueryFixAction): TQuery;
/**
* @deprecated since version 8.2.0
* Not used anymore.
*/
getHighlighterExpression?(query: TQuery): string[];
/** Get an identifier object for this datasource instance */
getRef(): DataSourceRef {
return { type: this.type, uid: this.uid };
}
/**
* Used in explore
*/
languageProvider?: any;
getVersion?(optionalOptions?: any): Promise<string>;
interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[];
/**
* An annotation processor allows explicit control for how annotations are managed.
*
* It is only necessary to configure an annotation processor if the default behavior is not desirable
*/
annotations?: AnnotationSupport<TQuery>;
/**
* Can be optionally implemented to allow datasource to be a source of annotations for dashboard.
* This function will only be called if an angular {@link AnnotationsQueryCtrl} is configured and
* the {@link annotations} is undefined
*
* @deprecated -- prefer using {@link AnnotationSupport}
*/
annotationQuery?(options: AnnotationQueryRequest<TQuery>): Promise<AnnotationEvent[]>;
/**
* Defines new variable support
* @alpha -- experimental
*/
variables?: VariableSupport<TQuery, TOptions>;
/*
* Optionally, use this method to set default values for a query
* @alpha -- experimental
*/
getDefaultQuery?(app: CoreApp): Partial<TQuery>;
}
export interface MetadataInspectorProps<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData
> {
datasource: DSType;
// All Data from this DataSource
data: DataFrame[];
}
export interface QueryEditorProps<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData,
TVQuery extends DataQuery = TQuery
> {
datasource: DSType;
query: TVQuery;
onRunQuery: () => void;
onChange: (value: TVQuery) => void;
onBlur?: () => void;
onAddQuery?: (query: TQuery) => void;
/**
* Contains query response filtered by refId of QueryResultBase and possible query error
*/
data?: PanelData;
range?: TimeRange;
exploreId?: any;
history?: Array<HistoryItem<TQuery>>;
queries?: DataQuery[];
app?: CoreApp;
}
// TODO: not really needed but used as type in some data sources and in DataQueryRequest
export enum ExploreMode {
Logs = 'Logs',
Metrics = 'Metrics',
Tracing = 'Tracing',
}
/**
* @deprecated use QueryEditorProps instead
*/
export type ExploreQueryFieldProps<
DSType extends DataSourceApi<TQuery, TOptions>,
TQuery extends DataQuery = DataQuery,
TOptions extends DataSourceJsonData = DataSourceJsonData
> = QueryEditorProps<DSType, TQuery, TOptions>;
export interface QueryEditorHelpProps<TQuery extends DataQuery = DataQuery> {
datasource: DataSourceApi<TQuery>;
query: TQuery;
onClickExample: (query: TQuery) => void;
exploreId?: any;
}
/**
* Starting in v6.2 DataFrame can represent both TimeSeries and TableData
*/
export type LegacyResponseData = TimeSeries | TableData | any;
export type DataQueryResponseData = DataFrame | DataFrameDTO | LegacyResponseData;
export interface DataQueryResponse {
/**
* The response data. When streaming, this may be empty
* or a partial result set
*/
data: DataQueryResponseData[];
/**
* When returning multiple partial responses or streams
* Use this key to inform Grafana how to combine the partial responses
* Multiple responses with same key are replaced (latest used)
*/
key?: string;
/**
* Optionally include error info along with the response data
*/
error?: DataQueryError;
/**
* Use this to control which state the response should have
* Defaults to LoadingState.Done if state is not defined
*/
state?: LoadingState;
}
export enum DataQueryErrorType {
Cancelled = 'cancelled',
Timeout = 'timeout',
Unknown = 'unknown',
}
export interface DataQueryError {
data?: {
/**
* Short information about the error
*/
message?: string;
/**
* Detailed information about the error. Only returned when app_mode is development.
*/
error?: string;
};
message?: string;
status?: number;
statusText?: string;
refId?: string;
type?: DataQueryErrorType;
}
export interface DataQueryRequest<TQuery extends DataQuery = DataQuery> {
requestId: string; // Used to identify results and optionally cancel the request in backendSrv
interval: string;
intervalMs: number;
maxDataPoints?: number;
range: TimeRange;
scopedVars: ScopedVars;
targets: TQuery[];
timezone: string;
app: CoreApp | string;
cacheTimeout?: string | null;
rangeRaw?: RawTimeRange;
timeInfo?: string; // The query time description (blue text in the upper right)
panelId?: number;
/** @deprecate */
dashboardId?: number;
dashboardUID?: string;
publicDashboardAccessToken?: string;
// Request Timing
startTime: number;
endTime?: number;
// Explore state used by various datasources
liveStreaming?: boolean;
// Make it possible to hide support queries from the inspector
hideFromInspector?: boolean;
}
export interface DataQueryTimings {
dataProcessingTime: number;
}
export interface QueryFix {
label: string;
action?: QueryFixAction;
}
export interface QueryFixAction {
type: string;
query?: string;
preventSubmit?: boolean;
options?: KeyValue<string>;
}
export interface QueryHint {
type: string;
label: string;
fix?: QueryFix;
}
export interface MetricFindValue {
text: string;
value?: string | number;
expandable?: boolean;
}
export interface DataSourceJsonData {
authType?: string;
defaultRegion?: string;
profile?: string;
manageAlerts?: boolean;
alertmanagerUid?: string;
}
/**
* Data Source instance edit model. This is returned from:
* /api/datasources
*/
export interface DataSourceSettings<T extends DataSourceJsonData = DataSourceJsonData, S = {}>
extends WithAccessControlMetadata {
id: number;
uid: string;
orgId: number;
name: string;
typeLogoUrl: string;
type: string;
typeName: string;
access: string;
url: string;
user: string;
database: string;
basicAuth: boolean;
basicAuthUser: string;
isDefault: boolean;
jsonData: T;
secureJsonData?: S;
secureJsonFields: KeyValue<boolean>;
readOnly: boolean;
withCredentials: boolean;
version?: number;
}
/**
* Frontend settings model that is passed to Datasource constructor. This differs a bit from the model above
* as this data model is available to every user who has access to a data source (Viewers+). This is loaded
* in bootData (on page load), or from: /api/frontend/settings
*/
export interface DataSourceInstanceSettings<T extends DataSourceJsonData = DataSourceJsonData> {
id: number;
uid: string;
type: string;
name: string;
meta: DataSourcePluginMeta;
readOnly: boolean;
url?: string;
jsonData: T;
username?: string;
password?: string; // when access is direct, for some legacy datasources
database?: string;
isDefault?: boolean;
access: 'direct' | 'proxy'; // Currently we support 2 options - direct (browser) and proxy (server)
/**
* This is the full Authorization header if basic auth is enabled.
* Only available here when access is Browser (direct), when access is Server (proxy)
* The basic auth header, username & password is never exposed to browser/Frontend
* so this will be empty then.
*/
basicAuth?: string;
withCredentials?: boolean;
/** When the name+uid are based on template variables, maintain access to the real values */
rawRef?: DataSourceRef;
}
/**
* @deprecated -- use {@link DataSourceInstanceSettings} instead
*/
export interface DataSourceSelectItem {
name: string;
value: string | null;
meta: DataSourcePluginMeta;
}
/**
* Options passed to the datasource.annotationQuery method. See docs/plugins/developing/datasource.md
*
* @deprecated -- use {@link AnnotationSupport}
*/
export interface AnnotationQueryRequest<MoreOptions = {}> {
range: TimeRange;
rangeRaw: RawTimeRange;
// Should be DataModel but cannot import that here from the main app. Needs to be moved to package first.
dashboard: any;
annotation: AnnotationQuery;
}
export interface HistoryItem<TQuery extends DataQuery = DataQuery> {
ts: number;
query: TQuery;
}
abstract class LanguageProvider {
abstract datasource: DataSourceApi<any, any>;
abstract request: (url: string, params?: any) => Promise<any>;
/**
* Returns startTask that resolves with a task list when main syntax is loaded.
* Task list consists of secondary promises that load more detailed language features.
*/
abstract start: () => Promise<Array<Promise<any>>>;
startTask?: Promise<any[]>;
}
//@ts-ignore
LanguageProvider = makeClassES5Compatible(LanguageProvider);
export { LanguageProvider };
//@ts-ignore
DataSourceApi = makeClassES5Compatible(DataSourceApi);
export { DataSourceApi };