Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP API versioned types in usage collection plugin #152875

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/plugins/usage_collection/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting the types as aliased to the unversioned type definitions allows us to use:
Stats.v1.StatsHTTPBodyTyped for example

export * as Stats from './stats';
export * as UiCounters from './ui_counters';
export * as UsageCounters from './usage_counters';
219 changes: 219 additions & 0 deletions src/plugins/usage_collection/common/types/stats/core_metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

/**
* The types are exact duplicates of the ops metrics types declared in core needed for reporting in the stats api.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're duplicating the types from core so that we'll detect any changes to the original metrics.

* We use a copy of these to detect changes in the ops metrics reported from core
* See packages/core/metrics/core-metrics-server/index.ts
*/

/**
* an IntervalHistogram object that samples and reports the event loop delay over time.
* The delays will be reported in milliseconds.
* See {@link IntervalHistogram}
* @public
*/
export interface IntervalHistogram {
// The first timestamp the interval timer kicked in for collecting data points.
fromTimestamp: string;
// Last timestamp the interval timer kicked in for collecting data points.
lastUpdatedAt: string;
// The minimum recorded event loop delay.
min: number;
// The maximum recorded event loop delay.
max: number;
// The mean of the recorded event loop delays.
mean: number;
// The number of times the event loop delay exceeded the maximum 1 hour event loop delay threshold.
exceeds: number;
// The standard deviation of the recorded event loop delays.
stddev: number;
// An object detailing the accumulated percentile distribution.
percentiles: {
// 50th percentile of delays of the collected data points.
50: number;
// 75th percentile of delays of the collected data points.
75: number;
// 95th percentile of delays of the collected data points.
95: number;
// 99th percentile of delays of the collected data points.
99: number;
};
}

/**
* See {@link ElasticsearchClientProtocol}
* Protocol(s) used by the Elasticsearch Client
* @public
*/

export type ElasticsearchClientProtocol = 'none' | 'http' | 'https' | 'mixed';

/**
* See {@link ElasticsearchClientsMetrics}
* Metrics related to the elasticsearch clients
* @public
*/
export interface ElasticsearchClientsMetrics {
/** Total number of active sockets (all nodes, all connections) */
totalActiveSockets: number;
/** Total number of available sockets (alive but idle, all nodes, all connections) */
totalIdleSockets: number;
/** Total number of queued requests (all nodes, all connections) */
totalQueuedRequests: number;
}

/**
* See {@link OpsProcessMetrics}
* Process related metrics
* @public
*/
export interface OpsProcessMetrics {
/** pid of the kibana process */
pid: number;
/** process memory usage */
memory: {
/** heap memory usage */
heap: {
/** total heap available */
total_in_bytes: number;
/** used heap */
used_in_bytes: number;
/** v8 heap size limit */
size_limit: number;
};
/** node rss */
resident_set_size_in_bytes: number;
};
/** mean event loop delay since last collection*/
event_loop_delay: number;
/** node event loop delay histogram since last collection */
event_loop_delay_histogram: IntervalHistogram;
/** uptime of the kibana process */
uptime_in_millis: number;
}

/**
* See {@link OpsOsMetrics}
* OS related metrics
* @public
*/
export interface OpsOsMetrics {
/** The os platform */
platform: NodeJS.Platform;
/** The os platform release, prefixed by the platform name */
platformRelease: string;
/** The os distrib. Only present for linux platforms */
distro?: string;
/** The os distrib release, prefixed by the os distrib. Only present for linux platforms */
distroRelease?: string;
/** cpu load metrics */
load: {
/** load for last minute */
'1m': number;
/** load for last 5 minutes */
'5m': number;
/** load for last 15 minutes */
'15m': number;
};
/** system memory usage metrics */
memory: {
/** total memory available */
total_in_bytes: number;
/** current free memory */
free_in_bytes: number;
/** current used memory */
used_in_bytes: number;
};
/** the OS uptime */
uptime_in_millis: number;

/** cpu accounting metrics, undefined when not running in a cgroup */
cpuacct?: {
/** name of this process's cgroup */
control_group: string;
/** cpu time used by this process's cgroup */
usage_nanos: number;
};

/** cpu cgroup metrics, undefined when not running in a cgroup */
cpu?: {
/** name of this process's cgroup */
control_group: string;
/** the length of the cfs period */
cfs_period_micros: number;
/** total available run-time within a cfs period */
cfs_quota_micros: number;
/** current stats on the cfs periods */
stat: {
/** number of cfs periods that elapsed */
number_of_elapsed_periods: number;
/** number of times the cgroup has been throttled */
number_of_times_throttled: number;
/** total amount of time the cgroup has been throttled for */
time_throttled_nanos: number;
};
};
}

