Skip to content

Commit

Permalink
Adds nodes feature usage stats merged into cluster_stats.nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers committed Jun 26, 2020
1 parent 4845bef commit de25409
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import {
StatsGetter,
StatsCollectionContext,
} from 'src/plugins/telemetry_collection_manager/server';
import { merge } from 'lodash';
import { getClusterInfo, ESClusterInfo } from './get_cluster_info';
import { getClusterStats } from './get_cluster_stats';
import { getKibana, handleKibanaStats, KibanaUsageStats } from './get_kibana';
import { getNodesUsage } from './get_nodes_usage';

/**
* Handle the separate local calls by combining them into a single object response that looks like the
Expand Down Expand Up @@ -67,12 +69,21 @@ export const getLocalStats: StatsGetter<{}, TelemetryLocalStats> = async (

return await Promise.all(
clustersDetails.map(async (clustersDetail) => {
const [clusterInfo, clusterStats, kibana] = await Promise.all([
const [clusterInfo, clusterStats, nodesUsage, kibana] = await Promise.all([
getClusterInfo(callCluster), // cluster info
getClusterStats(callCluster), // cluster stats (not to be confused with cluster _state_)
getNodesUsage(callCluster), // nodes_usage info
getKibana(usageCollection, callCluster),
]);
return handleLocalStats(clusterInfo, clusterStats, kibana, context);
return handleLocalStats(
clusterInfo,
{
...clusterStats,
nodes: merge(clusterStats.nodes, { usage: nodesUsage }),
},
kibana,
context
);
})
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { APICaller } from 'kibana/server';
import { TIMEOUT } from './constants';

export interface NodeAggregation {
[key: string]: number;
}

// we set aggregations as an optional type because it was only added in v7.8.0
export interface NodeObj {
node_id?: string;
timestamp: number;
since: number;
rest_actions: {
[key: string]: number;
};
aggregations?: {
[key: string]: NodeAggregation;
};
}
export interface NodesStats {
total: number;
successful: number;
failed: number;
}
export interface NodesFeatureUsageResponse {
cluster_name: string;
nodes: {
[key: string]: NodeObj;
};
}

export type NodesUsageFetcher = (callCluster: APICaller) => Promise<NodesFeatureUsageResponse>;

export type NodesUsageGetter = (callCluster: APICaller) => Promise<{ nodes: NodeObj[] }>;
/**
* Get the nodes usage data from the connected cluster.
*
* This is the equivalent to GET /_nodes/usage?timeout=30s.
*
* The Nodes usage API was introduced in v6.0.0
*/
export async function fetchNodesUsage<NodesUsageFetcher>(callCluster: APICaller) {
const response = await callCluster('transport.request', {
method: 'GET',
path: '/_nodes/usage',
query: {
timeout: TIMEOUT,
},
});
return response;
}

/**
* Get the nodes usage from the connected cluster
* @param callCluster APICaller
* @returns Object containing array of modified usage information with the node_id nested within the data for that node.
*/
export const getNodesUsage: NodesUsageGetter = async (callCluster) => {
const result = await fetchNodesUsage<NodesUsageFetcher>(callCluster);
const transformedNodes = Object.entries(result.nodes).map(([key, value]) => ({
...(value as NodeObj),
node_id: key,
}));
return { nodes: transformedNodes };
};

0 comments on commit de25409

Please sign in to comment.