import { i18n } from '@kbn/i18n' ; import type { ValuesType } from 'utility-types' ; import type { AsDuration , AsPercent } from '../../observability/common' ; import type { ActionGroup } from '../../alerting/common' ; import { ANOMALY_SEVERITY , ANOMALY_THRESHOLD } from './ml_constants' ; export const APM_SERVER_FEATURE_ID = 'apm' ; export enum AlertType { ErrorCount = 'apm.error_rate' , TransactionErrorRate = 'apm.transaction_error_rate' , TransactionDuration = 'apm.transaction_duration' , TransactionDurationAnomaly = 'apm.transaction_duration_anomaly' } export const THRESHOLD_MET_GROUP_ID = 'threshold_met' ; export type ThresholdMetActionGroupId = typeof THRESHOLD_MET_GROUP_ID ; const THRESHOLD_MET_GROUP = { id : THRESHOLD_MET_GROUP_ID , name : i18n . translate ( 'xpack.apm.a.thresholdMet' , { defaultMessage : 'Threshold met' , } ) , } ; export function formatErrorCountReason ( { threshold , measured , serviceName , } ) { return i18n . translate ( 'xpack.apm.alertTypes.errorCount.reason' , { defaultMessage : `Error count is greater than {threshold} (current value is {measured}) for {serviceName}` , values : { threshold , measured , serviceName , } , } ) ; } export function formatTransactionDurationReason ( { threshold , measured , serviceName , asDuration , } ) { return i18n . translate ( 'xpack.apm.alertTypes.transactionDuration.reason' , { defaultMessage : `Latency is above {threshold} (current value is {measured}) for {serviceName}` , values : { threshold : asDuration ( threshold ) , measured : asDuration ( measured ) , serviceName , } , } ) ; } export function formatTransactionErrorRateReason ( { threshold , measured , serviceName , asPercent , } ) { return i18n . translate ( 'xpack.apm.alertTypes.transactionErrorRate.reason' , { defaultMessage : `Failed transactions rate is greater than {threshold} (current value is {measured}) for {serviceName}` , values : { threshold : asPercent ( threshold , 100 ) , measured : asPercent ( measured , 100 ) , serviceName , } , } ) ; } export function formatTransactionDurationAnomalyReason ( { serviceName , severityLevel , measured , } ) { return i18n . translate ( 'xpack.apm.alertTypes.transactionDurationAnomaly.reason' , { defaultMessage : `{severityLevel} anomaly detected for {serviceName} (score was {measured})` , values : { serviceName , severityLevel , measured , } , } ) ; } export const ALERT_TYPES_CONFIG = { [ AlertType . ErrorCount ] : { name : i18n . translate ( 'xpack.apm.errorCountAlert.name' , { defaultMessage : 'Error count threshold' , } ) , actionGroups : [ THRESHOLD_MET_GROUP ] , defaultActionGroupId : THRESHOLD_MET_GROUP_ID , minimumLicenseRequired : 'basic' , producer : APM_SERVER_FEATURE_ID , isExportable : true , } , [ AlertType . TransactionDuration ] : { name : i18n . translate ( 'xpack.apm.transactionDurationAlert.name' , { defaultMessage : 'Latency threshold' , } ) , actionGroups : [ THRESHOLD_MET_GROUP ] , defaultActionGroupId : THRESHOLD_MET_GROUP_ID , minimumLicenseRequired : 'basic' , producer : APM_SERVER_FEATURE_ID , isExportable : true , } , [ AlertType . TransactionDurationAnomaly ] : { name : i18n . translate ( 'xpack.apm.transactionDurationAnomalyAlert.name' , { defaultMessage : 'Latency anomaly' , } ) , actionGroups : [ THRESHOLD_MET_GROUP ] , defaultActionGroupId : THRESHOLD_MET_GROUP_ID , minimumLicenseRequired : 'basic' , producer : APM_SERVER_FEATURE_ID , isExportable : true , } , [ AlertType . TransactionErrorRate ] : { name : i18n . translate ( 'xpack.apm.transactionErrorRateAlert.name' , { defaultMessage : 'Failed transaction rate threshold' , } ) , actionGroups : [ THRESHOLD_MET_GROUP ] , defaultActionGroupId : THRESHOLD_MET_GROUP_ID , minimumLicenseRequired : 'basic' , producer : APM_SERVER_FEATURE_ID , isExportable : true , } , } ; export const ANOMALY_ALERT_SEVERITY_TYPES = [ { type : ANOMALY_SEVERITY . CRITICAL , label : i18n . translate ( 'xpack.apm.alerts.anomalySeverity.criticalLabel' , { defaultMessage : 'critical' , } ) , threshold : ANOMALY_THRESHOLD . CRITICAL , } , { type : ANOMALY_SEVERITY . MAJOR , label : i18n . translate ( 'xpack.apm.alerts.anomalySeverity.majorLabel' , { defaultMessage : 'major' , } ) , threshold : ANOMALY_THRESHOLD . MAJOR , } , { type : ANOMALY_SEVERITY . MINOR , label : i18n . translate ( 'xpack.apm.alerts.anomalySeverity.minor' , { defaultMessage : 'minor' , } ) , threshold : ANOMALY_THRESHOLD . MINOR , } , { type : ANOMALY_SEVERITY . WARNING , label : i18n . translate ( 'xpack.apm.alerts.anomalySeverity.warningLabel' , { defaultMessage : 'warning' , } ) , threshold : ANOMALY_THRESHOLD . WARNING , } , ] as const ; export type AnomalyAlertSeverityType = ValuesType < typeof ANOMALY_ALERT_SEVERITY_TYPES > [ 'type' ] ;