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

rename metric -> performance_metric #12

Merged
merged 2 commits into from
Jul 28, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ describe('AnalyticsService', () => {
test('should register some context providers on creation', async () => {
expect(analyticsClientMock.registerContextProvider).toHaveBeenCalledTimes(3);
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[0][0].context$)
).resolves.toMatchInlineSnapshot(`
Object {
"branch": "branch",
"buildNum": 100,
"buildSha": "buildSha",
"isDev": true,
"isDistributable": false,
"version": "version",
}
`);
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[0][0].context$)
).toMatchInlineSnapshot(`
Object {
"branch": "branch",
"buildNum": 100,
"buildSha": "buildSha",
"isDev": true,
"isDistributable": false,
"version": "version",
}
`);
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[1][0].context$)
).resolves.toEqual({ session_id: expect.any(String) });
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[1][0].context$)
).toEqual({ session_id: expect.any(String) });
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[2][0].context$)
).resolves.toEqual({
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[2][0].context$)
).toEqual({
preferred_language: 'en-US',
preferred_languages: ['en-US', 'en'],
user_agent: expect.any(String),
Expand Down Expand Up @@ -210,8 +210,8 @@ describe('AnalyticsService', () => {
const injectedMetadata = injectedMetadataServiceMock.createSetupContract();
analyticsService.setup({ injectedMetadata });
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).resolves.toMatchInlineSnapshot(`undefined`);
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).toMatchInlineSnapshot(`undefined`);
});