/**
* {@link OpsServerMetrics}
* server related metrics
* @public
*/
export interface OpsServerMetrics {
/** server response time stats */
response_times: {
/** average response time */
avg_in_millis: number;
/** maximum response time */
max_in_millis: number;
};
/** server requests stats */
requests: {
/** number of disconnected requests since startup */
disconnects: number;
/** total number of requests handled since startup */
total: number;
/** number of request handled per response status code */
statusCodes: Record<number, number>;
};
/** number of current concurrent connections to the server */
concurrent_connections: number;
}

/**
* {@link OpsMetrics}
* Regroups metrics gathered by all the collectors.
* This contains metrics about the os/runtime, the kibana process and the http server.

* @public
*/
export interface OpsMetrics {
/** Time metrics were recorded at. */
collected_at: Date;
/**
* Metrics related to the elasticsearch client
*/
elasticsearch_client: ElasticsearchClientsMetrics;
/**
* Process related metrics.
* @remarks processes field preferred
*/
process: OpsProcessMetrics;
/** Process related metrics. Reports an array of objects for each kibana pid.*/
processes: OpsProcessMetrics[];
/** OS related metrics */
os: OpsOsMetrics;
/** server response time stats */
response_times: OpsServerMetrics['response_times'];
/** server requests stats */
requests: OpsServerMetrics['requests'];
/** number of current concurrent connections to the server */
concurrent_connections: OpsServerMetrics['concurrent_connections'];
}
10 changes: 10 additions & 0 deletions src/plugins/usage_collection/common/types/stats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * as v1 from './v1';
export * from './latest';
9 changes: 9 additions & 0 deletions src/plugins/usage_collection/common/types/stats/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
export * as OpsMetricsCopy from './core_metrics';
export * from './v1';
70 changes: 70 additions & 0 deletions src/plugins/usage_collection/common/types/stats/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import * as OpsMetricsCopy from './core_metrics';

/** v1 types Start */
/**
* stats query v1
* @remarks exclude_usage is always interpreted as true. query param retained to prevent breaking changes to existing consumers.
*/
export interface StatsHTTPQuery {
extended?: boolean | '';
legacy?: boolean | '';
exclude_usage?: boolean | '';
}

export interface UsageObject {
kibana?: UsageObject;
xpack?: UsageObject;
[key: string]: unknown | UsageObject;
}

export interface ClusterUuidLegacy {
clusterUuid?: string;
}
export interface ClusterUuid {
cluster_uuid?: string;
}
/**
* Extended usage stats.
* @remarks
* Legacy implementation used to conditionally include kibana usage metrics
* as of https://github.com/elastic/kibana/pull/151082, usage is no longer reported
* and set to an empty object to prevent breaking changes to existing consumers.
*/
export interface ExtendedStats {
[key: string]: unknown | UsageObject;
clusterUuid?: string; // camel case if legacy === true
cluster_uuid?: string; // snake_case if legacy === false
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
}
/**
* OpsMetrics: aliased from a duplicate of core's OpsMetrics types
* @remarks the alternative to creating a local copy of the OpsMetrics types is to declare them as `unknown` and assume validation happens elsewhere.
* The disadvantage is that any changes made to the original OpsMetrics will be passed through without needing to update the API types.
*/
export type LastOpsMetrics = OpsMetricsCopy.OpsMetrics;

export type KibanaServiceStatus = Record<string, string>;
/** explicitly typed stats for kibana */
export interface KibanaStats {
// kibana
kibana: {
uuid: string;
name: string;
index: string;
host: string;
locale: string;
transport_address: string;
version: string;
snapshot: boolean;
status: string;
};
}
/** Stats response body */
export type StatsHTTPBodyTyped = LastOpsMetrics | KibanaStats | ExtendedStats;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly declare (with appropriate types) the full response body from the stats api

10 changes: 10 additions & 0 deletions src/plugins/usage_collection/common/types/ui_counters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * as v1 from './v1';
export * from './latest';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * as v1 from './v1';
Loading