Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Actionable Observability] - Formate Actual value and the Expected value in Alert Details #147025

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ import {
ALERT_EVALUATION_THRESHOLD,
ALERT_EVALUATION_VALUE,
ALERT_RULE_TAGS,
ALERT_RULE_TYPE_ID,
ALERT_START,
ALERT_STATUS,
ALERT_STATUS_ACTIVE,
ALERT_STATUS_RECOVERED,
TIMESTAMP,
} from '@kbn/rule-data-utils';
import { formatAlertEvaluationValue } from '../../../utils/format_alert_evaluation_value';
import { asDuration } from '../../../../common/utils/formatters';
import { AlertSummaryProps } from '../types';
import { AlertStatusIndicator } from '../../../components/shared/alert_status_indicator';
import { DEFAULT_DATE_FORMAT } from '../constants';

export function AlertSummary({ alert }: AlertSummaryProps) {
const tags = alert?.fields[ALERT_RULE_TAGS];

return (
<div data-test-subj="alert-summary-container">
<EuiFlexGroup>
Expand All @@ -49,7 +50,10 @@ export function AlertSummary({ alert }: AlertSummaryProps) {
</EuiTitle>
<EuiSpacer size="s" />
<EuiText size="s" color="subdued">
{alert?.fields[ALERT_EVALUATION_VALUE] ?? '-'}
{formatAlertEvaluationValue(
alert?.fields[ALERT_RULE_TYPE_ID],
alert?.fields[ALERT_EVALUATION_VALUE]
)}
</EuiText>
</EuiFlexItem>
<EuiFlexItem>
Expand All @@ -63,7 +67,10 @@ export function AlertSummary({ alert }: AlertSummaryProps) {
</EuiTitle>
<EuiSpacer size="s" />
<EuiText size="s" color="subdued">
{alert?.fields[ALERT_EVALUATION_THRESHOLD] ?? '-'}
{formatAlertEvaluationValue(
alert?.fields[ALERT_RULE_TYPE_ID],
alert?.fields[ALERT_EVALUATION_THRESHOLD]
)}
</EuiText>
</EuiFlexItem>
<EuiFlexItem>
Expand Down Expand Up @@ -94,7 +101,7 @@ export function AlertSummary({ alert }: AlertSummaryProps) {
<AlertStatusIndicator
textSize="s"
alertStatus={
alert?.fields[ALERT_STATUS] === ALERT_STATUS_ACTIVE
alert.fields[ALERT_STATUS] === ALERT_STATUS_ACTIVE
? ALERT_STATUS_ACTIVE
: ALERT_STATUS_RECOVERED
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { formatAlertEvaluationValue } from './format_alert_evaluation_value';

describe('formatAlertEvaluationValue', () => {
it('returns - when there is no evaluationValue passed', () => {
expect(formatAlertEvaluationValue('apm.transaction_error_rate', undefined)).toBe('-');
});
it('returns the evaluation value when ruleTypeId in unknown aka unformatted', () => {
expect(formatAlertEvaluationValue('unknown.rule.type', 2000)).toBe(2000);
});
it('returns the evaluation value formatted as percent when the alert rule type is "apm.transaction_error_rate" ', () => {
expect(formatAlertEvaluationValue('apm.transaction_error_rate', 20)).toBe('20%');
});
it('returns the evaluation value formatted as duration in ms when the alert rule type is "apm.transaction_duration" ', () => {
expect(formatAlertEvaluationValue('apm.transaction_duration', 140000)).toBe('140 ms');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { asMillisecondDuration, asPercent } from '../../common/utils/formatters';
import {
ALERT_EVALUATION_UNIT_TYPE,
getAlertEvaluationUnitTypeByRuleTypeId,
} from './get_alert_evaluation_unit_type_by_rule_type_id';

export const formatAlertEvaluationValue = (ruleTypeId?: string, evaluationValue?: number) => {
if (!evaluationValue || !ruleTypeId) return '-';
const unitType = getAlertEvaluationUnitTypeByRuleTypeId(ruleTypeId);
switch (unitType) {
case ALERT_EVALUATION_UNIT_TYPE.DURATION:
return asMillisecondDuration(evaluationValue);
case ALERT_EVALUATION_UNIT_TYPE.PERCENT:
return asPercent(evaluationValue, 100);
default:
return evaluationValue;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const ALERT_EVALUATION_UNIT_TYPE = {
DURATION: 'DURATION',
PERCENT: 'PERCENT',
NUMBER: 'NUMBER',
} as const;

type ObjectValues<T> = T[keyof T];
type AlertEvaluationUnitType = ObjectValues<typeof ALERT_EVALUATION_UNIT_TYPE>;

export const getAlertEvaluationUnitTypeByRuleTypeId = (
ruleTypeId: string
): AlertEvaluationUnitType => {
switch (ruleTypeId) {
case 'apm.transaction_duration':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking out loud:
Does it make sense that the Observability plugin knows about specific details of the APM rule types? I am wondering if this logic is something that should be provided by each app or if we should handle it like this in the Observability plugin. (Especially if we will have similar scenarios for each rule type)
@fkanout @simianhacker What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maryam-saeidi, it's a good question. The goal was to make the AlertSummary sharable, and then each App will import it and then use their logic/formatter.

However, when I started implementation, I realized that

  • APM (most likely other Apps) don't have formatters, AND they are importing and using Observability formatter.
  • Doing the formatting at the Rule level is not ideal, as we only have three types of values that need to be formatted across all rule types.

Copy link
Member

@maryam-saeidi maryam-saeidi Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.

It makes sense that Observability shares formatter since the formatting logic can be re-used in different rule types.

The concern here is to spread the logic related to rules in multiple plugins and make the Observability plugin unnecessarily aware of details of each rule type and as a result, in case of any change or addition, to always update this plugin as well. So basically, we are affecting the separation of concern here.

Doing the formatting at the Rule level is not ideal, as we only have three types of values that need to be formatted across all rule types.

Also, can you please elaborate on why it is not ideal? What issues it might cause?

return ALERT_EVALUATION_UNIT_TYPE.DURATION;
case 'apm.transaction_error_rate':
return ALERT_EVALUATION_UNIT_TYPE.PERCENT;
default:
return ALERT_EVALUATION_UNIT_TYPE.NUMBER;
}
};