Skip to content

Commit

Permalink
Migrate APM alerts test
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Nov 8, 2024
1 parent e1bb126 commit 670ee82
Show file tree
Hide file tree
Showing 26 changed files with 1,947 additions and 1,587 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/
import datemath from '@elastic/datemath';
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { rangeQuery, ScopedAnnotationsClient } from '@kbn/observability-plugin/server';
import { rangeQuery, ScopedAnnotationsClient, termsQuery } from '@kbn/observability-plugin/server';
import {
ALERT_RULE_PRODUCER,
ALERT_STATUS,
ALERT_STATUS_ACTIVE,
} from '@kbn/rule-registry-plugin/common/technical_rule_data_field_names';
import * as t from 'io-ts';
import { observabilityFeatureId } from '@kbn/observability-shared-plugin/common';
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values';
import { Environment } from '../../../../common/environment_rt';
import { SERVICE_NAME } from '../../../../common/es_fields/apm';
Expand Down Expand Up @@ -139,7 +140,7 @@ export async function getApmServiceSummary({
query: {
bool: {
filter: [
...termQuery(ALERT_RULE_PRODUCER, 'apm'),
...termsQuery(ALERT_RULE_PRODUCER, 'apm', observabilityFeatureId),
...termQuery(ALERT_STATUS, ALERT_STATUS_ACTIVE),
...rangeQuery(start, end),
...termQuery(SERVICE_NAME, serviceName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

import { kqlQuery } from '@kbn/observability-plugin/server';
import { ALERT_RULE_PRODUCER, ALERT_STATUS } from '@kbn/rule-data-utils';
import { kqlQuery, termQuery, termsQuery } from '@kbn/observability-plugin/server';
import { ALERT_RULE_PRODUCER, ALERT_STATUS, ALERT_STATUS_ACTIVE } from '@kbn/rule-data-utils';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { Logger } from '@kbn/core/server';
import { observabilityFeatureId } from '@kbn/observability-shared-plugin/common';
import { ApmPluginRequestHandlerContext } from '../typings';
import { SavedServiceGroup } from '../../../common/service_groups';
import { ApmAlertsClient } from '../../lib/helpers/get_apm_alerts_client';
Expand Down Expand Up @@ -42,8 +43,8 @@ export async function getServiceGroupAlerts({
query: {
bool: {
filter: [
{ term: { [ALERT_RULE_PRODUCER]: 'apm' } },
{ term: { [ALERT_STATUS]: 'active' } },
...termsQuery(ALERT_RULE_PRODUCER, 'apm', observabilityFeatureId),
...termQuery(ALERT_STATUS, ALERT_STATUS_ACTIVE),
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* 2.0.
*/

import { kqlQuery, termQuery, rangeQuery, wildcardQuery } from '@kbn/observability-plugin/server';
import {
kqlQuery,
termQuery,
rangeQuery,
wildcardQuery,
termsQuery,
} from '@kbn/observability-plugin/server';
import {
ALERT_RULE_PRODUCER,
ALERT_STATUS,
ALERT_STATUS_ACTIVE,
ALERT_RULE_PARAMETERS,
} from '@kbn/rule-data-utils';
import { observabilityFeatureId } from '@kbn/observability-shared-plugin/common';
import { SERVICE_NAME, TRANSACTION_NAME, TRANSACTION_TYPE } from '../../../common/es_fields/apm';
import { LatencyAggregationType } from '../../../common/latency_aggregation_types';
import { AggregationType } from '../../../common/rules/apm_rule_types';
Expand Down Expand Up @@ -59,7 +66,7 @@ export async function getServiceTransactionGroupsAlerts({
query: {
bool: {
filter: [
...termQuery(ALERT_RULE_PRODUCER, 'apm'),
...termsQuery(ALERT_RULE_PRODUCER, 'apm', observabilityFeatureId),
...termQuery(ALERT_STATUS, ALERT_STATUS_ACTIVE),
...rangeQuery(start, end),
...kqlQuery(kuery),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* 2.0.
*/

import { kqlQuery, termQuery, rangeQuery, wildcardQuery } from '@kbn/observability-plugin/server';
import {
kqlQuery,
termQuery,
rangeQuery,
wildcardQuery,
termsQuery,
} from '@kbn/observability-plugin/server';
import {
ALERT_RULE_PRODUCER,
ALERT_STATUS,
ALERT_STATUS_ACTIVE,
ALERT_UUID,
} from '@kbn/rule-data-utils';
import { observabilityFeatureId } from '@kbn/observability-shared-plugin/common';
import { SERVICE_NAME } from '../../../../common/es_fields/apm';
import { ServiceGroup } from '../../../../common/service_groups';
import { ApmAlertsClient } from '../../../lib/helpers/get_apm_alerts_client';
Expand Down Expand Up @@ -51,7 +58,7 @@ export async function getServicesAlerts({
query: {
bool: {
filter: [
...termQuery(ALERT_RULE_PRODUCER, 'apm'),
...termsQuery(ALERT_RULE_PRODUCER, 'apm', observabilityFeatureId),
...termQuery(ALERT_STATUS, ALERT_STATUS_ACTIVE),
...rangeQuery(start, end),
...kqlQuery(kuery),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { errorCountActionVariables } from '@kbn/apm-plugin/server/routes/alerts/
import { apm, timerange } from '@kbn/apm-synthtrace-client';
import expect from '@kbn/expect';
import { omit } from 'lodash';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';
import type { SupertestWithRoleScopeType } from '../../../../services';
import type { DeploymentAgnosticFtrProviderContext } from '../../../../ftr_provider_context';
import {
createApmRule,
fetchServiceInventoryAlertCounts,
Expand All @@ -24,15 +26,17 @@ import { waitForAlertsForRule } from './helpers/wait_for_alerts_for_rule';
import { waitForIndexConnectorResults } from './helpers/wait_for_index_connector_results';
import { waitForActiveRule } from './helpers/wait_for_active_rule';

export default function ApiTest({ getService }: FtrProviderContext) {
const registry = getService('registry');
const supertest = getService('supertest');
export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
const roleScopedSupertest = getService('roleScopedSupertest');
const es = getService('es');
const logger = getService('log');
const apmApiClient = getService('apmApiClient');
const apmSynthtraceEsClient = getService('apmSynthtraceEsClient');
const apmApiClient = getService('apmApi');
const synthtrace = getService('synthtrace');

describe('error count threshold alert', () => {
let apmSynthtraceEsClient: ApmSynthtraceEsClient;
let supertest: SupertestWithRoleScopeType;

registry.when('error count threshold alert', { config: 'basic', archives: [] }, () => {
const javaErrorMessage = 'a java error';
const phpErrorMessage = 'a php error';

Expand All @@ -50,7 +54,11 @@ export default function ApiTest({ getService }: FtrProviderContext) {
],
};

before(() => {
before(async () => {
supertest = await roleScopedSupertest.getSupertestWithRoleScope('admin', {
withInternalHeaders: true,
});

const opbeansJava = apm
.service({ name: 'opbeans-java', environment: 'production', agentName: 'java' })
.instance('instance');
Expand Down Expand Up @@ -95,13 +103,18 @@ export default function ApiTest({ getService }: FtrProviderContext) {
];
});

apmSynthtraceEsClient = await synthtrace.createApmSynthtraceEsClient();

return Promise.all([
apmSynthtraceEsClient.index(events),
apmSynthtraceEsClient.index(phpEvents),
]);
});

after(() => apmSynthtraceEsClient.clean());
after(async () => {
await apmSynthtraceEsClient.clean();
await supertest.destroy();
});

describe('create rule without kql filter', () => {
let ruleId: string;
Expand Down Expand Up @@ -141,6 +154,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
let results: Array<Record<string, string>>;

before(async () => {
await waitForActiveRule({ ruleId, supertest });
results = await waitForIndexConnectorResults({ es, minCount: 2 });
});

Expand All @@ -151,6 +165,11 @@ export default function ApiTest({ getService }: FtrProviderContext) {
]);
});

it('checks if rule is active', async () => {
const ruleStatus = await waitForActiveRule({ ruleId, supertest });
expect(ruleStatus).to.be('active');
});

it('has the right keys', async () => {
const phpEntry = results.find((result) => result.serviceName === 'opbeans-php')!;
expect(Object.keys(phpEntry).sort()).to.eql([
Expand All @@ -170,7 +189,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {

it('has the right values', () => {
const phpEntry = results.find((result) => result.serviceName === 'opbeans-php')!;
expect(omit(phpEntry, 'alertDetailsUrl')).to.eql({
expect(omit(phpEntry, 'alertDetailsUrl', 'viewInAppUrl')).to.eql({
environment: 'production',
interval: '1 hr',
reason:
Expand All @@ -181,9 +200,12 @@ export default function ApiTest({ getService }: FtrProviderContext) {
errorGroupingName: 'a php error',
threshold: '1',
triggerValue: '30',
viewInAppUrl:
'http://mockedPublicBaseUrl/app/apm/services/opbeans-php/errors?environment=production',
});

const url = new URL(phpEntry.viewInAppUrl);

expect(url.pathname).to.equal('/app/apm/services/opbeans-php/errors');
expect(url.searchParams.get('environment')).to.equal('production');
});
});

Expand Down
Loading

0 comments on commit 670ee82

Please sign in to comment.