diff --git a/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.test.ts b/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.test.ts new file mode 100644 index 000000000000..9dedf9b5afaa --- /dev/null +++ b/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.test.ts @@ -0,0 +1,30 @@ +/* + * 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 { savedObjectsClientMock } from '@kbn/core/server/mocks'; +import { defaultSourceConfiguration, InfraSource } from '../sources'; +import { createInfraSourcesMock } from '../sources/mocks'; +import { makeGetMetricIndices } from './make_get_metric_indices'; + +describe('getMetricIndices', () => { + it('should return the indices from a resolved configuration', async () => { + const sourceConfiguration: InfraSource = { + id: 'default', + origin: 'stored', + configuration: defaultSourceConfiguration, + }; + const infraSourcesMock = createInfraSourcesMock(); + infraSourcesMock.getSourceConfiguration.mockResolvedValueOnce(sourceConfiguration); + + const getMetricIndices = makeGetMetricIndices(infraSourcesMock); + + const savedObjectsClient = savedObjectsClientMock.create(); + const metricIndices = await getMetricIndices(savedObjectsClient); + + expect(metricIndices).toEqual(defaultSourceConfiguration.metricAlias); + }); +}); diff --git a/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.ts b/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.ts new file mode 100644 index 000000000000..a6ff7e96a55a --- /dev/null +++ b/x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.ts @@ -0,0 +1,16 @@ +/* + * 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 type { SavedObjectsClientContract } from '@kbn/core/server'; +import type { IInfraSources } from '../sources'; + +export function makeGetMetricIndices(metricSources: IInfraSources) { + return async (savedObjectsClient: SavedObjectsClientContract, sourceId: string = 'default') => { + const source = await metricSources.getSourceConfiguration(savedObjectsClient, sourceId); + return source.configuration.metricAlias; + }; +} diff --git a/x-pack/plugins/infra/server/plugin.ts b/x-pack/plugins/infra/server/plugin.ts index 8eb2bac57dc5..bfd7113ec4dc 100644 --- a/x-pack/plugins/infra/server/plugin.ts +++ b/x-pack/plugins/infra/server/plugin.ts @@ -7,8 +7,6 @@ import { Server } from '@hapi/hapi'; import { schema } from '@kbn/config-schema'; -import { i18n } from '@kbn/i18n'; -import { Logger } from '@kbn/logging'; import { CoreStart, Plugin, @@ -16,6 +14,8 @@ import { PluginInitializerContext, } from '@kbn/core/server'; import { handleEsError } from '@kbn/es-ui-shared-plugin/server'; +import { i18n } from '@kbn/i18n'; +import { Logger } from '@kbn/logging'; import { LOGS_FEATURE_ID, METRICS_FEATURE_ID } from '../common/constants'; import { defaultLogViewsStaticConfig } from '../common/log_views'; import { publicConfigKeys } from '../common/plugin_config_types'; @@ -35,6 +35,7 @@ import { InfraFieldsDomain } from './lib/domains/fields_domain'; import { InfraLogEntriesDomain } from './lib/domains/log_entries_domain'; import { InfraMetricsDomain } from './lib/domains/metrics_domain'; import { InfraBackendLibs, InfraDomainLibs } from './lib/infra_types'; +import { makeGetMetricIndices } from './lib/metrics/make_get_metric_indices'; import { infraSourceConfigurationSavedObjectType, InfraSources } from './lib/sources'; import { InfraSourceStatus } from './lib/source_status'; import { logViewSavedObjectType } from './saved_objects'; @@ -236,6 +237,7 @@ export class InfraServerPlugin return { logViews, + getMetricIndices: makeGetMetricIndices(this.libs.sources), }; } diff --git a/x-pack/plugins/infra/server/types.ts b/x-pack/plugins/infra/server/types.ts index e3792c8977cf..108575c0f832 100644 --- a/x-pack/plugins/infra/server/types.ts +++ b/x-pack/plugins/infra/server/types.ts @@ -5,7 +5,11 @@ * 2.0. */ -import type { CoreSetup, CustomRequestHandlerContext } from '@kbn/core/server'; +import type { + CoreSetup, + CustomRequestHandlerContext, + SavedObjectsClientContract, +} from '@kbn/core/server'; import type { SearchRequestHandlerContext } from '@kbn/data-plugin/server'; import type { MlPluginSetup } from '@kbn/ml-plugin/server'; import type { InfraStaticSourceConfiguration } from '../common/source_configuration/source_configuration'; @@ -27,6 +31,10 @@ export interface InfraPluginSetup { export interface InfraPluginStart { logViews: LogViewsServiceStart; + getMetricIndices: ( + savedObjectsClient: SavedObjectsClientContract, + sourceId?: string + ) => Promise; } export type MlSystem = ReturnType;