mirror of https://github.com/grafana/grafana.git
				
				
				
			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:
		
							parent
							
								
									5120765b0c
								
							
						
					
					
						commit
						a6a3ef74be
					
				| 
						 | 
				
			
			@ -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?: <TContextQueryOptions extends {}>(
 | 
			
		||||
    row: LogRowModel,
 | 
			
		||||
    options?: TContextQueryOptions
 | 
			
		||||
  ) => 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.
 | 
			
		||||
   */
 | 
			
		||||
| 
						 | 
				
			
			@ -307,8 +322,6 @@ abstract class DataSourceApi<
 | 
			
		|||
 | 
			
		||||
  getVersion?(optionalOptions?: any): Promise<string>;
 | 
			
		||||
 | 
			
		||||
  showContextToggle?(row?: LogRowModel): boolean;
 | 
			
		||||
 | 
			
		||||
  interpolateVariablesInQueries?(queries: TQuery[], scopedVars: ScopedVars | {}): TQuery[];
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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: <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;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<LogsContainerProps> {
 | 
			
		|||
  getLogRowContext = async (row: LogRowModel, options?: any): Promise<any> => {
 | 
			
		||||
    const { datasourceInstance } = this.props;
 | 
			
		||||
 | 
			
		||||
    if (datasourceInstance?.getLogRowContext) {
 | 
			
		||||
    if (hasLogsContextSupport(datasourceInstance)) {
 | 
			
		||||
      return datasourceInstance.getLogRowContext(row, options);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +53,7 @@ export class LogsContainer extends PureComponent<LogsContainerProps> {
 | 
			
		|||
  showContextToggle = (row?: LogRowModel): boolean => {
 | 
			
		||||
    const { datasourceInstance } = this.props;
 | 
			
		||||
 | 
			
		||||
    if (datasourceInstance?.showContextToggle) {
 | 
			
		||||
    if (hasLogsContextSupport(datasourceInstance)) {
 | 
			
		||||
      return datasourceInstance.showContextToggle(row);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<CloudWatchQuery, CloudWatchJsonData> {
 | 
			
		||||
export class CloudWatchDatasource
 | 
			
		||||
  extends DataSourceWithBackend<CloudWatchQuery, CloudWatchJsonData>
 | 
			
		||||
  implements DataSourceWithLogsContextSupport {
 | 
			
		||||
  proxyUrl: any;
 | 
			
		||||
  defaultRegion: any;
 | 
			
		||||
  datasourceName: string;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<ElasticsearchQuery, ElasticsearchOptions> {
 | 
			
		||||
export class ElasticDatasource
 | 
			
		||||
  extends DataSourceApi<ElasticsearchQuery, ElasticsearchOptions>
 | 
			
		||||
  implements DataSourceWithLogsContextSupport {
 | 
			
		||||
  basicAuth?: string;
 | 
			
		||||
  withCredentials?: boolean;
 | 
			
		||||
  url: string;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,7 @@ import {
 | 
			
		|||
  DataQueryResponse,
 | 
			
		||||
  DataSourceApi,
 | 
			
		||||
  DataSourceInstanceSettings,
 | 
			
		||||
  DataSourceWithLogsContextSupport,
 | 
			
		||||
  DataSourceWithLogsVolumeSupport,
 | 
			
		||||
  dateMath,
 | 
			
		||||
  DateTime,
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +72,7 @@ const DEFAULT_QUERY_PARAMS: Partial<LokiRangeQueryRequest> = {
 | 
			
		|||
 | 
			
		||||
export class LokiDatasource
 | 
			
		||||
  extends DataSourceApi<LokiQuery, LokiOptions>
 | 
			
		||||
  implements DataSourceWithLogsVolumeSupport<LokiQuery> {
 | 
			
		||||
  implements DataSourceWithLogsContextSupport, DataSourceWithLogsVolumeSupport<LokiQuery> {
 | 
			
		||||
  private streams = new LiveStreams();
 | 
			
		||||
  languageProvider: LanguageProvider;
 | 
			
		||||
  maxLines: number;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue