Skip to content

Commit

Permalink
Merge branch 'main' into observability-remove-timeline-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko authored Oct 19, 2022
2 parents 590383d + 1def8b5 commit 0c877a8
Show file tree
Hide file tree
Showing 39 changed files with 2,531 additions and 425 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function getSuggestionsWithTermsAggregation({
fieldName: string;
fieldValue: string;
searchAggregatedTransactions: boolean;
serviceName: string;
serviceName?: string;
setup: Setup;
size: number;
start: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { getProcessorEventForTransactions } from '../../lib/helpers/transactions';
import { Setup } from '../../lib/helpers/setup_request';

export async function getSuggestions({
export async function getSuggestionsWithTermsEnum({
fieldName,
fieldValue,
searchAggregatedTransactions,
Expand All @@ -27,30 +27,33 @@ export async function getSuggestions({
}) {
const { apmEventClient } = setup;

const response = await apmEventClient.termsEnum('get_suggestions', {
apm: {
events: [
getProcessorEventForTransactions(searchAggregatedTransactions),
ProcessorEvent.error,
ProcessorEvent.metric,
],
},
body: {
case_insensitive: true,
field: fieldName,
size,
string: fieldValue,
index_filter: {
range: {
['@timestamp']: {
gte: start,
lte: end,
format: 'epoch_millis',
const response = await apmEventClient.termsEnum(
'get_suggestions_with_terms_enum',
{
apm: {
events: [
getProcessorEventForTransactions(searchAggregatedTransactions),
ProcessorEvent.error,
ProcessorEvent.metric,
],
},
body: {
case_insensitive: true,
field: fieldName,
size,
string: fieldValue,
index_filter: {
range: {
['@timestamp']: {
gte: start,
lte: end,
format: 'epoch_millis',
},
},
},
},
},
});
}
);

return { terms: response.terms };
}
51 changes: 29 additions & 22 deletions x-pack/plugins/apm/server/routes/suggestions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import * as t from 'io-ts';
import { maxSuggestions } from '@kbn/observability-plugin/common';
import { getSuggestions } from './get_suggestions';
import { getSuggestionsWithTermsEnum } from './get_suggestions_with_terms_enum';
import { getSuggestionsWithTermsAggregation } from './get_suggestions_with_terms_aggregation';
import { getSearchTransactionsEvents } from '../../lib/helpers/transactions';
import { setupRequest } from '../../lib/helpers/setup_request';
Expand Down Expand Up @@ -41,28 +41,35 @@ const suggestionsRoute = createApmServerRoute({
maxSuggestions
);

const suggestions = serviceName
? await getSuggestionsWithTermsAggregation({
fieldName,
fieldValue,
searchAggregatedTransactions,
serviceName,
setup,
size,
start,
end,
})
: await getSuggestions({
fieldName,
fieldValue,
searchAggregatedTransactions,
setup,
size,
start,
end,
});
if (!serviceName) {
const suggestions = await getSuggestionsWithTermsEnum({
fieldName,
fieldValue,
searchAggregatedTransactions,
setup,
size,
start,
end,
});

return suggestions;
// if no terms are found using terms enum it will fall back to using ordinary terms agg search
// This is useful because terms enum can only find terms that start with the search query
// whereas terms agg approach can find terms that contain the search query
if (suggestions.terms.length > 0) {
return suggestions;
}
}

return getSuggestionsWithTermsAggregation({
fieldName,
fieldValue,
searchAggregatedTransactions,
serviceName,
setup,
size,
start,
end,
});
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import type {
FleetServerAgent,
FleetServerAgentComponentStatus,
} from '@kbn/fleet-plugin/common';
import { AGENTS_INDEX, FleetServerAgentComponentStatuses } from '@kbn/fleet-plugin/common';
import { FleetServerAgentComponentStatuses, AGENTS_INDEX } from '@kbn/fleet-plugin/common';
import moment from 'moment';
import { BaseDataGenerator } from './base_data_generator';

// List of computed (as in, done in code is kibana via
// https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/common/services/agent_status.ts#L13-L44
const agentStatusList: readonly AgentStatus[] = [
'offline',
'error',
Expand All @@ -29,6 +32,13 @@ const agentStatusList: readonly AgentStatus[] = [
'degraded',
];

const lastCheckinStatusList: ReadonlyArray<FleetServerAgent['last_checkin_status']> = [
'error',
'online',
'degraded',
'updating',
];

export class FleetAgentGenerator extends BaseDataGenerator<Agent> {
/**
* @param [overrides] any partial value to the full Agent record
Expand Down Expand Up @@ -138,8 +148,6 @@ export class FleetAgentGenerator extends BaseDataGenerator<Agent> {
policy_id: this.randomUUID(),
type: 'PERMANENT',
default_api_key: 'so3dWnkBj1tiuAw9yAm3:t7jNlnPnR6azEI_YpXuBXQ',
// policy_output_permissions_hash:
// '81b3d070dddec145fafcbdfb6f22888495a12edc31881f6b0511fa10de66daa7',
default_api_key_id: 'so3dWnkBj1tiuAw9yAm3',
updated_at: now,
last_checkin: now,
Expand Down Expand Up @@ -171,13 +179,76 @@ export class FleetAgentGenerator extends BaseDataGenerator<Agent> {
],
},
],
last_checkin_status: this.randomChoice(lastCheckinStatusList),
upgraded_at: null,
upgrade_started_at: null,
unenrolled_at: undefined,
unenrollment_started_at: undefined,
},
},
overrides
);
}

private randomAgentStatus() {
generateEsHitWithStatus(
status: AgentStatus,
overrides: DeepPartial<estypes.SearchHit<FleetServerAgent>> = {}
) {
const esHit = this.generateEsHit(overrides);

// Basically: reverse engineer the Fleet `getAgentStatus()` utility:
// https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/common/services/agent_status.ts#L13-L44

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const fleetServerAgent = esHit._source!;

// Reset the `last_checkin_status since we're controlling the agent status here
fleetServerAgent.last_checkin_status = 'online';

switch (status) {
case 'degraded':
fleetServerAgent.last_checkin_status = 'degraded';
break;

case 'enrolling':
fleetServerAgent.last_checkin = undefined;

break;
case 'error':
fleetServerAgent.last_checkin_status = 'error';
break;

case 'inactive':
fleetServerAgent.active = false;
break;

case 'offline':
// current fleet timeout interface for offline is 5 minutes
// https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/common/services/agent_status.ts#L11
fleetServerAgent.last_checkin = moment().subtract(6, 'minutes').toISOString();
break;

case 'unenrolling':
fleetServerAgent.unenrollment_started_at = fleetServerAgent.updated_at;
fleetServerAgent.unenrolled_at = undefined;
break;

case 'updating':
fleetServerAgent.upgrade_started_at = fleetServerAgent.updated_at;
fleetServerAgent.upgraded_at = undefined;
break;

case 'warning':
// NOt able to find anything in fleet
break;

// default is `online`, which is also the default returned by `generateEsHit()`
}

return esHit;
}

public randomAgentStatus() {
return this.randomChoice(agentStatusList);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0c877a8

Please sign in to comment.