Skip to content

Commit

Permalink
Filter alerts counts with timerage
Browse files Browse the repository at this point in the history
  • Loading branch information
kpatticha committed Jan 26, 2023
1 parent d2a6594 commit ea7c2c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ function useTabs({ selectedTab }: { selectedTab: Tab['key'] }) {
query: queryFromUrl,
} = useApmParams(`/services/{serviceName}/${selectedTab}` as const);

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

const { data: serviceAlertsCount = { alertsCount: 0 } } = useFetcher(
(callApmApi) => {
return callApmApi(
Expand All @@ -222,11 +225,15 @@ function useTabs({ selectedTab }: { selectedTab: Tab['key'] }) {
path: {
serviceName,
},
query: {
start,
end,
},
},
}
);
},
[serviceName]
[serviceName, start, end]
);

const query = omit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
AggregationsCardinalityAggregate,
AggregationsFilterAggregate,
} from '@elastic/elasticsearch/lib/api/types';
import { kqlQuery } from '@kbn/observability-plugin/server';
import {
kqlQuery,
termQuery,
rangeQuery,
} from '@kbn/observability-plugin/server';
import {
ALERT_RULE_PRODUCER,
ALERT_STATUS,
Expand Down Expand Up @@ -37,23 +41,28 @@ export async function getServicesAlerts({
maxNumServices = MAX_NUMBER_OF_SERVICES,
serviceGroup,
serviceName,
start,
end,
}: {
apmAlertsClient: ApmAlertsClient;
kuery?: string;
maxNumServices?: number;
serviceGroup?: ServiceGroup | null;
serviceName?: string;
start: number;
end: number;
}) {
const params = {
size: 0,
query: {
bool: {
filter: [
{ term: { [ALERT_RULE_PRODUCER]: 'apm' } },
{ term: { [ALERT_STATUS]: ALERT_STATUS_ACTIVE } },
...termQuery(ALERT_RULE_PRODUCER, 'apm'),
...termQuery(ALERT_STATUS, ALERT_STATUS_ACTIVE),
...rangeQuery(start, end),
...kqlQuery(kuery),
...serviceGroupQuery(serviceGroup),
...(serviceName ? [{ term: { [SERVICE_NAME]: serviceName } }] : []),
...termQuery(SERVICE_NAME, serviceName),
],
},
},
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/apm/server/routes/services/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ const serviceAlertsRoute = createApmServerRoute({
path: t.type({
serviceName: t.string,
}),
query: rangeRt,
}),
options: { tags: ['access:apm'] },
handler: async (
Expand All @@ -1229,13 +1230,17 @@ const serviceAlertsRoute = createApmServerRoute({
alertsCount: number;
}> => {
const { params } = resources;

const {
query: { start, end },
} = params;
const { serviceName } = params.path;

const apmAlertsClient = await getApmAlertsClient(resources);
const servicesAlerts = await getServicesAlerts({
serviceName,
apmAlertsClient,
start,
end,
});

return servicesAlerts.length > 0
Expand Down

0 comments on commit ea7c2c5

Please sign in to comment.