Skip to content

Commit

Permalink
move Metrics API to start (#69787) (#70056)
Browse files Browse the repository at this point in the history
* move metrics to start

* update plugins accordingly

* update docs

* update legacy code

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
mshustov and elasticmachine authored Jun 26, 2020
1 parent 3aa59e9 commit 7f46f11
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface CoreSetup<TPluginsStart extends object = object, TStart = unkno
| [getStartServices](./kibana-plugin-core-server.coresetup.getstartservices.md) | <code>StartServicesAccessor&lt;TPluginsStart, TStart&gt;</code> | [StartServicesAccessor](./kibana-plugin-core-server.startservicesaccessor.md) |
| [http](./kibana-plugin-core-server.coresetup.http.md) | <code>HttpServiceSetup &amp; {</code><br/><code> resources: HttpResources;</code><br/><code> }</code> | [HttpServiceSetup](./kibana-plugin-core-server.httpservicesetup.md) |
| [logging](./kibana-plugin-core-server.coresetup.logging.md) | <code>LoggingServiceSetup</code> | [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) |
| [metrics](./kibana-plugin-core-server.coresetup.metrics.md) | <code>MetricsServiceSetup</code> | [MetricsServiceSetup](./kibana-plugin-core-server.metricsservicesetup.md) |
| [savedObjects](./kibana-plugin-core-server.coresetup.savedobjects.md) | <code>SavedObjectsServiceSetup</code> | [SavedObjectsServiceSetup](./kibana-plugin-core-server.savedobjectsservicesetup.md) |
| [status](./kibana-plugin-core-server.coresetup.status.md) | <code>StatusServiceSetup</code> | [StatusServiceSetup](./kibana-plugin-core-server.statusservicesetup.md) |
| [uiSettings](./kibana-plugin-core-server.coresetup.uisettings.md) | <code>UiSettingsServiceSetup</code> | [UiSettingsServiceSetup](./kibana-plugin-core-server.uisettingsservicesetup.md) |
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface CoreStart
| [capabilities](./kibana-plugin-core-server.corestart.capabilities.md) | <code>CapabilitiesStart</code> | [CapabilitiesStart](./kibana-plugin-core-server.capabilitiesstart.md) |
| [elasticsearch](./kibana-plugin-core-server.corestart.elasticsearch.md) | <code>ElasticsearchServiceStart</code> | [ElasticsearchServiceStart](./kibana-plugin-core-server.elasticsearchservicestart.md) |
| [http](./kibana-plugin-core-server.corestart.http.md) | <code>HttpServiceStart</code> | [HttpServiceStart](./kibana-plugin-core-server.httpservicestart.md) |
| [metrics](./kibana-plugin-core-server.corestart.metrics.md) | <code>MetricsServiceStart</code> | |
| [savedObjects](./kibana-plugin-core-server.corestart.savedobjects.md) | <code>SavedObjectsServiceStart</code> | [SavedObjectsServiceStart](./kibana-plugin-core-server.savedobjectsservicestart.md) |
| [uiSettings](./kibana-plugin-core-server.corestart.uisettings.md) | <code>UiSettingsServiceStart</code> | [UiSettingsServiceStart](./kibana-plugin-core-server.uisettingsservicestart.md) |

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [CoreStart](./kibana-plugin-core-server.corestart.md) &gt; [metrics](./kibana-plugin-core-server.corestart.metrics.md)

## CoreStart.metrics property


<b>Signature:</b>

```typescript
metrics: MetricsServiceStart;
```
2 changes: 1 addition & 1 deletion docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [LoggerFactory](./kibana-plugin-core-server.loggerfactory.md) | The single purpose of <code>LoggerFactory</code> interface is to define a way to retrieve a context-based logger instance. |
| [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) | Provides APIs to plugins for customizing the plugin's logger. |
| [LogMeta](./kibana-plugin-core-server.logmeta.md) | Contextual metadata |
| [MetricsServiceSetup](./kibana-plugin-core-server.metricsservicesetup.md) | APIs to retrieves metrics gathered and exposed by the core platform. |
| [MetricsServiceSetup](./kibana-plugin-core-server.metricsservicesetup.md) | |
| [NodesVersionCompatibility](./kibana-plugin-core-server.nodesversioncompatibility.md) | |
| [OnPostAuthToolkit](./kibana-plugin-core-server.onpostauthtoolkit.md) | A tool set defining an outcome of OnPostAuth interceptor for incoming request. |
| [OnPreAuthToolkit](./kibana-plugin-core-server.onpreauthtoolkit.md) | A tool set defining an outcome of OnPreAuth interceptor for incoming request. |
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@

## MetricsServiceSetup interface

APIs to retrieves metrics gathered and exposed by the core platform.

<b>Signature:</b>

```typescript
export interface MetricsServiceSetup
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [getOpsMetrics$](./kibana-plugin-core-server.metricsservicesetup.getopsmetrics_.md) | <code>() =&gt; Observable&lt;OpsMetrics&gt;</code> | Retrieve an observable emitting the [OpsMetrics](./kibana-plugin-core-server.opsmetrics.md) gathered. The observable will emit an initial value during core's <code>start</code> phase, and a new value every fixed interval of time, based on the <code>opts.interval</code> configuration property. |

6 changes: 3 additions & 3 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
} from './saved_objects';
import { CapabilitiesSetup, CapabilitiesStart } from './capabilities';
import { UuidServiceSetup } from './uuid';
import { MetricsServiceSetup } from './metrics';
import { MetricsServiceStart } from './metrics';
import { StatusServiceSetup } from './status';
import {
LoggingServiceSetup,
Expand Down Expand Up @@ -403,8 +403,6 @@ export interface CoreSetup<TPluginsStart extends object = object, TStart = unkno
};
/** {@link LoggingServiceSetup} */
logging: LoggingServiceSetup;
/** {@link MetricsServiceSetup} */
metrics: MetricsServiceSetup;
/** {@link SavedObjectsServiceSetup} */
savedObjects: SavedObjectsServiceSetup;
/** {@link StatusServiceSetup} */
Expand Down Expand Up @@ -442,6 +440,8 @@ export interface CoreStart {
elasticsearch: ElasticsearchServiceStart;
/** {@link HttpServiceStart} */
http: HttpServiceStart;
/** {@link MetricsServiceStart} */
metrics: MetricsServiceStart;
/** {@link SavedObjectsServiceStart} */
savedObjects: SavedObjectsServiceStart;
/** {@link UiSettingsServiceStart} */
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/internal_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from './saved_objects';
import { InternalUiSettingsServiceSetup, InternalUiSettingsServiceStart } from './ui_settings';
import { UuidServiceSetup } from './uuid';
import { InternalMetricsServiceSetup } from './metrics';
import { InternalMetricsServiceStart } from './metrics';
import { InternalRenderingServiceSetup } from './rendering';
import { InternalHttpResourcesSetup } from './http_resources';
import { InternalStatusServiceSetup } from './status';
Expand All @@ -42,7 +42,6 @@ export interface InternalCoreSetup {
context: ContextSetup;
http: InternalHttpServiceSetup;
elasticsearch: InternalElasticsearchServiceSetup;
metrics: InternalMetricsServiceSetup;
savedObjects: InternalSavedObjectsServiceSetup;
status: InternalStatusServiceSetup;
uiSettings: InternalUiSettingsServiceSetup;
Expand All @@ -59,6 +58,7 @@ export interface InternalCoreStart {
capabilities: CapabilitiesStart;
elasticsearch: ElasticsearchServiceStart;
http: InternalHttpServiceStart;
metrics: InternalMetricsServiceStart;
savedObjects: InternalSavedObjectsServiceStart;
uiSettings: InternalUiSettingsServiceStart;
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/legacy/legacy_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { capabilitiesServiceMock } from '../capabilities/capabilities_service.mo
import { httpResourcesMock } from '../http_resources/http_resources_service.mock';
import { setupMock as renderingServiceMock } from '../rendering/__mocks__/rendering_service';
import { uuidServiceMock } from '../uuid/uuid_service.mock';
import { metricsServiceMock } from '../metrics/metrics_service.mock';
import { findLegacyPluginSpecs } from './plugins';
import { LegacyVars, LegacyServiceSetupDeps, LegacyServiceStartDeps } from './types';
import { LegacyService } from './legacy_service';
Expand Down Expand Up @@ -98,7 +97,6 @@ beforeEach(() => {
contracts: new Map([['plugin-id', 'plugin-value']]),
},
rendering: renderingServiceMock,
metrics: metricsServiceMock.createInternalSetupContract(),
uuid: uuidSetup,
status: statusServiceMock.createInternalSetupContract(),
logging: loggingServiceMock.createInternalSetupContract(),
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ export class LegacyService implements CoreService {
createSerializer: startDeps.core.savedObjects.createSerializer,
getTypeRegistry: startDeps.core.savedObjects.getTypeRegistry,
},
metrics: {
getOpsMetrics$: startDeps.core.metrics.getOpsMetrics$,
},
uiSettings: { asScopedToClient: startDeps.core.uiSettings.asScopedToClient },
};

Expand Down Expand Up @@ -312,9 +315,6 @@ export class LegacyService implements CoreService {
logging: {
configure: (config$) => setupDeps.core.logging.configure([], config$),
},
metrics: {
getOpsMetrics$: setupDeps.core.metrics.getOpsMetrics$,
},
savedObjects: {
setClientFactoryProvider: setupDeps.core.savedObjects.setClientFactoryProvider,
addClientWrapper: setupDeps.core.savedObjects.addClientWrapper,
Expand Down
41 changes: 29 additions & 12 deletions src/core/server/metrics/metrics_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,46 @@
* specific language governing permissions and limitations
* under the License.
*/

import { BehaviorSubject } from 'rxjs';
import { MetricsService } from './metrics_service';
import {
InternalMetricsServiceSetup,
InternalMetricsServiceStart,
MetricsServiceSetup,
MetricsServiceStart,
} from './types';

const createSetupContractMock = () => {
const setupContract: jest.Mocked<MetricsServiceSetup> = {
getOpsMetrics$: jest.fn(),
};
return setupContract;
};

const createInternalSetupContractMock = () => {
const setupContract: jest.Mocked<InternalMetricsServiceSetup> = createSetupContractMock();
const setupContract: jest.Mocked<InternalMetricsServiceSetup> = {};
return setupContract;
};

const createStartContractMock = () => {
const startContract: jest.Mocked<MetricsServiceStart> = {};
const startContract: jest.Mocked<MetricsServiceStart> = {
getOpsMetrics$: jest.fn(),
};
startContract.getOpsMetrics$.mockReturnValue(
new BehaviorSubject({
process: {
memory: {
heap: { total_in_bytes: 1, used_in_bytes: 1, size_limit: 1 },
resident_set_size_in_bytes: 1,
},
event_loop_delay: 1,
pid: 1,
uptime_in_millis: 1,
},
os: {
platform: 'darwin' as const,
platformRelease: 'test',
load: { '1m': 1, '5m': 1, '15m': 1 },
memory: { total_in_bytes: 1, free_in_bytes: 1, used_in_bytes: 1 },
uptime_in_millis: 1,
},
response_times: { avg_in_millis: 1, max_in_millis: 1 },
requests: { disconnects: 1, total: 1, statusCodes: { '200': 1 } },
concurrent_connections: 1,
})
);
return startContract;
};

Expand All @@ -60,7 +77,7 @@ const createMock = () => {

export const metricsServiceMock = {
create: createMock,
createSetupContract: createSetupContractMock,
createSetupContract: createStartContractMock,
createStartContract: createStartContractMock,
createInternalSetupContract: createInternalSetupContractMock,
createInternalStartContract: createInternalStartContractMock,
Expand Down
12 changes: 6 additions & 6 deletions src/core/server/metrics/metrics_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('MetricsService', () => {
it('resets the collector after each collection', async () => {
mockOpsCollector.collect.mockResolvedValue(dummyMetrics);

const { getOpsMetrics$ } = await metricsService.setup({ http: httpMock });
await metricsService.start();
await metricsService.setup({ http: httpMock });
const { getOpsMetrics$ } = await metricsService.start();

// `advanceTimersByTime` only ensure the interval handler is executed
// however the `reset` call is executed after the async call to `collect`
Expand Down Expand Up @@ -109,8 +109,8 @@ describe('MetricsService', () => {

describe('#stop', () => {
it('stops the metrics interval', async () => {
const { getOpsMetrics$ } = await metricsService.setup({ http: httpMock });
await metricsService.start();
await metricsService.setup({ http: httpMock });
const { getOpsMetrics$ } = await metricsService.start();

expect(mockOpsCollector.collect).toHaveBeenCalledTimes(1);

Expand All @@ -125,8 +125,8 @@ describe('MetricsService', () => {
});

it('completes the metrics observable', async () => {
const { getOpsMetrics$ } = await metricsService.setup({ http: httpMock });
await metricsService.start();
await metricsService.setup({ http: httpMock });
const { getOpsMetrics$ } = await metricsService.start();

let completed = false;

Expand Down
13 changes: 6 additions & 7 deletions src/core/server/metrics/metrics_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ export class MetricsService

public async setup({ http }: MetricsServiceSetupDeps): Promise<InternalMetricsServiceSetup> {
this.metricsCollector = new OpsMetricsCollector(http.server);

const metricsObservable = this.metrics$.asObservable();

return {
getOpsMetrics$: () => metricsObservable,
};
return {};
}

public async start(): Promise<InternalMetricsServiceStart> {
Expand All @@ -68,7 +63,11 @@ export class MetricsService
this.refreshMetrics();
}, config.interval.asMilliseconds());

return {};
const metricsObservable = this.metrics$.asObservable();

return {
getOpsMetrics$: () => metricsObservable,
};
}

private async refreshMetrics() {
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import { Observable } from 'rxjs';
import { OpsProcessMetrics, OpsOsMetrics, OpsServerMetrics } from './collectors';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MetricsServiceSetup {}
/**
* APIs to retrieves metrics gathered and exposed by the core platform.
*
* @public
*/
export interface MetricsServiceSetup {
export interface MetricsServiceStart {
/**
* Retrieve an observable emitting the {@link OpsMetrics} gathered.
* The observable will emit an initial value during core's `start` phase, and a new value every fixed interval of time,
Expand All @@ -40,8 +42,6 @@ export interface MetricsServiceSetup {
*/
getOpsMetrics$: () => Observable<OpsMetrics>;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MetricsServiceStart {}

export type InternalMetricsServiceSetup = MetricsServiceSetup;
export type InternalMetricsServiceStart = MetricsServiceStart;
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ function createCoreSetupMock({
http: httpMock,
savedObjects: savedObjectsServiceMock.createInternalSetupContract(),
status: statusServiceMock.createSetupContract(),
metrics: metricsServiceMock.createSetupContract(),
uiSettings: uiSettingsMock,
uuid: uuidServiceMock.createSetupContract(),
logging: loggingServiceMock.createSetupContract(),
Expand All @@ -146,6 +145,7 @@ function createCoreStartMock() {
capabilities: capabilitiesServiceMock.createStartContract(),
elasticsearch: elasticsearchServiceMock.createStart(),
http: httpServiceMock.createStartContract(),
metrics: metricsServiceMock.createStartContract(),
savedObjects: savedObjectsServiceMock.createStartContract(),
uiSettings: uiSettingsServiceMock.createStartContract(),
};
Expand All @@ -159,7 +159,6 @@ function createInternalCoreSetupMock() {
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createInternalSetup(),
http: httpServiceMock.createInternalSetupContract(),
metrics: metricsServiceMock.createInternalSetupContract(),
savedObjects: savedObjectsServiceMock.createInternalSetupContract(),
status: statusServiceMock.createInternalSetupContract(),
uuid: uuidServiceMock.createSetupContract(),
Expand All @@ -176,6 +175,7 @@ function createInternalCoreStartMock() {
capabilities: capabilitiesServiceMock.createStartContract(),
elasticsearch: elasticsearchServiceMock.createStart(),
http: httpServiceMock.createInternalStartContract(),
metrics: metricsServiceMock.createStartContract(),
savedObjects: savedObjectsServiceMock.createInternalStartContract(),
uiSettings: uiSettingsServiceMock.createStartContract(),
};
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
logging: {
configure: (config$) => deps.logging.configure(['plugins', plugin.name], config$),
},
metrics: {
getOpsMetrics$: deps.metrics.getOpsMetrics$,
},
savedObjects: {
setClientFactoryProvider: deps.savedObjects.setClientFactoryProvider,
addClientWrapper: deps.savedObjects.addClientWrapper,
Expand Down Expand Up @@ -225,6 +222,9 @@ export function createPluginStartContext<TPlugin, TPluginDependencies>(
createSerializer: deps.savedObjects.createSerializer,
getTypeRegistry: deps.savedObjects.getTypeRegistry,
},
metrics: {
getOpsMetrics$: deps.metrics.getOpsMetrics$,
},
uiSettings: {
asScopedToClient: deps.uiSettings.asScopedToClient,
},
Expand Down
Loading

0 comments on commit 7f46f11

Please sign in to comment.