Skip to content

Commit

Permalink
[Uptime] Synthetics monitor saved object (#119318)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Nov 23, 2021
1 parent 94c0a74 commit 4706a8b
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const previouslyRegisteredTypes = [
'siem-ui-timeline-pinned-event',
'space',
'spaces-usage-stats',
'synthetics-monitor',
'tag',
'task',
'telemetry',
Expand Down
16 changes: 16 additions & 0 deletions x-pack/plugins/uptime/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { SimpleSavedObject } from 'kibana/public';

/** Represents the average monitor duration ms at a point in time. */
export interface MonitorDurationAveragePoint {
/** The timeseries value for this point. */
Expand All @@ -28,3 +30,17 @@ export interface MonitorDurationResult {
export interface MonitorIdParam {
monitorId: string;
}

export type SyntheticsMonitorSavedObject = SimpleSavedObject<{
name: string;
runOnce: boolean;
urls?: string[];
tags?: string[];
schedule: string;
type: 'http' | 'tcp' | 'icmp' | 'browser';
source?: {
inline: {
script: string;
};
};
}>;
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PLUGIN } from '../common/constants/plugin';
import { compose } from './lib/compose/kibana';
import { initUptimeServer } from './uptime_server';
import { UptimeCorePlugins, UptimeCoreSetup } from './lib/adapters/framework';
import { umDynamicSettings } from './lib/saved_objects';
import { umDynamicSettings } from './lib/saved_objects/uptime_settings';
import { UptimeRuleRegistry } from './plugin';

export interface KibanaRouteOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import moment from 'moment';
import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server';
import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { PageViewParams, UptimeTelemetry, Usage } from './types';
import { savedObjectsAdapter } from '../../saved_objects';
import { savedObjectsAdapter } from '../../saved_objects/saved_objects';
import { UptimeESClient, createUptimeESClient } from '../../lib';

interface UptimeTelemetryCollector {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/lib/alerts/tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CertResult, DynamicSettings } from '../../../common/runtime_types';
import { createRuleTypeMocks, bootstrapDependencies } from './test_utils';
import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';

import { savedObjectsAdapter, UMSavedObjectsAdapter } from '../saved_objects';
import { savedObjectsAdapter, UMSavedObjectsAdapter } from '../saved_objects/saved_objects';

/**
* This function aims to provide an easy way to give mock props that will
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/lib/alerts/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { TlsTranslations } from '../../../common/translations';

import { ActionGroupIdsOf } from '../../../../alerting/common';

import { savedObjectsAdapter } from '../saved_objects';
import { savedObjectsAdapter } from '../saved_objects/saved_objects';
import { createUptimeESClient } from '../lib';

export type ActionGroupIds = ActionGroupIdsOf<typeof TLS>;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/lib/alerts/tls_legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ActionGroupIdsOf } from '../../../../alerting/common';
import { AlertInstanceContext } from '../../../../alerting/common';
import { AlertInstance } from '../../../../alerting/server';

import { savedObjectsAdapter } from '../saved_objects';
import { savedObjectsAdapter } from '../saved_objects/saved_objects';
import { createUptimeESClient } from '../lib';
import {
DEFAULT_FROM,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { UMBackendFrameworkAdapter } from './adapters';
import { UMLicenseCheck } from './domains';
import { UptimeRequests } from './requests';
import { savedObjectsAdapter } from './saved_objects';
import { savedObjectsAdapter } from './saved_objects/saved_objects';
import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch';
import { RequestStatus } from '../../../../../src/plugins/inspector';
import { getInspectResponse } from '../../../observability/server';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { FieldDescriptor, IndexPatternsFetcher } from '../../../../../../src/plugins/data/server';
import { UptimeESClient } from '../lib';
import { savedObjectsAdapter } from '../saved_objects';
import { savedObjectsAdapter } from '../saved_objects/saved_objects';

export interface IndexPatternTitleAndFields {
title: string;
Expand Down
82 changes: 0 additions & 82 deletions x-pack/plugins/uptime/server/lib/saved_objects.ts

This file was deleted.

53 changes: 53 additions & 0 deletions x-pack/plugins/uptime/server/lib/saved_objects/saved_objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 {
SavedObjectsErrorHelpers,
SavedObjectsServiceSetup,
} from '../../../../../../src/core/server';
import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { DynamicSettings } from '../../../common/runtime_types';
import { UMSavedObjectsQueryFn } from '../adapters';
import { UptimeConfig } from '../../config';
import { settingsObjectId, umDynamicSettings } from './uptime_settings';
import { syntheticsMonitor } from './synthetics_monitor';

export interface UMSavedObjectsAdapter {
config: UptimeConfig;
getUptimeDynamicSettings: UMSavedObjectsQueryFn<DynamicSettings>;
setUptimeDynamicSettings: UMSavedObjectsQueryFn<void, DynamicSettings>;
}

export const registerUptimeSavedObjects = (savedObjectsService: SavedObjectsServiceSetup) => {
savedObjectsService.registerType(umDynamicSettings);
savedObjectsService.registerType(syntheticsMonitor);
};

export const savedObjectsAdapter: UMSavedObjectsAdapter = {
config: null,
getUptimeDynamicSettings: async (client): Promise<DynamicSettings> => {
try {
const obj = await client.get<DynamicSettings>(umDynamicSettings.name, settingsObjectId);
return obj?.attributes ?? DYNAMIC_SETTINGS_DEFAULTS;
} catch (getErr) {
const config = savedObjectsAdapter.config;
if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) {
if (config?.index) {
return { ...DYNAMIC_SETTINGS_DEFAULTS, heartbeatIndices: config.index };
}
return DYNAMIC_SETTINGS_DEFAULTS;
}
throw getErr;
}
},
setUptimeDynamicSettings: async (client, settings): Promise<void> => {
await client.create(umDynamicSettings.name, settings, {
id: settingsObjectId,
overwrite: true,
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 { SavedObjectsType } from 'kibana/server';
import { i18n } from '@kbn/i18n';

export const syntheticsMonitorType = 'synthetics-monitor';

export const syntheticsMonitor: SavedObjectsType = {
name: syntheticsMonitorType,
hidden: false,
namespaceType: 'single',
mappings: {
dynamic: false,
properties: {
name: {
type: 'keyword',
},
id: {
type: 'keyword',
},
type: {
type: 'keyword',
},
urls: {
type: 'keyword',
},
tags: {
type: 'keyword',
},
},
},
management: {
importableAndExportable: true,
icon: 'uptimeApp',
getTitle: (savedObject) =>
savedObject.attributes.name +
' - ' +
i18n.translate('xpack.uptime.syntheticsMonitors', {
defaultMessage: 'Uptime - Monitor',
}),
},
};
46 changes: 46 additions & 0 deletions x-pack/plugins/uptime/server/lib/saved_objects/uptime_settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 { SavedObjectsType } from 'kibana/server';
import { i18n } from '@kbn/i18n';
export const settingsObjectType = 'uptime-dynamic-settings';
export const settingsObjectId = 'uptime-dynamic-settings-singleton';

export const umDynamicSettings: SavedObjectsType = {
name: settingsObjectType,
hidden: false,
namespaceType: 'single',
mappings: {
dynamic: false,
properties: {
/* Leaving these commented to make it clear that these fields exist, even though we don't want them indexed.
When adding new fields please add them here. If they need to be searchable put them in the uncommented
part of properties.
heartbeatIndices: {
type: 'keyword',
},
certAgeThreshold: {
type: 'long',
},
certExpirationThreshold: {
type: 'long',
},
defaultConnectors: {
type: 'keyword',
},
*/
},
},
management: {
importableAndExportable: true,
icon: 'uptimeApp',
getTitle: () =>
i18n.translate('xpack.uptime.uptimeSettings.index', {
defaultMessage: 'Uptime Settings - Index',
}),
},
};
6 changes: 4 additions & 2 deletions x-pack/plugins/uptime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { uptimeRuleFieldMap } from '../common/rules/uptime_rule_field_map';
import { initServerWithKibana } from './kibana.index';
import { KibanaTelemetryAdapter, UptimeCorePlugins } from './lib/adapters';
import { savedObjectsAdapter, umDynamicSettings } from './lib/saved_objects';
import { registerUptimeSavedObjects, savedObjectsAdapter } from './lib/saved_objects/saved_objects';
import { mappingFromFieldMap } from '../../rule_registry/common/mapping_from_field_map';
import { Dataset } from '../../rule_registry/server';
import { UptimeConfig } from './config';
Expand Down Expand Up @@ -59,7 +59,9 @@ export class Plugin implements PluginType {
ruleDataClient,
this.logger
);
core.savedObjects.registerType(umDynamicSettings);

