Skip to content

Commit

Permalink
[Stack Monitoring] Converts logstash api routes to typescript (#131946)
Browse files Browse the repository at this point in the history
* converts logstash api routes to typescript

* fixes register routes function names

* fixes cluster and node pipelines routes

* fixes types
  • Loading branch information
crespocarlos authored May 19, 2022
1 parent 178773f commit fa607d1
Show file tree
Hide file tree
Showing 24 changed files with 457 additions and 339 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/monitoring/common/http_api/logstash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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.
*/

export * from './post_logstash_node';
export * from './post_logstash_nodes';
export * from './post_logstash_overview';
export * from './post_logstash_pipeline';
export * from './post_logstash_pipeline_cluster_ids';
export * from './post_logstash_cluster_pipelines';
export * from './post_logstash_node_pipelines';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT, paginationRT, sortingRT } from '../shared';

export const postLogstashClusterPipelinesRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postLogstashClusterPipelinesRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
sort: sortingRT,
queryText: rt.string,
}),
rt.type({
timeRange: timeRangeRT,
pagination: paginationRT,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postLogstashNodeRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
logstashUuid: rt.string,
});

export const postLogstashNodeRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
is_advanced: rt.boolean,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT, paginationRT, sortingRT } from '../shared';

export const postLogstashNodePipelinesRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
logstashUuid: rt.string,
});

export const postLogstashNodePipelinesRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
sort: sortingRT,
queryText: rt.string,
}),
rt.type({
timeRange: timeRangeRT,
pagination: paginationRT,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postLogstashNodesRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postLogstashNodesRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postLogstashOverviewRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postLogstashOverviewRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT } from '../shared';

export const postLogstashPipelineRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
pipelineId: rt.string,
pipelineHash: rt.union([rt.string, rt.undefined]),
});

export const postLogstashPipelineRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.partial({
detailVertexId: rt.string,
}),
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 * as rt from 'io-ts';
import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared';

export const postLogstashPipelineClusterIdsRequestParamsRT = rt.type({
clusterUuid: clusterUuidRT,
});

export const postLogstashPipelineClusterIdsRequestPayloadRT = rt.intersection([
rt.partial({
ccs: ccsRT,
}),
rt.type({
timeRange: timeRangeRT,
}),
]);
16 changes: 9 additions & 7 deletions x-pack/plugins/monitoring/common/types/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export interface ElasticsearchIndexStats {
};
}

export interface ElasticsearchLogstashStatePipeline {
representation?: {
graph?: {
vertices?: ElasticsearchSourceLogstashPipelineVertex[];
};
};
}

