From a6a3ef74be0dfc0ae476a3bcc85bcdd46a5e5296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Fri, 1 Oct 2021 13:38:16 +0200 Subject: [PATCH] Refactor: Decouple logs from DataSourceApi (#39536) * Decouple logs from DataSourceApi * Remove unused code * Mark log context methods as deprecated * Update jsdocs deprecation message * Update packages/grafana-data/src/types/logs.ts Co-authored-by: Marcus Andersson * Check for undefined in hasLogsContextSupport * Add release tags * Change release tag * Retrigger the build * Post-merge: revert index.md changes Co-authored-by: Marcus Andersson --- packages/grafana-data/src/types/datasource.ts | 19 ++++++++++-- packages/grafana-data/src/types/logs.ts | 29 +++++++++++++++++++ public/app/features/explore/LogsContainer.tsx | 13 +++++++-- .../datasource/cloudwatch/datasource.ts | 5 +++- .../datasource/elasticsearch/datasource.ts | 5 +++- .../app/plugins/datasource/loki/datasource.ts | 3 +- 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/packages/grafana-data/src/types/datasource.ts b/packages/grafana-data/src/types/datasource.ts index d1fea53c2a4..c33328eb24e 100644 --- a/packages/grafana-data/src/types/datasource.ts +++ b/packages/grafana-data/src/types/datasource.ts @@ -251,13 +251,28 @@ abstract class DataSourceApi< getQueryDisplayText?(query: TQuery): string; /** - * Retrieve context for a given log row + * @deprecated getLogRowContext and showContextToggle in `DataSourceApi` is deprecated. + * + * DataSourceWithLogsContextSupport should be implemented instead (these methods have exactly + * the same signature in DataSourceWithLogsContextSupport). + * This method will be removed from DataSourceApi in the future. Some editors may still show + * a deprecation warning which can be ignored for time being. */ getLogRowContext?: ( row: LogRowModel, options?: TContextQueryOptions ) => Promise; + /** + * @deprecated getLogRowContext and showContextToggle in `DataSourceApi` is deprecated. + * + * DataSourceWithLogsContextSupport should be implemented instead (these methods have exactly + * the same signature in DataSourceWithLogsContextSupport). + * This method will be removed from DataSourceApi in the future. Some editors may still show + * a deprecation warning which can be ignored for time being. + */ + showContextToggle?(row?: LogRowModel): boolean; + /** * Variable query action. */ @@ -307,8 +322,6 @@ abstract class DataSourceApi< getVersion?(optionalOptions?: any): Promise; - showContextToggle?(row?: LogRowModel): boolean; - interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[]; /** diff --git a/packages/grafana-data/src/types/logs.ts b/packages/grafana-data/src/types/logs.ts index aec5de66df9..f7748a69681 100644 --- a/packages/grafana-data/src/types/logs.ts +++ b/packages/grafana-data/src/types/logs.ts @@ -2,6 +2,7 @@ import { Labels } from './data'; import { DataFrame } from './dataFrame'; import { DataQuery } from './query'; import { AbsoluteTimeRange } from './time'; +import { DataQueryResponse } from './datasource'; /** * Mapping of log level abbreviation to canonical log level. @@ -142,3 +143,31 @@ export enum LogsDedupDescription { numbers = 'De-duplication of successive lines that are identical when ignoring numbers, e.g., IP addresses, latencies.', signature = 'De-duplication of successive lines that have identical punctuation and whitespace.', } + +/** + * @alpha + */ +export interface DataSourceWithLogsContextSupport { + /** + * Retrieve context for a given log row + */ + getLogRowContext: ( + row: LogRowModel, + options?: TContextQueryOptions + ) => Promise; + + showContextToggle(row?: LogRowModel): boolean; +} + +/** + * @alpha + */ +export const hasLogsContextSupport = (datasource: any): datasource is DataSourceWithLogsContextSupport => { + if (!datasource) { + return false; + } + + const withLogsSupport = datasource as DataSourceWithLogsContextSupport; + + return withLogsSupport.getLogRowContext !== undefined && withLogsSupport.showContextToggle !== undefined; +}; diff --git a/public/app/features/explore/LogsContainer.tsx b/public/app/features/explore/LogsContainer.tsx index 2a9b35afd31..e9035b9cd6e 100644 --- a/public/app/features/explore/LogsContainer.tsx +++ b/public/app/features/explore/LogsContainer.tsx @@ -2,7 +2,14 @@ import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { css } from 'emotion'; import { Collapse } from '@grafana/ui'; -import { AbsoluteTimeRange, Field, LoadingState, LogRowModel, RawTimeRange } from '@grafana/data'; +import { + AbsoluteTimeRange, + Field, + hasLogsContextSupport, + LoadingState, + LogRowModel, + RawTimeRange, +} from '@grafana/data'; import { ExploreId, ExploreItemState } from 'app/types/explore'; import { StoreState } from 'app/types'; import { splitOpen } from './state/main'; @@ -36,7 +43,7 @@ export class LogsContainer extends PureComponent { getLogRowContext = async (row: LogRowModel, options?: any): Promise => { const { datasourceInstance } = this.props; - if (datasourceInstance?.getLogRowContext) { + if (hasLogsContextSupport(datasourceInstance)) { return datasourceInstance.getLogRowContext(row, options); } @@ -46,7 +53,7 @@ export class LogsContainer extends PureComponent { showContextToggle = (row?: LogRowModel): boolean => { const { datasourceInstance } = this.props; - if (datasourceInstance?.showContextToggle) { + if (hasLogsContextSupport(datasourceInstance)) { return datasourceInstance.showContextToggle(row); } diff --git a/public/app/plugins/datasource/cloudwatch/datasource.ts b/public/app/plugins/datasource/cloudwatch/datasource.ts index 65d988a8d51..9ccffb46800 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.ts +++ b/public/app/plugins/datasource/cloudwatch/datasource.ts @@ -11,6 +11,7 @@ import { DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings, + DataSourceWithLogsContextSupport, dateMath, LoadingState, LogRowModel, @@ -73,7 +74,9 @@ const displayCustomError = (title: string, message: string) => export const MAX_ATTEMPTS = 5; -export class CloudWatchDatasource extends DataSourceWithBackend { +export class CloudWatchDatasource + extends DataSourceWithBackend + implements DataSourceWithLogsContextSupport { proxyUrl: any; defaultRegion: any; datasourceName: string; diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index d429e6d1729..fd1cccb6360 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -11,6 +11,7 @@ import { DataQueryResponse, DataSourceApi, DataSourceInstanceSettings, + DataSourceWithLogsContextSupport, DateTime, dateTime, Field, @@ -56,7 +57,9 @@ const ELASTIC_META_FIELDS = [ '_meta', ]; -export class ElasticDatasource extends DataSourceApi { +export class ElasticDatasource + extends DataSourceApi + implements DataSourceWithLogsContextSupport { basicAuth?: string; withCredentials?: boolean; url: string; diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 48511afd580..9dec61e94ad 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -16,6 +16,7 @@ import { DataQueryResponse, DataSourceApi, DataSourceInstanceSettings, + DataSourceWithLogsContextSupport, DataSourceWithLogsVolumeSupport, dateMath, DateTime, @@ -71,7 +72,7 @@ const DEFAULT_QUERY_PARAMS: Partial = { export class LokiDatasource extends DataSourceApi - implements DataSourceWithLogsVolumeSupport { + implements DataSourceWithLogsContextSupport, DataSourceWithLogsVolumeSupport { private streams = new LiveStreams(); languageProvider: LanguageProvider; maxLines: number;