Skip to content

Commit

Permalink
extract buildKibanaInfo for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
klacabane committed Dec 28, 2021
1 parent fd911dd commit c434058
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/monitoring/common/types/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,11 @@ export interface ElasticsearchMetricbeatSource {
kibana?: {
stats?: {
name?: string;
index?: string;
status?: string;
transport_address?: string;
concurrent_connections?: number;
snapshot?: boolean;
host?: {
name?: string;
};
Expand Down
28 changes: 28 additions & 0 deletions x-pack/plugins/monitoring/server/lib/kibana/build_kibana_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ElasticsearchResponseHit } from '../../../common/types/es';

export interface KibanaInfo {
transport_address?: string;
name?: string;
index?: string;
host?: string;
uuid?: string;
status?: string;
snapshot?: boolean;
version?: string;
}

export const buildKibanaInfo = (hit: ElasticsearchResponseHit): KibanaInfo => {
const source = hit._source;
if (source.kibana_stats) return source.kibana_stats.kibana as KibanaInfo;

return {
name: source.kibana?.stats?.name,
host: source.kibana?.stats?.host?.name,
status: source.kibana?.stats?.status,
transport_address: source.kibana?.stats?.transport_address,
uuid: source.service?.id,
snapshot: source.kibana?.stats?.snapshot,
index: source.kibana?.stats?.index,
version: source.service?.version,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import { checkParam, MissingRequiredError } from '../error_missing_required';
import { calculateAvailability } from '../calculate_availability';
import { LegacyRequest } from '../../types';
import { ElasticsearchResponse } from '../../../common/types/es';
import { buildKibanaInfo } from './build_kibana_info';

export function handleResponse(resp: ElasticsearchResponse) {
const hit = resp.hits?.hits[0];
const legacySource = hit?._source.kibana_stats;
const mbSource = hit?._source.kibana?.stats;
const kibana = mbSource ?? legacySource?.kibana;
const availabilityTimestamp = hit?._source['@timestamp'] ?? legacySource?.timestamp;
if (!availabilityTimestamp) {
throw new MissingRequiredError('timestamp');
}
return merge(kibana, {

return merge(buildKibanaInfo(hit!), {
availability: calculateAvailability(availabilityTimestamp),
os_memory_free: mbSource?.os?.memory?.free_in_bytes ?? legacySource?.os?.memory?.free_in_bytes,
uptime: mbSource?.process?.uptime?.ms ?? legacySource?.process?.uptime_in_millis,
version: hit?._source.service?.version ?? legacySource?.kibana?.version,
});
}

Expand All @@ -50,6 +50,7 @@ export function getKibanaInfo(
'hits.hits._source.kibana.stats.process.uptime.ms',
'hits.hits._source.kibana_stats.timestamp',
'hits.hits._source.@timestamp',
'hits.hits._source.service.id',
'hits.hits._source.service.version',
],
body: {
Expand Down
22 changes: 1 addition & 21 deletions x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ import { calculateAvailability } from '../calculate_availability';
import { KibanaMetric } from '../metrics';
import { LegacyRequest } from '../../types';
import { ElasticsearchResponse, ElasticsearchResponseHit } from '../../../common/types/es';

interface KibanaInfo {
transport_address?: string;
name?: string;
host?: string;
uuid?: string;
status?: string;
}
import { KibanaInfo, buildKibanaInfo } from './build_kibana_info';

interface Kibana {
process?: {
Expand Down Expand Up @@ -119,19 +112,6 @@ export async function getKibanas(
const response: ElasticsearchResponse = await callWithRequest(req, 'search', params);
const instances = response.hits?.hits ?? [];

const buildKibanaInfo = (hit: ElasticsearchResponseHit): KibanaInfo => {
const source = hit._source;
if (source.kibana_stats) return source.kibana_stats.kibana as KibanaInfo;

return {
name: source.kibana?.stats?.name,
host: source.kibana?.stats?.host?.name,
status: source.kibana?.stats?.status,
transport_address: source.kibana?.stats?.transport_address,
uuid: source.service?.id,
};
};

return instances.map((hit: ElasticsearchResponseHit) => {
const legacyStats = hit._source.kibana_stats;
const mbStats = hit._source.kibana?.stats;
Expand Down

0 comments on commit c434058

Please sign in to comment.