export interface ElasticsearchLegacySource {
timestamp: string;
cluster_uuid: string;
Expand Down Expand Up @@ -204,13 +212,7 @@ export interface ElasticsearchLegacySource {
expiry_date_in_millis?: number;
};
logstash_state?: {
pipeline?: {
representation?: {
graph?: {
vertices?: ElasticsearchSourceLogstashPipelineVertex[];
};
};
};
pipeline?: ElasticsearchLogstashStatePipeline;
};
logstash_stats?: {
timestamp?: string;
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/monitoring/server/lib/details/get_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export type SimpleMetricDescriptor = string;

export type MetricDescriptor = SimpleMetricDescriptor | NamedMetricDescriptor;

export function isNamedMetricDescriptor(
metricDescriptor: MetricDescriptor
): metricDescriptor is NamedMetricDescriptor {
return (metricDescriptor as NamedMetricDescriptor).name !== undefined;
}

// TODO: Switch to an options object argument here
export async function getMetrics(
req: LegacyRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { get } from 'lodash';
import { getLogstashForClusters } from './get_logstash_for_clusters';
import { LegacyRequest } from '../../types';

Expand All @@ -20,7 +19,7 @@ import { LegacyRequest } from '../../types';
*/
export function getClusterStatus(req: LegacyRequest, { clusterUuid }: { clusterUuid: string }) {
const clusters = [{ cluster_uuid: clusterUuid }];
return getLogstashForClusters(req, clusters).then((clusterStatus) =>
get(clusterStatus, '[0].stats')
return getLogstashForClusters(req, clusters).then(
(clusterStatus) => clusterStatus && clusterStatus[0]?.stats
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* 2.0.
*/

export const metricSets = {
import { MetricDescriptor } from '../../../../lib/details/get_metrics';

export const metricSets: {
advanced: MetricDescriptor[];
overview: MetricDescriptor[];
} = {
advanced: [
{
keys: ['logstash_node_cpu_utilization', 'logstash_node_cgroup_quota'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* 2.0.
*/

export const metricSet = [
import { SimpleMetricDescriptor } from '../../../../lib/details/get_metrics';

export const metricSet: SimpleMetricDescriptor[] = [
'logstash_cluster_events_input_rate',
'logstash_cluster_events_output_rate',
'logstash_cluster_events_latency',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,33 @@
* 2.0.
*/

import { schema } from '@kbn/config-schema';
import {
postLogstashNodeRequestParamsRT,
postLogstashNodeRequestPayloadRT,
} from '../../../../../common/http_api/logstash';
import { getNodeInfo } from '../../../../lib/logstash/get_node_info';
import { handleError } from '../../../../lib/errors';
import { getMetrics } from '../../../../lib/details/get_metrics';
import {
getMetrics,
isNamedMetricDescriptor,
NamedMetricDescriptor,
} from '../../../../lib/details/get_metrics';
import { metricSets } from './metric_set_node';
import { MonitoringCore } from '../../../../types';
import { createValidationFunction } from '../../../../lib/create_route_validation_function';

const { advanced: metricSetAdvanced, overview: metricSetOverview } = metricSets;

/*
* Logstash Node route.
*/
export function logstashNodeRoute(server) {
/**
* Logstash Node request.
*
* This will fetch all data required to display a Logstash Node page.
*
* The current details returned are:
*
* - Logstash Node Summary (Status)
* - Metrics
*/
export function logstashNodeRoute(server: MonitoringCore) {
const validateParams = createValidationFunction(postLogstashNodeRequestParamsRT);
const validateBody = createValidationFunction(postLogstashNodeRequestPayloadRT);

server.route({
method: 'POST',
method: 'post',
path: '/api/monitoring/v1/clusters/{clusterUuid}/logstash/node/{logstashUuid}',
config: {
validate: {
params: schema.object({
clusterUuid: schema.string(),
logstashUuid: schema.string(),
}),
body: schema.object({
ccs: schema.maybe(schema.string()),
timeRange: schema.object({
min: schema.string(),
max: schema.string(),
}),
is_advanced: schema.boolean(),
}),
},
validate: {
params: validateParams,
body: validateBody,
},
async handler(req) {
const config = server.config;
Expand All @@ -58,11 +45,17 @@ export function logstashNodeRoute(server) {
metricSet = metricSetOverview;
// set the cgroup option if needed
const showCgroupMetricsLogstash = config.ui.container.logstash.enabled;
const metricCpu = metricSet.find((m) => m.name === 'logstash_node_cpu_metric');
if (showCgroupMetricsLogstash) {
metricCpu.keys = ['logstash_node_cgroup_quota_as_cpu_utilization'];
} else {
metricCpu.keys = ['logstash_node_cpu_utilization'];
const metricCpu = metricSet.find(
(m): m is NamedMetricDescriptor =>
isNamedMetricDescriptor(m) && m.name === 'logstash_node_cpu_metric'
);

if (metricCpu) {
if (showCgroupMetricsLogstash) {
metricCpu.keys = ['logstash_node_cgroup_quota_as_cpu_utilization'];
} else {
metricCpu.keys = ['logstash_node_cpu_utilization'];
}
}
}

Expand Down
Loading

0 comments on commit fa607d1

Please sign in to comment.