Skip to content

Commit

Permalink
[ObsUX] Update APM api to pass preferred documentType and rollupInter…
Browse files Browse the repository at this point in the history
…val (#170749)

Part of #167020

---------

Co-authored-by: achyutjhunjhunwala <[email protected]>
  • Loading branch information
MiriamAparicio and achyutjhunjhunwala authored Nov 24, 2023
1 parent e40f790 commit 97b1851
Show file tree
Hide file tree
Showing 36 changed files with 304 additions and 135 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/apm/common/data_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type AnyApmDocumentType =
| ApmDocumentType.TransactionEvent
| ApmDocumentType.ServiceDestinationMetric
| ApmDocumentType.ServiceSummaryMetric
| ApmDocumentType.ErrorEvent;
| ApmDocumentType.ErrorEvent
| ApmDocumentType.SpanEvent;

export interface ApmDataSource<
TDocumentType extends AnyApmDocumentType = AnyApmDocumentType
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/common/document_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum ApmDocumentType {
ServiceDestinationMetric = 'serviceDestinationMetric',
ServiceSummaryMetric = 'serviceSummaryMetric',
ErrorEvent = 'error',
SpanEvent = 'span',
}

export type ApmServiceTransactionDocumentType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import React from 'react';
import { ApmDocumentType } from '../../../../../common/document_type';
import {
getServiceNodeName,
SERVICE_NODE_NAME_MISSING,
Expand All @@ -33,6 +34,7 @@ import { ChartPointerEventContextProvider } from '../../../../context/chart_poin
import { useApmParams } from '../../../../hooks/use_apm_params';
import { useApmRouter } from '../../../../hooks/use_apm_router';
import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher';
import { usePreferredDataSourceAndBucketSize } from '../../../../hooks/use_preferred_data_source_and_bucket_size';
import { useServiceMetricChartsFetcher } from '../../../../hooks/use_service_metric_charts_fetcher';
import { useTimeRange } from '../../../../hooks/use_time_range';
import { truncate, unit } from '../../../../utils/style';
Expand Down Expand Up @@ -83,9 +85,17 @@ export function ServiceNodeMetrics({ serviceNodeName }: Props) {
environment,
});

const preferred = usePreferredDataSourceAndBucketSize({
start,
end,
kuery,
type: ApmDocumentType.ServiceTransactionMetric,
numBuckets: 100,
});

const { data: { host, containerId } = INITIAL_DATA, status } = useFetcher(
(callApmApi) => {
if (start && end) {
if (start && end && preferred) {
return callApmApi(
'GET /internal/apm/services/{serviceName}/node/{serviceNodeName}/metadata',
{
Expand All @@ -96,13 +106,15 @@ export function ServiceNodeMetrics({ serviceNodeName }: Props) {
start,
end,
environment,
documentType: preferred.source.documentType,
rollupInterval: preferred.source.rollupInterval,
},
},
}
);
}
},
[kuery, serviceName, serviceNodeName, start, end, environment]
[kuery, serviceName, serviceNodeName, start, end, environment, preferred]
);

const { docLinks } = useApmPluginContext().core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export function ServiceList({
} = useApmParams('/services');

const { kuery } = query;

const { fallbackToTransactions } = useFallbackToTransactionsFetcher({
kuery,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export function TopTracesOverview() {
const {
query: { environment, kuery, rangeFrom, rangeTo },
} = useApmParams('/traces');
const { start, end } = useTimeRange({ rangeFrom, rangeTo });

const { fallbackToTransactions } = useFallbackToTransactionsFetcher({
kuery,
});

const { start, end } = useTimeRange({ rangeFrom, rangeTo });

const response = useProgressiveFetcher(
(callApmApi) => {
if (start && end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getKueryWithMobileFilters } from '../../common/utils/get_kuery_with_mobile_filters';
import { useApmParams } from './use_apm_params';
import { useFetcher } from './use_fetcher';
Expand Down Expand Up @@ -33,11 +34,17 @@ export function useFallbackToTransactionsFetcher({ kuery }: { kuery: string }) {

const { data = { fallbackToTransactions: false } } = useFetcher(
(callApmApi) => {
return callApmApi('GET /internal/apm/fallback_to_transactions', {
params: {
query: { kuery: kueryWithFilters, start, end },
},
});
if (start && end) {
return callApmApi('GET /internal/apm/fallback_to_transactions', {
params: {
query: {
kuery: kueryWithFilters,
start,
end,
},
},
});
}
},
[kueryWithFilters, start, end]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { sum } from 'lodash';
import objectHash from 'object-hash';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { rangeQuery } from '@kbn/observability-plugin/server';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent';
import { getOffsetInMs } from '../../../../common/utils/get_offset_in_ms';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../common/environment_filter_values';
Expand All @@ -28,6 +27,8 @@ import {
import { getBucketSize } from '../../../../common/utils/get_bucket_size';
import { EventOutcome } from '../../../../common/event_outcome';
import { NodeType } from '../../../../common/connections';
import { ApmDocumentType } from '../../../../common/document_type';
import { RollupInterval } from '../../../../common/rollup';
import { excludeRumExitSpansQuery } from '../exclude_rum_exit_spans_query';
import { APMEventClient } from '../../helpers/create_es_client/create_apm_event_client';
import { getDocumentTypeFilterForServiceDestinationStatistics } from '../../helpers/spans/get_is_using_service_destination_metrics';
Expand Down Expand Up @@ -55,7 +56,12 @@ export const getStats = async ({

const response = await apmEventClient.search('get_connection_stats', {
apm: {
events: [ProcessorEvent.metric],
sources: [
{
documentType: ApmDocumentType.ServiceDestinationMetric,
rollupInterval: RollupInterval.OneMinute,
},
],
},
body: {
track_total_hits: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export function getRequestBase(options: {

if ('sources' in options.apm) {
options.apm.sources.forEach((source) => {
const { getQuery } = getConfigForDocumentType(source.documentType);
if (getQuery) {
filters.push(getQuery(source.rollupInterval));
const documentTypeConfig = getConfigForDocumentType(source.documentType);
if ('getQuery' in documentTypeConfig) {
filters.push(documentTypeConfig.getQuery(source.rollupInterval));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ type APMEventTermsEnumRequest = APMEventWrapper<TermsEnumRequest>;
type APMEventEqlSearchRequest = APMEventWrapper<EqlSearchRequest>;
type APMEventFieldCapsRequest = APMEventWrapper<FieldCapsRequest>;

// These keys shoul all be `ProcessorEvent.x`, but until TypeScript 4.2 we're inlining them here.
// See https://github.com/microsoft/TypeScript/issues/37888
type TypeOfProcessorEvent<T extends ProcessorEvent> = {
error: APMError;
transaction: Transaction;
span: Span;
metric: Metric;
[ProcessorEvent.error]: APMError;
[ProcessorEvent.transaction]: Transaction;
[ProcessorEvent.span]: Span;
[ProcessorEvent.metric]: Metric;
}[T];

type TypedLogEventSearchResponse<TParams extends APMLogEventESSearchRequest> =
Expand All @@ -77,15 +75,13 @@ type TypedLogEventSearchResponse<TParams extends APMLogEventESSearchRequest> =
type TypedSearchResponse<TParams extends APMEventESSearchRequest> =
InferSearchResponseOf<
TypeOfProcessorEvent<
ValuesType<
TParams['apm'] extends { events: ProcessorEvent[] }
? TParams['apm']['events']
: TParams['apm'] extends { sources: ApmDataSource[] }
? ProcessorEventOfDocumentType<
ValuesType<TParams['apm']['sources']>['documentType']
>
: never
>
TParams['apm'] extends { events: ProcessorEvent[] }
? ValuesType<TParams['apm']['events']>
: TParams['apm'] extends { sources: ApmDataSource[] }
? ProcessorEventOfDocumentType<
ValuesType<TParams['apm']['sources']>['documentType']
>
: never
>,
TParams
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { ApmDocumentType } from '../../../../common/document_type';
import {
Expand Down Expand Up @@ -33,18 +32,11 @@ function getDefaultFilter(
];
}

const documentTypeConfigMap: Record<
ApmDocumentType,
{
processorEvent: ProcessorEvent;
getQuery?: (rollupInterval: RollupInterval) => QueryDslQueryContainer;
rollupIntervals: RollupInterval[];
}
> = {
const documentTypeConfigMap = {
[ApmDocumentType.ServiceTransactionMetric]: {
processorEvent: ProcessorEvent.metric,

getQuery: (rollupInterval) => ({
getQuery: (rollupInterval: RollupInterval) => ({
bool: {
filter: getDefaultFilter('service_transaction', rollupInterval),
},
Expand All @@ -53,7 +45,7 @@ const documentTypeConfigMap: Record<
},
[ApmDocumentType.ServiceSummaryMetric]: {
processorEvent: ProcessorEvent.metric,
getQuery: (rollupInterval) => ({
getQuery: (rollupInterval: RollupInterval) => ({
bool: {
filter: getDefaultFilter('service_summary', rollupInterval),
},
Expand All @@ -62,7 +54,7 @@ const documentTypeConfigMap: Record<
},
[ApmDocumentType.TransactionMetric]: {
processorEvent: ProcessorEvent.metric,
getQuery: (rollupInterval) => ({
getQuery: (rollupInterval: RollupInterval) => ({
bool: {
filter:
rollupInterval === RollupInterval.OneMinute
Expand All @@ -79,7 +71,7 @@ const documentTypeConfigMap: Record<
[ApmDocumentType.ServiceDestinationMetric]: {
processorEvent: ProcessorEvent.metric,
rollupIntervals: defaultRollupIntervals,
getQuery: (rollupInterval) => ({
getQuery: (rollupInterval: RollupInterval) => ({
bool: {
filter:
rollupInterval === RollupInterval.OneMinute
Expand All @@ -92,7 +84,11 @@ const documentTypeConfigMap: Record<
processorEvent: ProcessorEvent.error,
rollupIntervals: [RollupInterval.None],
},
};
[ApmDocumentType.SpanEvent]: {
processorEvent: ProcessorEvent.span,
rollupIntervals: [RollupInterval.None],
},
} as const;

type DocumentTypeConfigOf<TApmDocumentType extends ApmDocumentType> =
typeof documentTypeConfigMap[TApmDocumentType];
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import { kqlQuery, rangeQuery } from '@kbn/observability-plugin/server';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { getSearchTransactionsEvents } from '.';
import { APMEventClient } from '../create_es_client/create_apm_event_client';
import { SearchAggregatedTransactionSetting } from '../../../../common/aggregated_transactions';
import { ApmDocumentType } from '../../../../common/document_type';
import { RollupInterval } from '../../../../common/rollup';
import { APMConfig } from '../../..';

export async function getIsUsingTransactionEvents({
Expand Down Expand Up @@ -63,7 +64,12 @@ async function getHasTransactions({
}) {
const response = await apmEventClient.search('get_has_transactions', {
apm: {
events: [ProcessorEvent.transaction],
sources: [
{
documentType: ApmDocumentType.TransactionEvent,
rollupInterval: RollupInterval.None,
},
],
},
body: {
track_total_hits: 1,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { getBuckets } from './get_buckets';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { ApmDocumentType } from '../../../../common/document_type';
import { RollupInterval } from '../../../../common/rollup';

describe('get buckets', () => {
let clientSpy: jest.Mock;
Expand Down Expand Up @@ -42,6 +43,11 @@ describe('get buckets', () => {

it('should limit query results to error documents', () => {
const query = clientSpy.mock.calls[0][1];
expect(query.apm.events).toEqual([ProcessorEvent.error]);
expect(query.apm.sources).toEqual([
{
documentType: ApmDocumentType.ErrorEvent,
rollupInterval: RollupInterval.None,
},
]);
});
});
Loading

0 comments on commit 97b1851

Please sign in to comment.