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

Add new telemetry data from event-log index. #140943

Merged
merged 17 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
66 changes: 66 additions & 0 deletions x-pack/plugins/actions/server/usage/actions_telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,56 @@ Object {
},
},
},
avg_run_duration_by_connector_type: {
connector_types: {
buckets: [
{
key: '.slack',
duration: {
average: {
value: 10,
},
},
},
{
key: '.email',
duration: {
average: {
value: 11,
},
},
},
],
},
},
count_connector_types_by_action_run_outcome_per_day: {
connector_types: {
buckets: [
{
key: '.slack',
outcome: {
count: {
buckets: [
{ key: 'success', doc_count: 12 },
{ key: 'failure', doc_count: 1 },
],
},
},
},
{
key: '.email',
outcome: {
count: {
buckets: [
{ key: 'success', doc_count: 13 },
{ key: 'failure', doc_count: 2 },
],
},
},
},
],
},
},
},
}
);
Expand Down Expand Up @@ -754,6 +804,20 @@ Object {
__slack: 7,
},
countTotal: 120,
avgRunDurationByConnectorType: {
__email: 11,
__slack: 10,
},
countRunOutcomeByConnectorType: {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the paradigm we follow in other telemetry objects is to have the rule type id or connector type id last. That way, we could search by countRunOutcomeByConnectorType.failed.* and get all the different connector types that have failed, instead of having to look inside each connector type object. WDYT of switching this around to be similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But the request is We'd like to know which connector types are failing the most relative to their successful runs.
Wouldn't it be more difficult to get a connector's success/failure ratio?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm...I'm not sure either of these options will make calculating the ratio per rule type easier :). We can leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my idea was getting it like:
count_connector_types_by_action_run_outcome_per_day.__slack.failure / count_connector_types_by_action_run_outcome_per_day.__slack.success

but

count_connector_types_by_action_run_outcome_per_day.failure.__slack./ count_connector_types_by_action_run_outcome_per_day.success.__slack

would also do the same thing... IDK i can change it :)

__email: {
failure: 2,
success: 13,
},
__slack: {
failure: 1,
success: 12,
},
},
hasErrors: false,
});
});
Expand All @@ -772,9 +836,11 @@ Object {
Object {
"avgExecutionTime": 0,
"avgExecutionTimeByType": Object {},
"avgRunDurationByConnectorType": Object {},
"countByType": Object {},
"countFailed": 0,
"countFailedByType": Object {},
"countRunOutcomeByConnectorType": Object {},
"countTotal": 0,
"errorMessage": "oh no",
"hasErrors": true,
Expand Down
76 changes: 75 additions & 1 deletion x-pack/plugins/actions/server/usage/actions_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AggregationsTermsAggregateBase } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import {
AvgActionRunDurationByConnectorTypeBucket,
AvgActionRunOutcomeByConnectorTypeBucket,
parseActionRunOutcomeByConnectorTypesBucket,
parseDurationsByConnectorTypesBucket,
} from './lib/parse_connector_type_bucket';
import { AlertHistoryEsIndexConnectorId } from '../../common';
import { ActionResult, PreConfiguredAction } from '../types';

Expand Down Expand Up @@ -395,7 +402,7 @@ export async function getInUseTotalCount(
}
}

function replaceFirstAndLastDotSymbols(strToReplace: string) {
export function replaceFirstAndLastDotSymbols(strToReplace: string) {
const hasFirstSymbolDot = strToReplace.startsWith('.');
const appliedString = hasFirstSymbolDot ? strToReplace.replace('.', '__') : strToReplace;
const hasLastSymbolDot = strToReplace.endsWith('.');
Expand All @@ -415,6 +422,8 @@ export async function getExecutionsPerDayCount(
countFailedByType: Record<string, number>;
avgExecutionTime: number;
avgExecutionTimeByType: Record<string, number>;
avgRunDurationByConnectorType: Record<string, number>;
countRunOutcomeByConnectorType: Record<string, number>;
}> {
const scriptedMetric = {
scripted_metric: {
Expand Down Expand Up @@ -536,6 +545,54 @@ export async function getExecutionsPerDayCount(
},
},
},
avg_run_duration_by_connector_type: {
nested: {
path: 'kibana.saved_objects',
},
aggs: {
connector_types: {
terms: {
field: 'kibana.saved_objects.type_id',
},
aggs: {
duration: {
reverse_nested: {},
aggs: {
average: {
avg: {
field: 'event.duration',
},
},
},
},
},
},
},
},
count_connector_types_by_action_run_outcome_per_day: {
nested: {
path: 'kibana.saved_objects',
},
aggs: {
connector_types: {
terms: {
field: 'kibana.saved_objects.type_id',
},
aggs: {
outcome: {
reverse_nested: {},
aggs: {
count: {
terms: {
field: 'event.outcome',
},
},
},
},
},
},
},
},
},
},
});
Expand Down Expand Up @@ -564,6 +621,15 @@ export async function getExecutionsPerDayCount(
{}
);