registerUptimeSavedObjects(core.savedObjects);

KibanaTelemetryAdapter.registerUsageCollector(
plugins.usageCollection,
() => this.savedObjectsClient
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PathReporter } from 'io-ts/lib/PathReporter';
import { UMServerLibs } from '../lib/lib';
import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types';
import { UMRestApiRouteFactory } from '.';
import { savedObjectsAdapter } from '../lib/saved_objects';
import { savedObjectsAdapter } from '../lib/saved_objects/saved_objects';
import {
VALUE_MUST_BE_GREATER_THAN_ZERO,
VALUE_MUST_BE_AN_INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/uptime/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FtrProviderContext } from '../../../ftr_provider_context';
import {
settingsObjectId,
settingsObjectType,
} from '../../../../../plugins/uptime/server/lib/saved_objects';
} from '../../../../../plugins/uptime/server/lib/saved_objects/uptime_settings';

export default function ({ getService, loadTestFile }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/functional/apps/uptime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FtrProviderContext } from '../../ftr_provider_context';
import {
settingsObjectId,
settingsObjectType,
} from '../../../../plugins/uptime/server/lib/saved_objects';
} from '../../../../plugins/uptime/server/lib/saved_objects/uptime_settings';

const ARCHIVE = 'x-pack/test/functional/es_archives/uptime/full_heartbeat';

Expand Down

0 comments on commit 4706a8b

Please sign in to comment.