Skip to content

Commit

Permalink
Uptime index config using kibana.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Oct 20, 2021
1 parent d2c6c41 commit 0c5154d
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ kibana_vars=(
xpack.task_manager.poll_interval
xpack.task_manager.request_capacity
xpack.task_manager.version_conflict_threshold
xpack.uptime.index
)

longopts=''
Expand Down
17 changes: 17 additions & 0 deletions x-pack/plugins/uptime/server/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { PluginConfigDescriptor } from 'kibana/server';
import { schema, TypeOf } from '@kbn/config-schema';

export const config: PluginConfigDescriptor = {
schema: schema.object({
index: schema.string(),
}),
};

export type UptimeConfig = TypeOf<typeof config.schema>;
2 changes: 2 additions & 0 deletions x-pack/plugins/uptime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ import { Plugin } from './plugin';

export const plugin = (initializerContext: PluginInitializerContext) =>
new Plugin(initializerContext);

export { config } from './config';
6 changes: 4 additions & 2 deletions x-pack/plugins/uptime/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { initUptimeServer } from './uptime_server';
import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework';
import { umDynamicSettings } from './lib/saved_objects';
import { UptimeRuleRegistry } from './plugin';
import { UptimeConfig } from './config';

export interface KibanaRouteOptions {
path: string;
Expand All @@ -31,7 +32,8 @@ export const initServerWithKibana = (
server: UptimeCoreSetup,
plugins: UptimeCorePlugins,
ruleRegistry: UptimeRuleRegistry,
logger: Logger
logger: Logger,
config: UptimeConfig
) => {
const { features } = plugins;
const libs = compose(server);
Expand Down Expand Up @@ -113,5 +115,5 @@ export const initServerWithKibana = (
},
});

initUptimeServer(server, libs, plugins, ruleRegistry, logger);
initUptimeServer(server, libs, plugins, ruleRegistry, logger, config);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MlPluginSetup as MlSetup } from '../../../../../ml/server';
import { RuleRegistryPluginSetupContract } from '../../../../../rule_registry/server';
import { UptimeESClient } from '../../lib';
import type { UptimeRouter } from '../../../types';
import { UptimeConfig } from '../../../config';

export type UMElasticsearchQueryFn<P, R = any> = (
params: {
Expand All @@ -28,6 +29,7 @@ export type UMElasticsearchQueryFn<P, R = any> = (

export type UMSavedObjectsQueryFn<T = any, P = undefined> = (
client: SavedObjectsClientContract | ISavedObjectsRepository,
config: UptimeConfig,
params?: P
) => Promise<T> | T;

Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/uptime/server/lib/saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ export const umDynamicSettings: SavedObjectsType = {
};

export const savedObjectsAdapter: UMSavedObjectsAdapter = {
getUptimeDynamicSettings: async (client): Promise<DynamicSettings> => {
getUptimeDynamicSettings: async (client, config): Promise<DynamicSettings> => {
try {
const obj = await client.get<DynamicSettings>(umDynamicSettings.name, settingsObjectId);
return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS;
} catch (getErr) {
if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) {
if (config?.index) {
return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index };
}
return DYNAMIC_SETTINGS_DEFAULTS;
}
throw getErr;
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { KibanaTelemetryAdapter, UptimeCorePlugins } from './lib/adapters';
import { umDynamicSettings } from './lib/saved_objects';
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { Dataset } from '../../rule_registry/server';
import { UptimeConfig } from './config';

export type UptimeRuleRegistry = ReturnType<Plugin['setup']>['ruleRegistry'];

Expand All @@ -32,6 +33,8 @@ export class Plugin implements PluginType {
}

public setup(core: CoreSetup, plugins: UptimeCorePlugins) {
const config = this.initContext.config.get<UptimeConfig>();

this.logger = this.initContext.logger.get();
const { ruleDataService } = plugins.ruleRegistry;

Expand All @@ -52,7 +55,8 @@ export class Plugin implements PluginType {
{ router: core.http.createRouter() },
plugins,
ruleDataClient,
this.logger
this.logger,
config
);
core.savedObjects.registerType(umDynamicSettings);
KibanaTelemetryAdapter.registerUsageCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const createRouteWithAuth = (
request,
response,
savedObjectsClient,
config,
}) => {
const { statusCode, message } = libs.license(context.licensing.license);
if (statusCode === 200) {
Expand All @@ -29,6 +30,7 @@ export const createRouteWithAuth = (
request,
response,
savedObjectsClient,
config,
});
}
switch (statusCode) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSer
method: 'GET',
path: '/api/uptime/dynamic_settings',
validate: false,
handler: async ({ savedObjectsClient }): Promise<any> => {
return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
handler: async ({ savedObjectsClient, config }): Promise<any> => {
return savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient, config);
},
});

Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugins/uptime/server/rest_api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from 'kibana/server';
import { UMServerLibs, UptimeESClient } from '../lib/lib';
import type { UptimeRequestHandlerContext } from '../types';
import { UptimeConfig } from '../config';

/**
* Defines the basic properties employed by Uptime routes.
Expand Down Expand Up @@ -58,7 +59,10 @@ export type UMRestApiRouteFactory = (libs: UMServerLibs) => UptimeRoute;
* Functions of this type accept our internal route format and output a route
* object that the Kibana platform can consume.
*/
export type UMKibanaRouteWrapper = (uptimeRoute: UptimeRoute) => UMKibanaRoute;
export type UMKibanaRouteWrapper = (
uptimeRoute: UptimeRoute,
config: UptimeConfig
) => UMKibanaRoute;

/**
* This is the contract we specify internally for route handling.
Expand All @@ -69,10 +73,12 @@ export type UMRouteHandler = ({
request,
response,
savedObjectsClient,
config,
}: {
uptimeEsClient: UptimeESClient;
context: UptimeRequestHandlerContext;
request: KibanaRequest<Record<string, any>, Record<string, any>, Record<string, any>>;
response: KibanaResponseFactory;
savedObjectsClient: SavedObjectsClientContract;
config: UptimeConfig;
}) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createUptimeESClient, inspectableEsQueriesMap } from '../lib/lib';
import { KibanaResponse } from '../../../../../src/core/server/http/router';
import { enableInspectEsQueries } from '../../../observability/common';

export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute) => ({
export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute, config) => ({
...uptimeRoute,
options: {
tags: ['access:uptime-read', ...(uptimeRoute?.writeAccess ? ['access:uptime-write'] : [])],
Expand Down Expand Up @@ -40,6 +40,7 @@ export const uptimeRouteWrapper: UMKibanaRouteWrapper = (uptimeRoute) => ({
context,
request,
response,
config,
});

if (res instanceof KibanaResponse) {
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/uptime/server/uptime_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ import { statusCheckAlertFactory } from './lib/alerts/status_check';
import { tlsAlertFactory } from './lib/alerts/tls';
import { tlsLegacyAlertFactory } from './lib/alerts/tls_legacy';
import { durationAnomalyAlertFactory } from './lib/alerts/duration_anomaly';
import { UptimeConfig } from './config';

export const initUptimeServer = (
server: UptimeCoreSetup,
libs: UMServerLibs,
plugins: UptimeCorePlugins,
ruleDataClient: IRuleDataClient,
logger: Logger
logger: Logger,
config: UptimeConfig
) => {
restApiRoutes.forEach((route) =>
libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route)))
libs.framework.registerRoute(uptimeRouteWrapper(createRouteWithAuth(libs, route), config))
);

const {
Expand Down

0 comments on commit 0c5154d

Please sign in to comment.