const aggregations = actionResults.aggregations as {
avg_run_duration_by_connector_type: {
connector_types: AggregationsTermsAggregateBase<AvgActionRunDurationByConnectorTypeBucket>;
};
count_connector_types_by_action_run_outcome_per_day: {
connector_types: AggregationsTermsAggregateBase<AvgActionRunOutcomeByConnectorTypeBucket>;
};
};

return {
hasErrors: false,
countTotal: aggsExecutions.total,
Expand All @@ -586,6 +652,12 @@ export async function getExecutionsPerDayCount(
),
avgExecutionTime: aggsAvgExecutionTime,
avgExecutionTimeByType,
avgRunDurationByConnectorType: parseDurationsByConnectorTypesBucket(
aggregations.avg_run_duration_by_connector_type.connector_types.buckets
),
countRunOutcomeByConnectorType: parseActionRunOutcomeByConnectorTypesBucket(
aggregations.count_connector_types_by_action_run_outcome_per_day.connector_types.buckets
),
};
} catch (err) {
const errorMessage = err && err.message ? err.message : err.toString();
Expand All @@ -601,6 +673,8 @@ export async function getExecutionsPerDayCount(
countFailedByType: {},
avgExecutionTime: 0,
avgExecutionTimeByType: {},
avgRunDurationByConnectorType: {},
countRunOutcomeByConnectorType: {},
};
}
}
10 changes: 10 additions & 0 deletions x-pack/plugins/actions/server/usage/actions_usage_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export function createActionsUsageCollector(
count_actions_executions_failed_by_type_per_day: byTypeSchema,
avg_execution_time_per_day: { type: 'long' },
avg_execution_time_by_type_per_day: byTypeSchema,
count_connector_types_by_action_run_outcome_per_day: {
DYNAMIC_KEY: {
success: { type: 'long' },
failure: { type: 'long' },
unknown: { type: 'long' },
},
},
avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } },
},
fetch: async () => {
try {
Expand Down Expand Up @@ -77,6 +85,8 @@ export function createActionsUsageCollector(
count_actions_executions_failed_by_type_per_day: {},
avg_execution_time_per_day: 0,
avg_execution_time_by_type_per_day: {},
count_connector_types_by_action_run_outcome_per_day: {},
avg_actions_run_duration_by_connector_type_per_day: {},
};
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 {
parseDurationsByConnectorTypesBucket,
parseActionRunOutcomeByConnectorTypesBucket,
} from './parse_connector_type_bucket';

describe('parseDurationsByConnectorTypesBucket', () => {
test('should correctly parse connector type bucket results', () => {
expect(
parseDurationsByConnectorTypesBucket([
{
key: '.server-log',
doc_count: 78,
duration: { average: { value: 10.2 } },
},
{
key: '.index',
doc_count: 42,
duration: { average: { value: 11.6 } },
},
{
key: '.slack',
doc_count: 28,
duration: { average: { value: 12.4 } },
},
])
).toEqual({
'__server-log': 10,
__index: 12,
__slack: 12,
});
});

test('should handle missing values', () => {
expect(
parseDurationsByConnectorTypesBucket([
// @ts-expect-error
{
key: '.server-log',
doc_count: 78,
},
{
key: '.index',
doc_count: 42,
// @ts-expect-error
duration: {},
},
{
key: '.slack',
doc_count: 28,
// @ts-expect-error
duration: { average: {} },
},
])
).toEqual({
'__server-log': 0,
__index: 0,
__slack: 0,
});
});

test('should handle empty input', () => {
expect(parseDurationsByConnectorTypesBucket([])).toEqual({});
});
//
test('should handle undefined input', () => {
expect(parseDurationsByConnectorTypesBucket(undefined)).toEqual({});
});
});

describe('parseActionRunOutcomeByConnectorTypesBucket', () => {
test('should correctly parse connector type bucket results', () => {
expect(
parseActionRunOutcomeByConnectorTypesBucket([
{
key: '.server-log',
doc_count: 78,
outcome: {
count: {
buckets: [
{ key: 'success', doc_count: 2 },
{ key: 'failure', doc_count: 1 },
],
},
},
},
{
key: '.index',
doc_count: 42,
outcome: {
count: {
buckets: [
{ key: 'success', doc_count: 3 },
{ key: 'failure', doc_count: 4 },
],
},
},
},
])
).toEqual({
__index: {
failure: 4,
success: 3,
},
'__server-log': {
failure: 1,
success: 2,
},
});
});

test('should handle missing values', () => {
expect(
parseActionRunOutcomeByConnectorTypesBucket([
{
key: '.server-log',
doc_count: 78,
outcome: {
count: {
// @ts-expect-error
buckets: [{ key: 'success', doc_count: 2 }, { key: 'failure' }],
},
},
},
{
key: '.index',
outcome: {
// @ts-expect-error
count: {},
},
},
])
).toEqual({
'__server-log': {
failure: 0,
success: 2,
},
__index: {},
});
});

test('should handle empty input', () => {
expect(parseActionRunOutcomeByConnectorTypesBucket([])).toEqual({});
});
//
test('should handle undefined input', () => {
expect(parseActionRunOutcomeByConnectorTypesBucket(undefined)).toEqual({});
});
});
Loading