forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added task runtime stats to health endpoint
- Loading branch information
Showing
18 changed files
with
837 additions
and
243 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
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
155 changes: 155 additions & 0 deletions
155
x-pack/plugins/task_manager/server/monitoring/monitoring_stats_stream.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,155 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { TaskManagerConfig } from '../config'; | ||
import { of, Subject } from 'rxjs'; | ||
import { take, bufferCount } from 'rxjs/operators'; | ||
import { createMonitoringStatsStream, AggregatedStat } from './monitoring_stats_stream'; | ||
import { JsonValue } from 'src/plugins/kibana_utils/common'; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('createMonitoringStatsStream', () => { | ||
const configuration: TaskManagerConfig = { | ||
enabled: true, | ||
max_workers: 10, | ||
index: 'foo', | ||
max_attempts: 9, | ||
poll_interval: 6000000, | ||
max_poll_inactivity_cycles: 10, | ||
request_capacity: 1000, | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
monitored_stats_running_average_window: 50, | ||
}; | ||
|
||
it('returns the initial config used to configure Task Manager', async () => { | ||
return new Promise((resolve) => { | ||
createMonitoringStatsStream(of(), configuration) | ||
.pipe(take(1)) | ||
.subscribe((firstValue) => { | ||
expect(firstValue.stats).toMatchObject({ | ||
configuration: { | ||
value: { | ||
max_workers: 10, | ||
poll_interval: 6000000, | ||
max_poll_inactivity_cycles: 10, | ||
request_capacity: 1000, | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
monitored_stats_running_average_window: 50, | ||
}, | ||
}, | ||
}); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('incrementally updates the stats returned by the endpoint', async () => { | ||
const aggregatedStats$ = new Subject<AggregatedStat>(); | ||
|
||
return new Promise((resolve) => { | ||
createMonitoringStatsStream(aggregatedStats$, configuration) | ||
.pipe(take(3), bufferCount(3)) | ||
.subscribe(([initialValue, secondValue, thirdValue]) => { | ||
expect(initialValue.stats).toMatchObject({ | ||
lastUpdate: expect.any(String), | ||
stats: { | ||
configuration: { | ||
value: { | ||
max_workers: 10, | ||
poll_interval: 6000000, | ||
max_poll_inactivity_cycles: 10, | ||
request_capacity: 1000, | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
monitored_stats_running_average_window: 50, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(secondValue).toMatchObject({ | ||
lastUpdate: expect.any(String), | ||
stats: { | ||
newAggregatedStat: { | ||
timestamp: expect.any(String), | ||
value: { | ||
some: { | ||
complex: { | ||
value: 123, | ||
}, | ||
}, | ||
}, | ||
}, | ||
configuration: { | ||
timestamp: expect.any(String), | ||
value: { | ||
max_workers: 10, | ||
poll_interval: 6000000, | ||
max_poll_inactivity_cycles: 10, | ||
request_capacity: 1000, | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
monitored_stats_running_average_window: 50, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(thirdValue).toMatchObject({ | ||
lastUpdate: expect.any(String), | ||
stats: { | ||
newAggregatedStat: { | ||
timestamp: expect.any(String), | ||
value: { | ||
some: { | ||
updated: { | ||
value: 456, | ||
}, | ||
}, | ||
}, | ||
}, | ||
configuration: { | ||
timestamp: expect.any(String), | ||
value: { | ||
max_workers: 10, | ||
poll_interval: 6000000, | ||
max_poll_inactivity_cycles: 10, | ||
request_capacity: 1000, | ||
monitored_aggregated_stats_refresh_rate: 5000, | ||
monitored_stats_running_average_window: 50, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
aggregatedStats$.next({ | ||
key: 'newAggregatedStat', | ||
value: { | ||
some: { | ||
complex: { | ||
value: 123, | ||
}, | ||
}, | ||
} as JsonValue, | ||
}); | ||
|
||
aggregatedStats$.next({ | ||
key: 'newAggregatedStat', | ||
value: { | ||
some: { | ||
updated: { | ||
value: 456, | ||
}, | ||
}, | ||
} as JsonValue, | ||
}); | ||
|
||
resolve(); | ||
}); | ||
}); | ||
}); |
127 changes: 127 additions & 0 deletions
127
x-pack/plugins/task_manager/server/monitoring/monitoring_stats_stream.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,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { merge, of, Observable } from 'rxjs'; | ||
import { map, scan } from 'rxjs/operators'; | ||
import { set } from '@elastic/safer-lodash-set'; | ||
import { pick } from 'lodash'; | ||
import { Logger } from 'src/core/server'; | ||
import { JsonObject } from 'src/plugins/kibana_utils/common'; | ||
import { TaskManager } from '../task_manager'; | ||
import { createWorkloadAggregator, WorkloadStat } from './workload_statistics'; | ||
import { createTaskRunAggregator, summarizeTaskRunStat, TaskRunStat } from './task_run_statistics'; | ||
import { TaskManagerConfig } from '../config'; | ||
import { AggregatedStatProvider } from './runtime_statistics_aggregator'; | ||
|
||
export { AggregatedStatProvider, AggregatedStat } from './runtime_statistics_aggregator'; | ||
|
||
const CONFIG_FIELDS_TO_EXPOSE = [ | ||
'max_workers', | ||
'poll_interval', | ||
'request_capacity', | ||
'max_poll_inactivity_cycles', | ||
'monitored_aggregated_stats_refresh_rate', | ||
'monitored_stats_running_average_window', | ||
] as const; | ||
|
||
type ConfigStat = Pick<TaskManagerConfig, typeof CONFIG_FIELDS_TO_EXPOSE[number]>; | ||
|
||
export interface MonitoringStats { | ||
lastUpdate: string; | ||
stats: { | ||
configuration: { | ||
timestamp: string; | ||
value: ConfigStat; | ||
}; | ||
workload?: { | ||
timestamp: string; | ||
value: WorkloadStat; | ||
}; | ||
runtime?: { | ||
timestamp: string; | ||
value: TaskRunStat; | ||
}; | ||
}; | ||
} | ||
|
||
interface MonitoredStat { | ||
timestamp: string; | ||
value: JsonObject; | ||
} | ||
|
||
export interface RawMonitoringStats { | ||
lastUpdate: string; | ||
stats: Record<string, MonitoredStat>; | ||
} | ||
|
||
export function createAggregators( | ||
taskManager: TaskManager, | ||
config: TaskManagerConfig, | ||
logger: Logger | ||
): AggregatedStatProvider { | ||
return merge( | ||
createTaskRunAggregator(taskManager, config.monitored_stats_running_average_window, logger), | ||
createWorkloadAggregator(taskManager, config.monitored_aggregated_stats_refresh_rate, logger) | ||
); | ||
} | ||
|
||
export function createMonitoringStatsStream( | ||
provider$: AggregatedStatProvider, | ||
config: TaskManagerConfig | ||
): Observable<MonitoringStats> { | ||
const initialStats = initializeStats(new Date().toISOString(), config); | ||
return merge( | ||
// emit the initial stats | ||
of(initialStats), | ||
// emit updated stats whenever a provider updates a specific key on the stats | ||
provider$.pipe( | ||
map(({ key, value }) => { | ||
return { | ||
value: { timestamp: new Date().toISOString(), value }, | ||
key, | ||
}; | ||
}), | ||
scan((monitoringStats: MonitoringStats, { key, value }) => { | ||
// incrementally merge stats as they come in | ||
set(monitoringStats.stats, key, value); | ||
monitoringStats.lastUpdate = new Date().toISOString(); | ||
return monitoringStats; | ||
}, initialStats) | ||
) | ||
); | ||
} | ||
|
||
export function summarizeMonitoringStats({ | ||
lastUpdate, | ||
stats: { runtime, ...otherStats }, | ||
}: MonitoringStats): RawMonitoringStats { | ||
return { | ||
lastUpdate, | ||
stats: { | ||
...((otherStats as unknown) as RawMonitoringStats['stats']), | ||
...(runtime | ||
? { | ||
runtime: { | ||
...runtime, | ||
value: summarizeTaskRunStat(runtime.value), | ||
}, | ||
} | ||
: {}), | ||
}, | ||
}; | ||
} | ||
|
||
const initializeStats = ( | ||
initialisationTimestamp: string, | ||
config: TaskManagerConfig | ||
): MonitoringStats => ({ | ||
lastUpdate: initialisationTimestamp, | ||
stats: { | ||
configuration: { | ||
timestamp: initialisationTimestamp, | ||
value: pick(config, ...CONFIG_FIELDS_TO_EXPOSE) as ConfigStat, | ||
}, | ||
}, | ||
}); |
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.