Skip to content

Commit

Permalink
[Uptime] Added uptime query inspector panel (#115170)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Oct 18, 2021
1 parent 411816c commit d08f091
Show file tree
Hide file tree
Showing 48 changed files with 266 additions and 91 deletions.
3 changes: 1 addition & 2 deletions x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { argv } from 'yargs';
import { Logger } from 'kibana/server';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { CollectTelemetryParams } from '../../server/lib/apm_telemetry/collect_data_telemetry';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { unwrapEsResponse } from '../../../observability/server/utils/unwrap_es_response';
import { unwrapEsResponse } from '../../../observability/common/utils/unwrap_es_response';
import { downloadTelemetryTemplate } from '../shared/download-telemetry-template';
import { mergeApmTelemetryMapping } from '../../common/apm_telemetry';
import { generateSampleDocuments } from './generate-sample-documents';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { i18n } from '@kbn/i18n';
import type { KibanaRequest } from 'kibana/server';
import type { RequestStatistics, RequestStatus } from '../../../../../src/plugins/inspector';
import { WrappedElasticsearchClientError } from '../index';
import { InspectResponse } from '../../typings/common';
import { WrappedElasticsearchClientError } from './unwrap_es_response';

/**
* Get statistics to show on inspector tab.
Expand All @@ -29,19 +29,26 @@ function getStats({
kibanaRequest: KibanaRequest;
}) {
const stats: RequestStatistics = {
kibanaApiQueryParameters: {
label: i18n.translate('xpack.observability.inspector.stats.kibanaApiQueryParametersLabel', {
defaultMessage: 'Kibana API query parameters',
}),
description: i18n.translate(
'xpack.observability.inspector.stats.kibanaApiQueryParametersDescription',
{
defaultMessage:
'The query parameters used in the Kibana API request that initiated the Elasticsearch request.',
...(kibanaRequest.query
? {
kibanaApiQueryParameters: {
label: i18n.translate(
'xpack.observability.inspector.stats.kibanaApiQueryParametersLabel',
{
defaultMessage: 'Kibana API query parameters',
}
),
description: i18n.translate(
'xpack.observability.inspector.stats.kibanaApiQueryParametersDescription',
{
defaultMessage:
'The query parameters used in the Kibana API request that initiated the Elasticsearch request.',
}
),
value: JSON.stringify(kibanaRequest.query, null, 2),
},
}
),
value: JSON.stringify(kibanaRequest.query, null, 2),
},
: {}),
kibanaApiRoute: {
label: i18n.translate('xpack.observability.inspector.stats.kibanaApiRouteLabel', {
defaultMessage: 'Kibana API route',
Expand Down Expand Up @@ -93,11 +100,17 @@ function getStats({
}

if (esResponse?.hits?.total !== undefined) {
const total = esResponse.hits.total as {
relation: string;
value: number;
};
const hitsTotalValue = total.relation === 'eq' ? `${total.value}` : `> ${total.value}`;
let hitsTotalValue;

if (typeof esResponse.hits.total === 'number') {
hitsTotalValue = esResponse.hits.total;
} else {
const total = esResponse.hits.total as {
relation: string;
value: number;
};
hitsTotalValue = total.relation === 'eq' ? `${total.value}` : `> ${total.value}`;
}

stats.hitsTotal = {
label: i18n.translate('xpack.observability.inspector.stats.hitsTotalLabel', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { useAppIndexPatternContext } from '../hooks/use_app_index_pattern';
import { ESFilter } from '../../../../../../../../src/core/types/elasticsearch';
import { PersistableFilter } from '../../../../../../lens/common';

export function useFilterValues({ field, series, baseFilters }: FilterProps, query?: string) {
export function useFilterValues(
{ field, series, baseFilters, label }: FilterProps,
query?: string
) {
const { indexPatterns } = useAppIndexPatternContext(series.dataType);

const queryFilters: ESFilter[] = [];
Expand All @@ -28,6 +31,7 @@ export function useFilterValues({ field, series, baseFilters }: FilterProps, que

return useValuesList({
query,
label,
sourceField: field,
time: series.time,
keepHistory: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function FieldValueSuggestions({
required,
allowExclusions = true,
cardinalityField,
inspector,
asCombobox = true,
onChange: onSelectionChange,
}: FieldValueSuggestionsProps) {
Expand All @@ -44,7 +45,9 @@ export function FieldValueSuggestions({
sourceField,
filters,
time,
inspector,
cardinalityField,
label,
keepHistory: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { PopoverAnchorPosition } from '@elastic/eui';
import { Dispatch, SetStateAction } from 'react';
import { ESFilter } from 'src/core/types/elasticsearch';
import { IInspectorInfo } from '../../../../../../../src/plugins/data/common';

interface CommonProps {
selectedValue?: string[];
Expand Down Expand Up @@ -37,6 +38,7 @@ export type FieldValueSuggestionsProps = CommonProps & {
onChange: (val?: string[], excludedValue?: string[]) => void;
filters: ESFilter[];
time?: { from: string; to: string };
inspector?: IInspectorInfo;
};

export type FieldValueSelectionProps = CommonProps & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const value: InspectorContextValue = {

export const InspectorContext = createContext<InspectorContextValue>(value);

export type AddInspectorRequest = (
result: FetcherResult<{
mainStatisticsData?: { _inspect?: InspectResponse };
_inspect?: InspectResponse;
}>
) => void;

export function InspectorContextProvider({ children }: { children: ReactNode }) {
const history = useHistory();
const { inspectorAdapters } = value;
Expand Down
46 changes: 40 additions & 6 deletions x-pack/plugins/observability/public/hooks/use_es_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,61 @@ import { estypes } from '@elastic/elasticsearch';
import { DataPublicPluginStart } from '../../../../../src/plugins/data/public';
import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch';
import { useKibana } from '../../../../../src/plugins/kibana_react/public';
import { isCompleteResponse } from '../../../../../src/plugins/data/common';
import { useFetcher } from './use_fetcher';
import { IInspectorInfo, isCompleteResponse } from '../../../../../src/plugins/data/common';
import { FETCH_STATUS, useFetcher } from './use_fetcher';
import { useInspectorContext } from '../context/inspector/use_inspector_context';
import { getInspectResponse } from '../../common/utils/get_inspect_response';

export const useEsSearch = <DocumentSource extends unknown, TParams extends estypes.SearchRequest>(
params: TParams,
fnDeps: any[]
fnDeps: any[],
options: { inspector?: IInspectorInfo; name: string }
) => {
const {
services: { data },
} = useKibana<{ data: DataPublicPluginStart }>();

const { name } = options ?? {};

const { addInspectorRequest } = useInspectorContext();

const { data: response = {}, loading } = useFetcher(() => {
if (params.index) {
const startTime = Date.now();
return new Promise((resolve) => {
const search$ = data.search
.search({
params,
})
.search(
{
params,
},
{}
)
.subscribe({
next: (result) => {
if (isCompleteResponse(result)) {
if (addInspectorRequest) {
addInspectorRequest({
data: {
_inspect: [
getInspectResponse({
startTime,
esRequestParams: params,
esResponse: result.rawResponse,
esError: null,
esRequestStatus: 1,
operationName: name,
kibanaRequest: {
route: {
path: '/internal/bsearch',
method: 'POST',
},
} as any,
}),
],
},
status: FETCH_STATUS.SUCCESS,
});
}
// Final result
resolve(result);
search$.unsubscribe();
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/observability/public/hooks/use_values_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { useEffect, useState } from 'react';
import useDebounce from 'react-use/lib/useDebounce';
import { ESFilter } from '../../../../../src/core/types/elasticsearch';
import { createEsParams, useEsSearch } from './use_es_search';
import { IInspectorInfo } from '../../../../../src/plugins/data/common';
import { TRANSACTION_URL } from '../components/shared/exploratory_view/configurations/constants/elasticsearch_fieldnames';

export interface Props {
sourceField: string;
label: string;
query?: string;
indexPatternTitle?: string;
filters?: ESFilter[];
time?: { from: string; to: string };
keepHistory?: boolean;
cardinalityField?: string;
inspector?: IInspectorInfo;
}

export interface ListItem {
Expand Down Expand Up @@ -60,6 +63,7 @@ export const useValuesList = ({
query = '',
filters,
time,
label,
keepHistory,
cardinalityField,
}: Props): { values: ListItem[]; loading?: boolean } => {
Expand Down Expand Up @@ -131,7 +135,8 @@ export const useValuesList = ({
},
},
}),
[debouncedQuery, from, to, JSON.stringify(filters), indexPatternTitle, sourceField]
[debouncedQuery, from, to, JSON.stringify(filters), indexPatternTitle, sourceField],
{ name: `get${label.replace(/\s/g, '')}ValuesList` }
);

useEffect(() => {
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/observability/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,8 @@ export type {
export { createObservabilityRuleTypeRegistryMock } from './rules/observability_rule_type_registry_mock';
export type { ExploratoryEmbeddableProps } from './components/shared/exploratory_view/embeddable/embeddable';

export { InspectorContextProvider } from './context/inspector/inspector_context';
export {
InspectorContextProvider,
AddInspectorRequest,
} from './context/inspector/inspector_context';
export { useInspectorContext } from './context/inspector/use_inspector_context';
7 changes: 5 additions & 2 deletions x-pack/plugins/observability/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import { PluginConfigDescriptor, PluginInitializerContext } from 'src/core/serve
import { ObservabilityPlugin, ObservabilityPluginSetup } from './plugin';
import { createOrUpdateIndex, Mappings } from './utils/create_or_update_index';
import { ScopedAnnotationsClient } from './lib/annotations/bootstrap_annotations';
import { unwrapEsResponse, WrappedElasticsearchClientError } from './utils/unwrap_es_response';
import {
unwrapEsResponse,
WrappedElasticsearchClientError,
} from '../common/utils/unwrap_es_response';
export { rangeQuery, kqlQuery } from './utils/queries';
export { getInspectResponse } from './utils/get_inspect_response';
export { getInspectResponse } from '../common/utils/get_inspect_response';

export * from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../../../common/annotations';
import { createOrUpdateIndex } from '../../utils/create_or_update_index';
import { mappings } from './mappings';
import { unwrapEsResponse } from '../../utils/unwrap_es_response';
import { unwrapEsResponse } from '../../../common/utils/unwrap_es_response';

type CreateParams = t.TypeOf<typeof createAnnotationRt>;
type DeleteParams = t.TypeOf<typeof deleteAnnotationRt>;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/uptime/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"requiredPlugins": [
"alerting",
"embeddable",
"inspector",
"features",
"licensing",
"triggersActionsUi",
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/uptime/public/apps/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
LazySyntheticsPolicyEditExtension,
} from '../components/fleet_package';
import { LazySyntheticsCustomAssetsExtension } from '../components/fleet_package/lazy_synthetics_custom_assets_extension';
import { Start as InspectorPluginStart } from '../../../../../src/plugins/inspector/public';

export interface ClientPluginsSetup {
data: DataPublicPluginSetup;
Expand All @@ -56,6 +57,7 @@ export interface ClientPluginsStart {
triggersActionsUi: TriggersAndActionsUIPublicPluginStart;
fleet?: FleetStart;
observability: ObservabilityPublicStart;
inspector: InspectorPluginStart;
}

export interface UptimePluginServices extends Partial<CoreStart> {
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/uptime/public/apps/uptime_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ActionMenu } from '../components/common/header/action_menu';
import { EuiThemeProvider } from '../../../../../src/plugins/kibana_react/common';
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { UptimeIndexPatternContextProvider } from '../contexts/uptime_index_pattern_context';
import { InspectorContextProvider } from '../../../observability/public';

export interface UptimeAppColors {
danger: string;
Expand Down Expand Up @@ -110,6 +111,7 @@ const Application = (props: UptimeAppProps) => {
...plugins,
storage,
data: startPlugins.data,
inspector: startPlugins.inspector,
triggersActionsUi: startPlugins.triggersActionsUi,
observability: startPlugins.observability,
}}
Expand All @@ -126,9 +128,11 @@ const Application = (props: UptimeAppProps) => {
className={APP_WRAPPER_CLASS}
application={core.application}
>
<UptimeAlertsFlyoutWrapper />
<PageRouter />
<ActionMenu appMountParameters={appMountParameters} />
<InspectorContextProvider>
<UptimeAlertsFlyoutWrapper />
<PageRouter />
<ActionMenu appMountParameters={appMountParameters} />
</InspectorContextProvider>
</RedirectAppLinks>
</div>
</UptimeIndexPatternContextProvider>
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/uptime/public/apps/uptime_page_template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useMemo } from 'react';
import React, { useEffect, useMemo } from 'react';
import styled from 'styled-components';
import { EuiPageHeaderProps } from '@elastic/eui';
import { CERTIFICATES_ROUTE, OVERVIEW_ROUTE } from '../../common/constants';
Expand All @@ -15,6 +15,7 @@ import { useNoDataConfig } from './use_no_data_config';
import { EmptyStateLoading } from '../components/overview/empty_state/empty_state_loading';
import { EmptyStateError } from '../components/overview/empty_state/empty_state_error';
import { useHasData } from '../components/overview/empty_state/use_has_data';
import { useInspectorContext } from '../../../observability/public';

interface Props {
path: string;
Expand All @@ -39,6 +40,11 @@ export const UptimePageTemplateComponent: React.FC<Props> = ({ path, pageHeader,
const noDataConfig = useNoDataConfig();

const { loading, error, data } = useHasData();
const { inspectorAdapters } = useInspectorContext();

useEffect(() => {
inspectorAdapters.requests.reset();
}, [inspectorAdapters.requests]);

if (error) {
return <EmptyStateError errors={[error]} />;
Expand Down
Loading

0 comments on commit d08f091

Please sign in to comment.