Skip to content

Commit

Permalink
Do not emmit empty status to status Observables
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila committed Nov 10, 2023
1 parent 6c5ffd2 commit 0c0512d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export function calculateHealthStatus(
config: TaskManagerConfig,
shouldRunTasks: boolean,
logger: Logger
): { status: HealthStatus; reason?: string } {
): { status: HealthStatus; reason?: string; isEmpty?: boolean } {
const now = Date.now();

// if stats are empty, return a warning
if (isEmpty(summarizedStats.stats)) {
return { status: HealthStatus.Warning, reason: `no health stats available` };
return { status: HealthStatus.Warning, reason: `no health stats available`, isEmpty: true };
}

// if "hot" health stats are any more stale than monitored_stats_required_freshness
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/task_manager/server/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@kbn/core/server';
import { IClusterClient, DocLinksServiceSetup } from '@kbn/core/server';
import { Observable, Subject } from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { tap, map, filter } from 'rxjs/operators';
import { throttleTime } from 'rxjs/operators';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { Logger, ServiceStatus, ServiceStatusLevels } from '@kbn/core/server';
Expand All @@ -31,6 +31,7 @@ import { calculateHealthStatus } from '../lib/calculate_health_status';
export type MonitoredHealth = RawMonitoringStats & {
id: string;
reason?: string;
isEmpty?: boolean;
status: HealthStatus;
timestamp: string;
};
Expand Down Expand Up @@ -88,15 +89,15 @@ export function healthRoute(params: HealthRouteParams): {

function getHealthStatus(monitoredStats: MonitoringStats) {
const summarizedStats = summarizeMonitoringStats(logger, monitoredStats, config);
const { status, reason } = calculateHealthStatus(
const { status, reason, isEmpty } = calculateHealthStatus(
summarizedStats,
config,
shouldRunTasks,
logger
);
const now = Date.now();
const timestamp = new Date(now).toISOString();
return { id: taskManagerId, timestamp, status, reason, ...summarizedStats };
return { id: taskManagerId, timestamp, status, reason, isEmpty, ...summarizedStats };
}

const serviceStatus$: Subject<TaskManagerServiceStatus> = new Subject<TaskManagerServiceStatus>();
Expand All @@ -114,7 +115,8 @@ export function healthRoute(params: HealthRouteParams): {
}),
// Only calculate the summarized stats (calculates all running averages and evaluates state)
// when needed by throttling down to the requiredHotStatsFreshness
map((stats) => withServiceStatus(getHealthStatus(stats)))
map((stats) => withServiceStatus(getHealthStatus(stats))),
filter(([monitoredHealth]) => !monitoredHealth.isEmpty)
)
.subscribe(([monitoredHealth, serviceStatus]) => {
serviceStatus$.next(serviceStatus);
Expand Down

0 comments on commit 0c0512d

Please sign in to comment.