-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Adds nodes feature usage stats merged into cluster_stats.nodes when usage collection is local
- Loading branch information
1 parent
95da850
commit 6795a4c
Showing
7 changed files
with
299 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 { getNodesUsage } from './get_nodes_usage'; | ||
import { TIMEOUT } from './constants'; | ||
|
||
const mockedNodesFetchResponse = { | ||
cluster_name: 'test cluster', | ||
nodes: { | ||
some_node_id: { | ||
timestamp: 1588617023177, | ||
since: 1588616945163, | ||
rest_actions: { | ||
nodes_usage_action: 1, | ||
create_index_action: 1, | ||
document_get_action: 1, | ||
search_action: 19, | ||
nodes_info_action: 36, | ||
}, | ||
aggregations: { | ||
terms: { | ||
bytes: 2, | ||
}, | ||
scripted_metric: { | ||
other: 7, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
describe('get_nodes_usage', () => { | ||
it('calls fetchNodesUsage', async () => { | ||
const callCluster = jest.fn(); | ||
callCluster.mockResolvedValueOnce(mockedNodesFetchResponse); | ||
await getNodesUsage(callCluster); | ||
expect(callCluster).toHaveBeenCalledWith('transport.request', { | ||
path: '/_nodes/usage', | ||
method: 'GET', | ||
query: { | ||
timeout: TIMEOUT, | ||
}, | ||
}); | ||
}); | ||
it('returns a modified array of node usage data', async () => { | ||
const callCluster = jest.fn(); | ||
callCluster.mockResolvedValueOnce(mockedNodesFetchResponse); | ||
const result = await getNodesUsage(callCluster); | ||
expect(result.nodes).toEqual([ | ||
{ | ||
aggregations: { scripted_metric: { other: 7 }, terms: { bytes: 2 } }, | ||
node_id: 'some_node_id', | ||
rest_actions: { | ||
create_index_action: 1, | ||
document_get_action: 1, | ||
nodes_info_action: 36, | ||
nodes_usage_action: 1, | ||
search_action: 19, | ||
}, | ||
since: 1588616945163, | ||
timestamp: 1588617023177, | ||
}, | ||
]); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 { LegacyAPICaller } 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 NodesFeatureUsageResponse { | ||
cluster_name: string; | ||
nodes: { | ||
[key: string]: NodeObj; | ||
}; | ||
} | ||
|
||
export type NodesUsageGetter = ( | ||
callCluster: LegacyAPICaller | ||
) => Promise<{ nodes: NodeObj[] | Array<{}> }>; | ||
/** | ||
* 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( | ||
callCluster: LegacyAPICaller | ||
): Promise<NodesFeatureUsageResponse> { | ||
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(callCluster); | ||
const transformedNodes = Object.entries(result?.nodes || {}).map(([key, value]) => ({ | ||
...(value as NodeObj), | ||
node_id: key, | ||
})); | ||
return { nodes: transformedNodes }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.