Skip to content

Commit

Permalink
[Uptime] Add ts support for es queries aggs (elastic#83331)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
shahzad31 and kibanamachine authored Nov 23, 2020
1 parent 26e9dbf commit 22fb000
Show file tree
Hide file tree
Showing 67 changed files with 874 additions and 993 deletions.
21 changes: 14 additions & 7 deletions x-pack/plugins/uptime/common/runtime_types/monitor/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import * as t from 'io-ts';

// IO type for validation
export const MonitorErrorType = t.partial({
code: t.number,
message: t.string,
type: t.string,
});
export const PingErrorType = t.intersection([
t.partial({
code: t.string,
id: t.string,
stack_trace: t.string,
type: t.string,
}),
t.type({
// this is _always_ on the error field
message: t.string,
}),
]);

// Typescript type for type checking
export type MonitorError = t.TypeOf<typeof MonitorErrorType>;
export type PingError = t.TypeOf<typeof PingErrorType>;

export const MonitorDetailsType = t.intersection([
t.type({ monitorId: t.string }),
t.partial({ error: MonitorErrorType, timestamp: t.string, alerts: t.unknown }),
t.partial({ error: PingErrorType, timestamp: t.string, alerts: t.unknown }),
]);
export type MonitorDetails = t.TypeOf<typeof MonitorDetailsType>;
14 changes: 2 additions & 12 deletions x-pack/plugins/uptime/common/runtime_types/ping/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import * as t from 'io-ts';
import { DateRangeType } from '../common';
import { PingErrorType } from '../monitor';

export const HttpResponseBodyType = t.partial({
bytes: t.number,
Expand Down Expand Up @@ -116,18 +117,7 @@ export const PingType = t.intersection([
ecs: t.partial({
version: t.string,
}),
error: t.intersection([
t.partial({
code: t.string,
id: t.string,
stack_trace: t.string,
type: t.string,
}),
t.type({
// this is _always_ on the error field
message: t.string,
}),
]),
error: PingErrorType,
http: t.partial({
request: t.partial({
body: t.partial({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const EmptyStateError = ({ errors }: EmptyStateErrorProps) => {
<Fragment>
{!unauthorized &&
errors.map((error: IHttpFetchError) => (
<p key={error.body.message}>{error.body.message || error.message}</p>
<p key={error.body.message || error.message}>
{error.body.message || error.message}
</p>
))}
</Fragment>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import React from 'react';
import moment from 'moment';
import { BrowserRouter as Router } from 'react-router-dom';
import { MostRecentError } from '../most_recent_error';
import { MonitorDetails, MonitorError } from '../../../../../../common/runtime_types';
import { MonitorDetails, PingError } from '../../../../../../common/runtime_types';

describe('MostRecentError component', () => {
let monitorDetails: MonitorDetails;
let monitorError: MonitorError;
let monitorError: PingError;

beforeAll(() => {
moment.prototype.fromNow = jest.fn(() => '5 days ago');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { i18n } from '@kbn/i18n';
import { MonitorPageLink } from '../../../common/monitor_page_link';
import { useGetUrlParams } from '../../../../hooks';
import { stringifyUrlParams } from '../../../../lib/helper/stringify_url_params';
import { MonitorError } from '../../../../../common/runtime_types';
import { PingError } from '../../../../../common/runtime_types';

interface MostRecentErrorProps {
/**
* error returned from API for monitor details
*/
error: MonitorError | undefined;
error: PingError | undefined;

/**
* monitorId to be used for link to detail page
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/uptime/public/state/actions/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { createAction } from 'redux-actions';
import { MonitorDetailsActionPayload } from './types';
import { MonitorError } from '../../../common/runtime_types';
import { PingError } from '../../../common/runtime_types';
import { MonitorLocations } from '../../../common/runtime_types';
import { QueryParams } from './types';
import { createAsyncAction } from './utils';
Expand All @@ -17,7 +17,7 @@ export interface MonitorLocationsPayload extends QueryParams {

export interface MonitorDetailsState {
monitorId: string;
error: MonitorError;
error: PingError;
}

export const getMonitorDetailsAction = createAsyncAction<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ import {
SavedObjectsClientContract,
ISavedObjectsRepository,
IScopedClusterClient,
ElasticsearchClient,
} from 'src/core/server';
import { UMKibanaRoute } from '../../../rest_api';
import { PluginSetupContract } from '../../../../../features/server';
import { DynamicSettings } from '../../../../common/runtime_types';
import { MlPluginSetup as MlSetup } from '../../../../../ml/server';
import { UptimeESClient } from '../../lib';

export type UMElasticsearchQueryFn<P, R = any> = (
params: {
callES: ElasticsearchClient;
uptimeEsClient: UptimeESClient;
esClient?: IScopedClusterClient;
dynamicSettings: DynamicSettings;
} & P
) => Promise<R>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { PageViewParams, UptimeTelemetry, Usage } from './types';
import { savedObjectsAdapter } from '../../saved_objects';
import { UptimeESClient } from '../../lib';

interface UptimeTelemetryCollector {
[key: number]: UptimeTelemetry;
Expand Down Expand Up @@ -131,7 +132,7 @@ export class KibanaTelemetryAdapter {
}

public static async countNoOfUniqueMonitorAndLocations(
callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | ElasticsearchClient,
callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | UptimeESClient,
savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract
) {
const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,19 @@ describe('status check alert', () => {
expect(mockGetter.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": undefined,
"locations": Array [],
"numTimes": 5,
"timerange": Object {
"from": "now-15m",
"to": "now",
},
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -152,20 +151,19 @@ describe('status check alert', () => {
expect(mockGetter.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": undefined,
"locations": Array [],
"numTimes": 5,
"timerange": Object {
"from": "now-15m",
"to": "now",
},
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -333,13 +331,6 @@ describe('status check alert', () => {
expect(mockGetter.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": Object {
"bool": Object {
"filter": Array [
Expand Down Expand Up @@ -506,6 +497,12 @@ describe('status check alert', () => {
"from": "now-15m",
"to": "now",
},
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -571,13 +568,6 @@ describe('status check alert', () => {
expect(mockGetter.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": Object {
"bool": Object {
"filter": Array [
Expand Down Expand Up @@ -614,6 +604,12 @@ describe('status check alert', () => {
"from": "now-30h",
"to": "now",
},
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -758,17 +754,16 @@ describe('status check alert', () => {
expect(mockAvailability.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": "{\\"bool\\":{\\"filter\\":[{\\"bool\\":{\\"should\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"url.port\\":12349}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"should\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"url.port\\":5601}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"url.port\\":443}}],\\"minimum_should_match\\":1}}],\\"minimum_should_match\\":1}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"filter\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"observer.geo.name\\":\\"harrisburg\\"}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"filter\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"monitor.type\\":\\"http\\"}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"should\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"tags\\":\\"unsecured\\"}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"should\\":[{\\"bool\\":{\\"should\\":[{\\"match\\":{\\"tags\\":\\"containers\\"}}],\\"minimum_should_match\\":1}},{\\"bool\\":{\\"should\\":[{\\"match_phrase\\":{\\"tags\\":\\"org:google\\"}}],\\"minimum_should_match\\":1}}],\\"minimum_should_match\\":1}}],\\"minimum_should_match\\":1}}]}}]}}]}}",
"range": 35,
"rangeUnit": "d",
"threshold": "99.34",
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -813,17 +808,16 @@ describe('status check alert', () => {
expect(mockAvailability.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": "{\\"bool\\":{\\"should\\":[{\\"exists\\":{\\"field\\":\\"ur.port\\"}}],\\"minimum_should_match\\":1}}",
"range": 23,
"rangeUnit": "w",
"threshold": "90",
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down Expand Up @@ -857,17 +851,16 @@ describe('status check alert', () => {
expect(mockAvailability.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"callES": [MockFunction],
"dynamicSettings": Object {
"certAgeThreshold": 730,
"certExpirationThreshold": 30,
"defaultConnectors": Array [],
"heartbeatIndices": "heartbeat-8*",
},
"filters": undefined,
"range": 23,
"rangeUnit": "w",
"threshold": "90",
"uptimeEsClient": Object {
"baseESClient": [MockFunction],
"count": [Function],
"getSavedObjectsClient": [Function],
"search": [Function],
},
},
]
`);
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/uptime/server/lib/alerts/duration_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const durationAnomalyAlertFactory: UptimeAlertTypeFactory = (_server, _li
context: [],
state: [...durationAnomalyTranslations.actionVariables, ...commonStateTranslations],
},
async executor({ options, esClient, savedObjectsClient, dynamicSettings }) {
async executor({ options, uptimeEsClient, savedObjectsClient, dynamicSettings }) {
const {
services: { alertInstanceFactory },
state,
Expand All @@ -96,8 +96,7 @@ export const durationAnomalyAlertFactory: UptimeAlertTypeFactory = (_server, _li

if (foundAnomalies) {
const monitorInfo = await getLatestMonitor({
dynamicSettings,
callES: esClient,
uptimeEsClient,
dateStart: 'now-15m',
dateEnd: 'now',
monitorId: params.monitorId,
Expand Down
Loading

0 comments on commit 22fb000

Please sign in to comment.