Skip to content

Commit

Permalink
[APM] Add time range to event metadata API (#167132)
Browse files Browse the repository at this point in the history
Closes #166424
  • Loading branch information
sorenlouv authored Oct 2, 2023
1 parent 97d26c1 commit 7a113e2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function TimelineTabContent({
}

function MetadataTabContent({ transaction }: { transaction: Transaction }) {
return <TransactionMetadata transactionId={transaction.transaction.id} />;
return <TransactionMetadata transaction={transaction} />;
}

function LogsTabContent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function SpanFlyoutBody({
content: (
<Fragment>
<EuiSpacer size="m" />
<SpanMetadata spanId={span.span.id} />
<SpanMetadata span={span} />
</Fragment>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function TransactionFlyoutBody({
content: (
<>
<EuiSpacer size="m" />
<TransactionMetadata transactionId={transaction.transaction.id} />
<TransactionMetadata transaction={transaction} />
</>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ export function ErrorMetadata({ error }: Props) {
processorEvent: ProcessorEvent.error,
id: error.error.id,
},
query: {
start: error['@timestamp'],
end: error['@timestamp'],
},
},
}
);
},
[error.error.id]
[error]
);

const sections = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

import React, { useMemo } from 'react';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { Span } from '../../../../../typings/es_schemas/ui/span';
import { getSectionsFromFields } from '../helper';
import { MetadataTable } from '..';
import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher';

interface Props {
spanId: string;
span: Span;
}

export function SpanMetadata({ spanId }: Props) {
export function SpanMetadata({ span }: Props) {
const { data: spanEvent, status } = useFetcher(
(callApmApi) => {
return callApmApi(
Expand All @@ -24,13 +25,17 @@ export function SpanMetadata({ spanId }: Props) {
params: {
path: {
processorEvent: ProcessorEvent.span,
id: spanId,
id: span.span.id,
},
query: {
start: span['@timestamp'],
end: span['@timestamp'],
},
},
}
);
},
[spanId]
[span]
);

const sections = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

import React, { useMemo } from 'react';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { Transaction } from '../../../../../typings/es_schemas/ui/transaction';
import { getSectionsFromFields } from '../helper';
import { MetadataTable } from '..';
import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher';

interface Props {
transactionId: string;
transaction: Transaction;
}

export function TransactionMetadata({ transactionId }: Props) {
export function TransactionMetadata({ transaction }: Props) {
const { data: transactionEvent, status } = useFetcher(
(callApmApi) => {
return callApmApi(
Expand All @@ -24,13 +25,17 @@ export function TransactionMetadata({ transactionId }: Props) {
params: {
path: {
processorEvent: ProcessorEvent.transaction,
id: transactionId,
id: transaction.transaction.id,
},
query: {
start: transaction['@timestamp'],
end: transaction['@timestamp'],
},
},
}
);
},
[transactionId]
[transaction]
);

const sections = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { rangeQuery } from '@kbn/observability-plugin/server';
import {
ERROR_ID,
SPAN_ID,
Expand All @@ -18,43 +18,26 @@ export async function getEventMetadata({
apmEventClient,
processorEvent,
id,
start,
end,
}: {
apmEventClient: APMEventClient;
processorEvent: ProcessorEvent;
id: string;
start: number;
end: number;
}) {
const filter: QueryDslQueryContainer[] = [];

switch (processorEvent) {
case ProcessorEvent.error:
filter.push({
term: { [ERROR_ID]: id },
});
break;

case ProcessorEvent.transaction:
filter.push({
term: {
[TRANSACTION_ID]: id,
},
});
break;

case ProcessorEvent.span:
filter.push({
term: { [SPAN_ID]: id },
});
break;
}

const fieldName = getFieldName(processorEvent);
const response = await apmEventClient.search('get_event_metadata', {
apm: {
events: [processorEvent],
},
body: {
track_total_hits: false,
query: {
bool: { filter },
bool: {
filter: [...rangeQuery(start, end), { term: { [fieldName]: id } }],
},
},
size: 1,
_source: false,
Expand All @@ -65,3 +48,19 @@ export async function getEventMetadata({

return response.hits.hits[0].fields;
}

function getFieldName(processorEvent: ProcessorEvent) {
switch (processorEvent) {
case ProcessorEvent.error:
return ERROR_ID;

case ProcessorEvent.transaction:
return TRANSACTION_ID;

case ProcessorEvent.span:
return SPAN_ID;

default:
throw new Error('Unknown processor event');
}
}
11 changes: 7 additions & 4 deletions x-pack/plugins/apm/server/routes/event_metadata/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createApmServerRoute } from '../apm_routes/create_apm_server_route';
import { getEventMetadata } from './get_event_metadata';
import { processorEventRt } from '../../../common/processor_event';
import { getApmEventClient } from '../../lib/helpers/get_apm_event_client';
import { rangeRt } from '../default_api_types';

const eventMetadataRoute = createApmServerRoute({
endpoint: 'GET /internal/apm/event_metadata/{processorEvent}/{id}',
Expand All @@ -19,20 +20,22 @@ const eventMetadataRoute = createApmServerRoute({
processorEvent: processorEventRt,
id: t.string,
}),
query: rangeRt,
}),
handler: async (
resources
): Promise<{ metadata: Partial<Record<string, unknown[]>> }> => {
const apmEventClient = await getApmEventClient(resources);

const {
path: { processorEvent, id },
} = resources.params;
const { params } = resources;
const { start, end } = params.query;
const { processorEvent, id } = params.path;

const metadata = await getEventMetadata({
apmEventClient,
processorEvent,
id,
start,
end,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import expect from '@kbn/expect';
import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { PROCESSOR_EVENT } from '@kbn/apm-plugin/common/es_fields/apm';
import { SpanRaw } from '@kbn/apm-plugin/typings/es_schemas/raw/span_raw';
import { ErrorRaw } from '@kbn/apm-plugin/typings/es_schemas/raw/error_raw';
import { TransactionRaw } from '@kbn/apm-plugin/typings/es_schemas/raw/transaction_raw';
import { FtrProviderContext } from '../../common/ftr_provider_context';

export default function ApiTest({ getService }: FtrProviderContext) {
const registry = getService('registry');
const apmApiClient = getService('apmApiClient');
const es = getService('es');

async function getLastDocId(processorEvent: ProcessorEvent) {
const response = await es.search<{
[key: string]: { id: string };
}>({
async function getMostRecentDoc(processorEvent: ProcessorEvent) {
const response = await es.search<TransactionRaw | SpanRaw | ErrorRaw>({
index: ['apm-*'],
body: {
query: {
Expand All @@ -32,12 +33,18 @@ export default function ApiTest({ getService }: FtrProviderContext) {
},
});

return response.hits.hits[0]._source![processorEvent].id;
const doc = response.hits.hits[0]._source!;

return {
// @ts-expect-error
id: doc[processorEvent].id as string,
timestamp: doc['@timestamp'],
};
}

registry.when('Event metadata', { config: 'basic', archives: ['apm_8.0.0'] }, () => {
it('fetches transaction event metadata', async () => {
const id = await getLastDocId(ProcessorEvent.transaction);
const { id, timestamp } = await getMostRecentDoc(ProcessorEvent.transaction);

const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/event_metadata/{processorEvent}/{id}',
Expand All @@ -46,6 +53,10 @@ export default function ApiTest({ getService }: FtrProviderContext) {
processorEvent: ProcessorEvent.transaction,
id,
},
query: {
start: timestamp,
end: timestamp,
},
},
});

Expand All @@ -67,7 +78,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

it('fetches error event metadata', async () => {
const id = await getLastDocId(ProcessorEvent.error);
const { id, timestamp } = await getMostRecentDoc(ProcessorEvent.error);

const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/event_metadata/{processorEvent}/{id}',
Expand All @@ -76,6 +87,10 @@ export default function ApiTest({ getService }: FtrProviderContext) {
processorEvent: ProcessorEvent.error,
id,
},
query: {
start: timestamp,
end: timestamp,
},
},
});

Expand All @@ -97,7 +112,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

it('fetches span event metadata', async () => {
const id = await getLastDocId(ProcessorEvent.span);
const { id, timestamp } = await getMostRecentDoc(ProcessorEvent.span);

const { body } = await apmApiClient.readUser({
endpoint: 'GET /internal/apm/event_metadata/{processorEvent}/{id}',
Expand All @@ -106,6 +121,10 @@ export default function ApiTest({ getService }: FtrProviderContext) {
processorEvent: ProcessorEvent.span,
id,
},
query: {
start: timestamp,
end: timestamp,
},
},
});

Expand Down

0 comments on commit 7a113e2

Please sign in to comment.