test('setup should register the elasticsearch info context provider (with info)', async () => {
Expand All @@ -223,8 +223,8 @@ describe('AnalyticsService', () => {
});
analyticsService.setup({ injectedMetadata });
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).resolves.toMatchInlineSnapshot(`
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).toMatchInlineSnapshot(`
Object {
"cluster_name": "cluster_name",
"cluster_uuid": "cluster_uuid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { of } from 'rxjs';
import type { AnalyticsClient } from '@kbn/analytics-client';
import { createAnalytics } from '@kbn/analytics-client';
import { registerMetricEventType } from '@kbn/ebt-tools';
import { registerPerformanceMetricEventType } from '@kbn/ebt-tools';
import type { CoreContext } from '@kbn/core-base-browser-internal';
import type { InternalInjectedMetadataSetup } from '@kbn/core-injected-metadata-browser-internal';
import type { AnalyticsServiceSetup, AnalyticsServiceStart } from '@kbn/core-analytics-browser';
Expand All @@ -35,8 +35,8 @@ export class AnalyticsService {
});

this.registerBuildInfoAnalyticsContext(core);
// Register special `metrics` type
registerMetricEventType(this.analyticsClient);
// Register special `performance_metrics` type
registerPerformanceMetricEventType(this.analyticsClient);

// We may eventually move the following to the client's package since they are not Kibana-specific
// and can benefit other consumers of the client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('AnalyticsService', () => {
test('should register the context provider `build info` on creation', async () => {
expect(analyticsClientMock.registerContextProvider).toHaveBeenCalledTimes(1);
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[0][0].context$)
).resolves.toMatchInlineSnapshot(`
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[0][0].context$)
).toMatchInlineSnapshot(`
Object {
"branch": "main",
"buildNum": 9007199254740991,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { of } from 'rxjs';
import type { AnalyticsClient } from '@kbn/analytics-client';
import { createAnalytics } from '@kbn/analytics-client';
import { registerMetricEventType } from '@kbn/ebt-tools';
import { registerPerformanceMetricEventType } from '@kbn/ebt-tools';
import type { CoreContext } from '@kbn/core-base-server-internal';
import type {
AnalyticsServiceSetup,
Expand All @@ -31,7 +31,7 @@ export class AnalyticsService {

this.registerBuildInfoAnalyticsContext(core);
// Register special `metrics` type
registerMetricEventType(this.analyticsClient);
registerPerformanceMetricEventType(this.analyticsClient);
}

public preboot(): AnalyticsServicePreboot {
Expand Down
30 changes: 16 additions & 14 deletions packages/kbn-ebt-tools/src/metric_events/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import { createAnalytics, type AnalyticsClient } from '@kbn/analytics-client';
import { loggerMock } from '@kbn/logging-mocks';
import { registerMetricEventType, reportMetricEvent } from './helpers';
import { registerPerformanceMetricEventType, reportPerformanceMetricEvent } from './helpers';
import { METRIC_EVENT_SCHEMA } from './schema';

describe('metric event helpers', () => {
let analyticsClient: AnalyticsClient;

describe('registerMetricEventType', () => {
describe('registerPerformanceMetricEventType', () => {
beforeEach(() => {
analyticsClient = createAnalytics({
isDev: true, // Explicitly setting `true` to ensure we have event validation to make sure the events sent pass our validation.
Expand All @@ -23,34 +23,34 @@ describe('metric event helpers', () => {
});
});

test('registers the `metrics` eventType to the analytics client', () => {
test('registers the `performance_metric` eventType to the analytics client', () => {
const registerEventTypeSpy = jest.spyOn(analyticsClient, 'registerEventType');

expect(() => registerMetricEventType(analyticsClient)).not.toThrow();
expect(() => registerPerformanceMetricEventType(analyticsClient)).not.toThrow();

expect(registerEventTypeSpy).toHaveBeenCalledWith({
eventType: 'metric',
eventType: 'performance_metric',
schema: METRIC_EVENT_SCHEMA,
});
});
});

describe('reportMetricEvent', () => {
describe('reportPerformanceMetricEvent', () => {
beforeEach(() => {
analyticsClient = createAnalytics({
isDev: true, // Explicitly setting `true` to ensure we have event validation to make sure the events sent pass our validation.
sendTo: 'staging',
logger: loggerMock.create(),
});
registerMetricEventType(analyticsClient);
registerPerformanceMetricEventType(analyticsClient);
});

test('reports the minimum allowed event', () => {
reportMetricEvent(analyticsClient, { eventName: 'test-event' });
reportPerformanceMetricEvent(analyticsClient, { eventName: 'test-event', duration: 1000 });
});

test('reports all the allowed fields in the event', () => {
reportMetricEvent(analyticsClient, {
reportPerformanceMetricEvent(analyticsClient, {
eventName: 'test-event',
meta: { my: { custom: { fields: 'here' } }, another_field: true },
status: 'something',
Expand All @@ -70,26 +70,28 @@ describe('metric event helpers', () => {

test('should fail if eventName is missing', () => {
expect(() =>
reportMetricEvent(
reportPerformanceMetricEvent(
analyticsClient,
// @ts-expect-error
{}
)
).toThrowErrorMatchingInlineSnapshot(`
"Failed to validate payload coming from \\"Event Type 'metric'\\":
- [eventName]: {\\"expected\\":\\"string\\",\\"actual\\":\\"undefined\\",\\"value\\":\\"undefined\\"}"
"Failed to validate payload coming from \\"Event Type 'performance_metric'\\":
- [eventName]: {\\"expected\\":\\"string\\",\\"actual\\":\\"undefined\\",\\"value\\":\\"undefined\\"}
- [duration]: {\\"expected\\":\\"number\\",\\"actual\\":\\"undefined\\",\\"value\\":\\"undefined\\"}"
`);
});

test('should fail if any additional unknown keys are added', () => {
expect(() =>
reportMetricEvent(analyticsClient, {
reportPerformanceMetricEvent(analyticsClient, {
eventName: 'test-event',
duration: 1000,
// @ts-expect-error
an_unknown_field: 'blah',
})
).toThrowErrorMatchingInlineSnapshot(`
"Failed to validate payload coming from \\"Event Type 'metric'\\":
"Failed to validate payload coming from \\"Event Type 'performance_metric'\\":
- []: excess key 'an_unknown_field' found"
`);
});
Expand Down
12 changes: 7 additions & 5 deletions packages/kbn-ebt-tools/src/metric_events/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import type { AnalyticsClient } from '@kbn/analytics-client';
import { type MetricEvent, METRIC_EVENT_SCHEMA } from './schema';

const METRIC_EVENT_TYPE = 'metric';
const PERFORMANCE_METRIC_EVENT_TYPE = 'performance_metric';

/**
* Register the `metrics` event type
* @param analytics The {@link AnalyticsClient} during the setup phase (it has the method `registerEventType`)
* @private To be called only by core's Analytics Service
*/
export function registerMetricEventType(analytics: Pick<AnalyticsClient, 'registerEventType'>) {
export function registerPerformanceMetricEventType(
analytics: Pick<AnalyticsClient, 'registerEventType'>
) {
analytics.registerEventType<MetricEvent>({
eventType: METRIC_EVENT_TYPE,
eventType: PERFORMANCE_METRIC_EVENT_TYPE,
schema: METRIC_EVENT_SCHEMA,
});
}
Expand All @@ -28,9 +30,9 @@ export function registerMetricEventType(analytics: Pick<AnalyticsClient, 'regist
* @param analytics The {@link AnalyticsClient} to report the events.
* @param eventData The data to send, conforming the structure of a {@link MetricEvent}.
*/
export function reportMetricEvent(
export function reportPerformanceMetricEvent(
analytics: Pick<AnalyticsClient, 'reportEvent'>,
eventData: MetricEvent
) {
analytics.reportEvent(METRIC_EVENT_TYPE, eventData);
analytics.reportEvent(PERFORMANCE_METRIC_EVENT_TYPE, eventData);
}
5 changes: 4 additions & 1 deletion packages/kbn-ebt-tools/src/metric_events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
* Side Public License, v 1.
*/
export type { MetricEvent } from './schema';
export { registerMetricEventType, reportMetricEvent } from './helpers';
export {
registerPerformanceMetricEventType as registerPerformanceMetricEventType,
reportPerformanceMetricEvent,
} from './helpers';
4 changes: 2 additions & 2 deletions packages/kbn-ebt-tools/src/metric_events/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface MetricEvent {
* @group Standardized fields
* The time (in milliseconds) it took to run the entire action.
*/
duration?: number;
duration: number;
/**
* @group Standardized fields
* A status relevant to the action (i.e.: `failed`, `succeeded`).
Expand Down Expand Up @@ -110,7 +110,7 @@ export const METRIC_EVENT_SCHEMA: RootSchema<MetricEvent> = {
},
duration: {
type: 'integer',
_meta: { description: 'The main event duration in ms', optional: true },
_meta: { description: 'The main event duration in ms' },
},
status: {
type: 'keyword',
Expand Down
4 changes: 2 additions & 2 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ describe('#start()', () => {
it('reports the metric event kibana-loaded and clears marks', async () => {
await startCore();
expect(analyticsServiceStartMock.reportEvent).toHaveBeenCalledTimes(2);
expect(analyticsServiceStartMock.reportEvent).toHaveBeenNthCalledWith(2, 'metric', {
expect(analyticsServiceStartMock.reportEvent).toHaveBeenNthCalledWith(2, 'performance_metric', {
eventName: KIBANA_LOADED_EVENT,
meta: {
kibana_version: '1.2.3',
Expand Down Expand Up @@ -320,7 +320,7 @@ describe('#start()', () => {
await startCore();

expect(analyticsServiceStartMock.reportEvent).toHaveBeenCalledTimes(2);
expect(analyticsServiceStartMock.reportEvent).toHaveBeenNthCalledWith(2, 'metric', {
expect(analyticsServiceStartMock.reportEvent).toHaveBeenNthCalledWith(2, 'performance_metric', {
eventName: KIBANA_LOADED_EVENT,
meta: {
kibana_version: '1.2.3',
Expand Down
4 changes: 2 additions & 2 deletions src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { HttpService } from '@kbn/core-http-browser-internal';
import { UiSettingsService } from '@kbn/core-ui-settings-browser-internal';
import { DeprecationsService } from '@kbn/core-deprecations-browser-internal';
import { IntegrationsService } from '@kbn/core-integrations-browser-internal';
import { reportMetricEvent } from '@kbn/ebt-tools';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import { fetchOptionalMemoryInfo } from './fetch_optional_memory_info';
import { CoreSetup, CoreStart } from '.';
import { ChromeService } from './chrome';
Expand Down Expand Up @@ -164,7 +164,7 @@ export class CoreSystem {
});

const timing = this.getLoadMarksInfo();
reportMetricEvent(analytics, {
reportPerformanceMetricEvent(analytics, {
eventName: KIBANA_LOADED_EVENT,
meta: {
kibana_version: this.coreContext.env.packageInfo.version,
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { NodeService, nodeConfig } from '@kbn/core-node-server-internal';
import { AnalyticsService } from '@kbn/core-analytics-server-internal';
import type { AnalyticsServiceSetup, AnalyticsServiceStart } from '@kbn/core-analytics-server';
import { reportMetricEvent } from '@kbn/ebt-tools';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import { EnvironmentService, pidConfig } from '@kbn/core-environment-server-internal';
import {
ExecutionContextService,
Expand Down Expand Up @@ -568,9 +568,9 @@ export class Server {

const ups = this.uptimePerStep;

const toMs = (sec: number) => Math.round(sec * 100);
const toMs = (sec: number) => Math.round(sec * 1000);
// Report the metric-shaped KIBANA_STARTED_EVENT.
reportMetricEvent(analyticsStart, {
reportPerformanceMetricEvent(analyticsStart, {
eventName: KIBANA_STARTED_EVENT,
duration: toMs(ups.start!.end - ups.constructor!.start),
key1: 'time_to_constructor',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { I18nProvider } from '@kbn/i18n-react';
import uuid from 'uuid';
import { CoreStart, IUiSettingsClient, KibanaExecutionContext } from '@kbn/core/public';
import { Start as InspectorStartContract } from '@kbn/inspector-plugin/public';
import { reportMetricEvent } from '@kbn/ebt-tools';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';

import { ControlGroupContainer } from '@kbn/controls-plugin/public';
import { Filter, TimeRange } from '@kbn/es-query';
Expand Down Expand Up @@ -166,7 +166,7 @@ export class DashboardContainer extends Container<InheritedChildInput, Dashboard

private onDataLoaded(data: DashboardLoadedInfo) {
if (this.services.analytics) {
reportMetricEvent(this.services.analytics, {
reportPerformanceMetricEvent(this.services.analytics, {
eventName: DASHBOARD_LOADED_EVENT,
duration: data.timeToDone,
status: data.status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {

const getEvents = async (count: number, options?: GetEventsOptions) =>
ebtUIHelper.getEvents(count, {
eventTypes: ['metric'],
eventTypes: ['performance_metric'],
fromTimestamp,
withTimeoutMs: 1000,
filters: { 'properties.eventName': { eq: DASHBOARD_LOADED_EVENT } },
Expand All @@ -42,7 +42,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const events = await getEvents(Number.MAX_SAFE_INTEGER, options);
expect(events.length).to.be(1);
const event = events[0];
expect(event.event_type).to.eql('metric');
expect(event.event_type).to.eql('performance_metric');
expect(event.properties.eventName).to.eql(DASHBOARD_LOADED_EVENT);
expect(event.context.applicationId).to.be('dashboards');
expect(event.context.page).to.be('app');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {

it('should emit the new kibana-loaded events', async () => {
const [event] = await ebtUIHelper.getEvents(1, {
eventTypes: ['metric'],
eventTypes: ['performance_metric'],
filters: { 'properties.eventName': { eq: 'kibana_loaded' } },
});

// New event
expect(event.event_type).to.eql('metric');
expect(event.event_type).to.eql('performance_metric');
expect(event.properties.eventName).to.eql('kibana_loaded');

// meta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export default function ({ getService }: FtrProviderContext) {

it('should emit the "kibana_started" metric event', async () => {
const [event] = await ebtServerHelper.getEvents(1, {
eventTypes: ['metric'],
eventTypes: ['performance_metric'],
filters: { 'properties.eventName': { eq: 'kibana_started' } },
});
expect(event.event_type).to.eql('metric');
expect(event.event_type).to.eql('performance_metric');
expect(event.properties.eventName).to.eql('kibana_started');
expect(event.properties.duration).to.be.a('number');
expect(event.properties.key1).to.eql('time_to_constructor');
Expand Down