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 <marcus.andersson@grafana.com>

* 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 <marcus.andersson@grafana.com>
This commit is contained in:
Piotr Jamróz 2021-10-01 13:38:16 +02:00 committed by GitHub
parent 5120765b0c
commit a6a3ef74be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 9 deletions

View File

@ -251,13 +251,28 @@ abstract class DataSourceApi<
getQueryDisplayText?(query: TQuery): string; 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?: <TContextQueryOptions extends {}>( getLogRowContext?: <TContextQueryOptions extends {}>(
row: LogRowModel, row: LogRowModel,
options?: TContextQueryOptions options?: TContextQueryOptions
) => Promise<DataQueryResponse>; ) => Promise<DataQueryResponse>;
/**
* @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. * Variable query action.
*/ */
@ -307,8 +322,6 @@ abstract class DataSourceApi<
getVersion?(optionalOptions?: any): Promise<string>; getVersion?(optionalOptions?: any): Promise<string>;
showContextToggle?(row?: LogRowModel): boolean;
interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[]; interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[];
/** /**

View File

@ -2,6 +2,7 @@ import { Labels } from './data';
import { DataFrame } from './dataFrame'; import { DataFrame } from './dataFrame';
import { DataQuery } from './query'; import { DataQuery } from './query';
import { AbsoluteTimeRange } from './time'; import { AbsoluteTimeRange } from './time';
import { DataQueryResponse } from './datasource';
/** /**
* Mapping of log level abbreviation to canonical log level. * 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.', 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.', signature = 'De-duplication of successive lines that have identical punctuation and whitespace.',
} }
/**
* @alpha
*/
export interface DataSourceWithLogsContextSupport {
/**
* Retrieve context for a given log row
*/
getLogRowContext: <TContextQueryOptions extends {}>(
row: LogRowModel,
options?: TContextQueryOptions
) => Promise<DataQueryResponse>;
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;
};

View File

@ -2,7 +2,14 @@ import React, { PureComponent } from 'react';
import { connect, ConnectedProps } from 'react-redux'; import { connect, ConnectedProps } from 'react-redux';
import { css } from 'emotion'; import { css } from 'emotion';
import { Collapse } from '@grafana/ui'; 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 { ExploreId, ExploreItemState } from 'app/types/explore';
import { StoreState } from 'app/types'; import { StoreState } from 'app/types';
import { splitOpen } from './state/main'; import { splitOpen } from './state/main';
@ -36,7 +43,7 @@ export class LogsContainer extends PureComponent<LogsContainerProps> {
getLogRowContext = async (row: LogRowModel, options?: any): Promise<any> => { getLogRowContext = async (row: LogRowModel, options?: any): Promise<any> => {
const { datasourceInstance } = this.props; const { datasourceInstance } = this.props;
if (datasourceInstance?.getLogRowContext) { if (hasLogsContextSupport(datasourceInstance)) {
return datasourceInstance.getLogRowContext(row, options); return datasourceInstance.getLogRowContext(row, options);
} }
@ -46,7 +53,7 @@ export class LogsContainer extends PureComponent<LogsContainerProps> {
showContextToggle = (row?: LogRowModel): boolean => { showContextToggle = (row?: LogRowModel): boolean => {
const { datasourceInstance } = this.props; const { datasourceInstance } = this.props;
if (datasourceInstance?.showContextToggle) { if (hasLogsContextSupport(datasourceInstance)) {
return datasourceInstance.showContextToggle(row); return datasourceInstance.showContextToggle(row);
} }

View File

@ -11,6 +11,7 @@ import {
DataQueryRequest, DataQueryRequest,
DataQueryResponse, DataQueryResponse,
DataSourceInstanceSettings, DataSourceInstanceSettings,
DataSourceWithLogsContextSupport,
dateMath, dateMath,
LoadingState, LoadingState,
LogRowModel, LogRowModel,
@ -73,7 +74,9 @@ const displayCustomError = (title: string, message: string) =>
export const MAX_ATTEMPTS = 5; export const MAX_ATTEMPTS = 5;
export class CloudWatchDatasource extends DataSourceWithBackend<CloudWatchQuery, CloudWatchJsonData> { export class CloudWatchDatasource
extends DataSourceWithBackend<CloudWatchQuery, CloudWatchJsonData>
implements DataSourceWithLogsContextSupport {
proxyUrl: any; proxyUrl: any;
defaultRegion: any; defaultRegion: any;
datasourceName: string; datasourceName: string;

View File

@ -11,6 +11,7 @@ import {
DataQueryResponse, DataQueryResponse,
DataSourceApi, DataSourceApi,
DataSourceInstanceSettings, DataSourceInstanceSettings,
DataSourceWithLogsContextSupport,
DateTime, DateTime,
dateTime, dateTime,
Field, Field,
@ -56,7 +57,9 @@ const ELASTIC_META_FIELDS = [
'_meta', '_meta',
]; ];
export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, ElasticsearchOptions> { export class ElasticDatasource
extends DataSourceApi<ElasticsearchQuery, ElasticsearchOptions>
implements DataSourceWithLogsContextSupport {
basicAuth?: string; basicAuth?: string;
withCredentials?: boolean; withCredentials?: boolean;
url: string; url: string;

View File

@ -16,6 +16,7 @@ import {
DataQueryResponse, DataQueryResponse,
DataSourceApi, DataSourceApi,
DataSourceInstanceSettings, DataSourceInstanceSettings,
DataSourceWithLogsContextSupport,
DataSourceWithLogsVolumeSupport, DataSourceWithLogsVolumeSupport,
dateMath, dateMath,
DateTime, DateTime,
@ -71,7 +72,7 @@ const DEFAULT_QUERY_PARAMS: Partial<LokiRangeQueryRequest> = {
export class LokiDatasource export class LokiDatasource
extends DataSourceApi<LokiQuery, LokiOptions> extends DataSourceApi<LokiQuery, LokiOptions>
implements DataSourceWithLogsVolumeSupport<LokiQuery> { implements DataSourceWithLogsContextSupport, DataSourceWithLogsVolumeSupport<LokiQuery> {
private streams = new LiveStreams(); private streams = new LiveStreams();
languageProvider: LanguageProvider; languageProvider: LanguageProvider;
maxLines: number; maxLines: number;