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

chore(slo): update telemetry data #206135

Merged
merged 8 commits into from
Jan 14, 2025
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 @@ -7251,21 +7251,53 @@
"total": {
"type": "long",
"_meta": {
"description": "The total number of slos in the cluster"
"description": "The total number of SLOs in the cluster"
}
},
"definitions": {
"properties": {
"total": {
"type": "long",
"_meta": {
"description": "The total number of SLO definitions in the cluster"
}
},
"total_with_ccs": {
"type": "long",
"_meta": {
"description": "The total number of SLO definitions using CCS in the cluster"
}
},
"total_with_groups": {
"type": "long",
"_meta": {
"description": "The total number of SLO definitions using groups in the cluster"
}
}
}
},
"instances": {
"properties": {
"total": {
"type": "long",
"_meta": {
"description": "The total number of SLO instances in the cluster"
}
}
}
},
"by_status": {
"properties": {
"enabled": {
"type": "long",
"_meta": {
"description": "The number of enabled slos in the cluster"
"description": "The number of enabled SLOs in the cluster"
}
},
"disabled": {
"type": "long",
"_meta": {
"description": "The number of disabled slos in the cluster"
"description": "The number of disabled SLOs in the cluster"
}
}
}
Expand All @@ -7275,7 +7307,7 @@
"DYNAMIC_KEY": {
"type": "long",
"_meta": {
"description": "The number of slos by sli type in the cluster"
"description": "The number of SLOs by sli type in the cluster"
}
}
}
Expand All @@ -7285,7 +7317,7 @@
"DYNAMIC_KEY": {
"type": "long",
"_meta": {
"description": "The number of slos by rolling duration in the cluster"
"description": "The number of SLOs by rolling duration in the cluster"
}
}
}
Expand All @@ -7295,7 +7327,7 @@
"DYNAMIC_KEY": {
"type": "long",
"_meta": {
"description": "The number of slos by calendar aligned duration in the cluster"
"description": "The number of SLOs by calendar aligned duration in the cluster"
}
}
}
Expand All @@ -7305,13 +7337,13 @@
"occurrences": {
"type": "long",
"_meta": {
"description": "The number of slos by timeslices budgeting method in the cluster"
"description": "The number of SLOs by timeslices budgeting method in the cluster"
}
},
"timeslices": {
"type": "long",
"_meta": {
"description": "The number of slos by occurrences budgeting method in the cluster"
"description": "The number of SLOs by occurrences budgeting method in the cluster"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test/jest_integration',
rootDir: '../../../../..',
roots: ['<rootDir>/x-pack/solutions/observability/plugins/slo'],
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,45 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { isCCSRemoteIndexName } from '@kbn/es-query';
import { ALL_VALUE } from '@kbn/slo-schema';
import { CollectorFetchContext } from '@kbn/usage-collection-plugin/server';
import { StoredSLODefinition } from '../../domain/models';
import { SO_SLO_TYPE } from '../../saved_objects';
import { Usage } from './type';
import { SLO_SUMMARY_DESTINATION_INDEX_PATTERN } from '../../../common/constants';

export const fetcher = async (context: CollectorFetchContext) => {
const finder = context.soClient.createPointInTimeFinder<StoredSLODefinition>({
type: SO_SLO_TYPE,
perPage: 100,
});

const totalInstances = await context.esClient.count({
index: SLO_SUMMARY_DESTINATION_INDEX_PATTERN,
query: {
bool: {
filter: [
{
term: {
isTempDoc: false,
},
},
],
},
},
});

let usage: Usage['slo'] = {
total: 0,
definitions: {
total: 0,
total_with_ccs: 0,
total_with_groups: 0,
},
instances: {
total: totalInstances?.count ?? 0,
},
by_status: {
enabled: 0,
disabled: 0,
Expand All @@ -34,7 +60,16 @@ export const fetcher = async (context: CollectorFetchContext) => {
usage = response.saved_objects.reduce((acc, so) => {
return {
...acc,
total: acc.total + 1,
total: acc.total + 1, // deprecated in favor of definitions.total
definitions: {
total: acc.definitions.total + 1,
total_with_ccs: isCCSRemoteIndexName(so.attributes.indicator.params.index)
? acc.definitions.total_with_ccs + 1
: acc.definitions.total_with_ccs,
total_with_groups: [so.attributes.groupBy].flat().includes(ALL_VALUE)
? acc.definitions.total_with_groups
: acc.definitions.total_with_groups + 1,
},
by_status: {
...acc.by_status,
...(so.attributes.enabled && { enabled: acc.by_status.enabled + 1 }),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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 {
createTestServers,
type TestElasticsearchUtils,
type TestKibanaUtils,
} from '@kbn/core-test-helpers-kbn-server';
import {
SavedObjectsClient,
type ElasticsearchClient,
type Logger,
type SavedObjectsClientContract,
} from '@kbn/core/server';
import { KibanaSavedObjectsSLORepository, SLORepository } from '../../../services';
import {
createAPMTransactionDurationIndicator,
createAPMTransactionErrorRateIndicator,
createKQLCustomIndicator,
createMetricCustomIndicator,
createSLO,
createSLOWithTimeslicesBudgetingMethod,
createSyntheticsAvailabilityIndicator,
createTimesliceMetricIndicator,
} from '../../../services/fixtures/slo';
import { fetcher } from '../fetcher';

const createLoggerMock = (): jest.Mocked<Logger> => {
const logger = {
debug: jest.fn(),
info: jest.fn(),
error: jest.fn(),
get: jest.fn(),
} as unknown as jest.Mocked<Logger>;

logger.get.mockReturnValue(logger);

return logger;
};

describe('SLO usage collector fetcher', () => {
let esServer: TestElasticsearchUtils;
let esClient: ElasticsearchClient;
let soClient: SavedObjectsClientContract;
let kibanaServer: TestKibanaUtils;
let sloRepository: SLORepository;
let loggerMock: jest.Mocked<Logger>;

beforeAll(async () => {
await createServers();
});

afterAll(async () => {
await stopServers();
});

describe('with some SLOs', () => {
beforeEach(async () => {
await Promise.all([
sloRepository.create(createSLO({ indicator: createAPMTransactionErrorRateIndicator() })),
sloRepository.create(createSLO({ indicator: createAPMTransactionDurationIndicator() })),
sloRepository.create(createSLO({ indicator: createSyntheticsAvailabilityIndicator() })),
sloRepository.create(createSLO({ indicator: createKQLCustomIndicator() })),
sloRepository.create(
createSLOWithTimeslicesBudgetingMethod({ indicator: createMetricCustomIndicator() })
),
sloRepository.create(
createSLOWithTimeslicesBudgetingMethod({ indicator: createTimesliceMetricIndicator() })
),
sloRepository.create(
createSLO({ groupBy: ['host.name'], indicator: createKQLCustomIndicator() })
),
sloRepository.create(
createSLO({ groupBy: 'host.name', indicator: createKQLCustomIndicator() })
),
]);
});

it('returns the correct metrics', async () => {
const results = await fetcher({ soClient, esClient });

expect(results.slo).toMatchInlineSnapshot(`
Object {
"by_budgeting_method": Object {
"occurrences": 6,
"timeslices": 2,
},
"by_calendar_aligned_duration": Object {},
"by_rolling_duration": Object {
"7d": 8,
},
"by_sli_type": Object {
"sli.apm.transactionDuration": 1,
"sli.apm.transactionErrorRate": 1,
"sli.kql.custom": 3,
"sli.metric.custom": 1,
"sli.metric.timeslice": 1,
"sli.synthetics.availability": 1,
},
"by_status": Object {
"disabled": 0,
"enabled": 8,
},
"definitions": Object {
"total": 8,
"total_with_ccs": 0,
"total_with_groups": 2,
},
"instances": Object {
"total": 0,
},
"total": 8,
}
`);
});
});

async function createServers() {
const { startES, startKibana } = createTestServers({
adjustTimeout: jest.setTimeout,
settings: {
es: {
license: 'trial',
},
kbn: {
cliArgs: {
oss: false,
},
},
},
});

esServer = await startES();
kibanaServer = await startKibana();

esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser;
soClient = new SavedObjectsClient(
kibanaServer.coreStart.savedObjects.createInternalRepository()
);
loggerMock = createLoggerMock();

sloRepository = new KibanaSavedObjectsSLORepository(soClient, loggerMock);
}

async function stopServers() {
if (kibanaServer) {
await kibanaServer.stop();
}
if (esServer) {
await esServer.stop();
}

jest.clearAllMocks();
}
});
Loading
Loading