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

Flexible EBT Performance Metric Schema #136395

Merged
merged 65 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
c96659e
use flexible performancce event schema
Jul 14, 2022
4a4f5c8
fix
Jul 14, 2022
d86ea42
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Jul 14, 2022
266d2d8
import
Jul 14, 2022
70dbbbe
code review
Jul 14, 2022
6ce59fe
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 14, 2022
e3baa5f
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Jul 14, 2022
e7dae10
fixes
Jul 18, 2022
0375310
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 18, 2022
2153b6e
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 18, 2022
dd48217
jest
Jul 18, 2022
a1f742d
dd
Jul 18, 2022
5791c43
fix
Jul 18, 2022
daa56fa
tests
Jul 19, 2022
b4bc48f
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 19, 2022
03dc26a
Review 1
Jul 19, 2022
f5e846d
Move code to package
Jul 19, 2022
6839713
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 19, 2022
4f217a4
rename
Jul 19, 2022
ce91896
lint
Jul 19, 2022
af0f047
ts
Jul 19, 2022
94b1a65
import
Jul 19, 2022
0c7de8b
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Jul 19, 2022
8fcf8c3
[CI] Auto-commit changed files from 'node scripts/generate packages_b…
kibanamachine Jul 19, 2022
1b15981
import
Jul 19, 2022
25499dd
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 19, 2022
cb98b78
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Jul 19, 2022
27f6d5c
fix package + create wrapper
Jul 19, 2022
b0b2c0e
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 19, 2022
60cf6e9
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 19, 2022
5214952
reporting wrappers
Jul 19, 2022
e3ce4f2
naming consistency
Jul 19, 2022
62f6509
meta
Jul 19, 2022
fa20280
AnalyticsServiceSetup
Jul 19, 2022
25c9659
func
Jul 19, 2022
bde7a42
fix(NA): types typo for @kbn/ebt-tools
mistic Jul 19, 2022
a5d57d3
Fix tests, add docs, apply nits, fix types
afharo Jul 20, 2022
7728ba7
Merge pull request #10 from afharo/afharo/normalize-ebt-events
lizozom Jul 20, 2022
06a8f38
Convert KIBANA_STARTED_EVENT times to ms
Jul 20, 2022
b33c9b4
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 20, 2022
adcce90
test
Jul 20, 2022
43d2f84
test
Jul 21, 2022
19b67b1
Merge branch 'main' of https://github.com/elastic/kibana into normali…
Jul 21, 2022
ffc7e6a
fix sec -> ms conversion
Jul 28, 2022
38dac93
rename metric->performance_metric
Jul 28, 2022
66c4532
Merge branch 'main' into normalize-ebt-events
kibanamachine Jul 28, 2022
ec00d0b
Merge branch 'main' of github.com:elastic/kibana into normalize-ebt-e…
Jul 29, 2022
51aa34b
update snapshots
Jul 29, 2022
ed9affc
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 29, 2022
bcfd341
remove memory related fields from standardized fields
Jul 29, 2022
a5978ed
Merge branch 'main' into normalize-ebt-events
kibanamachine Jul 29, 2022
7633faa
fix type errors
Jul 29, 2022
eaf8c8b
Merge branch 'normalize-ebt-events' of github.com:lizozom/kibana into…
Jul 29, 2022
8373386
Merge branch 'main' into normalize-ebt-events
suchcodemuchwow Aug 1, 2022
bfd59bc
small fixes
Aug 1, 2022
9d8b950
fix meta type for tests
Aug 1, 2022
9db2bfd
Merge branch 'main' of github.com:elastic/kibana into normalize-ebt-e…
Aug 1, 2022
2d9e30f
remove status field
Aug 1, 2022
bab8873
Merge branch 'main' of github.com:elastic/kibana into normalize-ebt-e…
Aug 2, 2022
aef999c
fix pr comments
Aug 2, 2022
e3efd40
update eventName comment for MetricEvent
Aug 2, 2022
9f46d83
update event name test snapshot
Aug 2, 2022
4c0e883
update event name test snapshot for server
Aug 2, 2022
8068f99
fix pr comments
Aug 3, 2022
048b217
Merge branch 'main' of github.com:elastic/kibana into normalize-ebt-e…
Aug 3, 2022
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
"@kbn/crypto-browser": "link:bazel-bin/packages/kbn-crypto-browser",
"@kbn/datemath": "link:bazel-bin/packages/kbn-datemath",
"@kbn/doc-links": "link:bazel-bin/packages/kbn-doc-links",
"@kbn/ebt-tools": "link:bazel-bin/packages/kbn-ebt-tools",
"@kbn/es-errors": "link:bazel-bin/packages/kbn-es-errors",
"@kbn/es-query": "link:bazel-bin/packages/kbn-es-query",
"@kbn/eslint-plugin-disable": "link:bazel-bin/packages/kbn-eslint-plugin-disable",
Expand Down Expand Up @@ -885,6 +886,7 @@
"@types/kbn__dev-utils": "link:bazel-bin/packages/kbn-dev-utils/npm_module_types",
"@types/kbn__doc-links": "link:bazel-bin/packages/kbn-doc-links/npm_module_types",
"@types/kbn__docs-utils": "link:bazel-bin/packages/kbn-docs-utils/npm_module_types",
"@types/kbn__ebt-tools": "link:bazel-bin/packages/kbn-ebt-tools/npm_module_types",
"@types/kbn__es-archiver": "link:bazel-bin/packages/kbn-es-archiver/npm_module_types",
"@types/kbn__es-errors": "link:bazel-bin/packages/kbn-es-errors/npm_module_types",
"@types/kbn__es-query": "link:bazel-bin/packages/kbn-es-query/npm_module_types",
Expand Down
2 changes: 2 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ filegroup(
"//packages/kbn-dev-utils:build",
"//packages/kbn-doc-links:build",
"//packages/kbn-docs-utils:build",
"//packages/kbn-ebt-tools:build",
"//packages/kbn-es-archiver:build",
"//packages/kbn-es-errors:build",
"//packages/kbn-es-query:build",
Expand Down Expand Up @@ -396,6 +397,7 @@ filegroup(
"//packages/kbn-dev-utils:build_types",
"//packages/kbn-doc-links:build_types",
"//packages/kbn-docs-utils:build_types",
"//packages/kbn-ebt-tools:build_types",
"//packages/kbn-es-archiver:build_types",
"//packages/kbn-es-errors:build_types",
"//packages/kbn-es-query:build_types",
Expand Down
16 changes: 10 additions & 6 deletions packages/analytics/client/src/analytics_client/analytics_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { Type } from 'io-ts';
import type { Mixed } from 'io-ts';
import type { Observable } from 'rxjs';
import { BehaviorSubject, Subject, combineLatest, from, merge } from 'rxjs';
import {
Expand Down Expand Up @@ -43,7 +43,7 @@ import { ContextService } from './context_service';
import { schemaToIoTs, validateSchema } from '../schema/validation';

interface EventDebugLogMeta extends LogMeta {
ebt_event: Event;
ebt_event: Event<unknown>;
}

export class AnalyticsClient implements IAnalyticsClient {
Expand All @@ -65,7 +65,7 @@ export class AnalyticsClient implements IAnalyticsClient {
private readonly shipperRegistered$ = new Subject<void>();
private readonly eventTypeRegistry = new Map<
EventType,
EventTypeOpts<unknown> & { validator?: Type<Record<string, unknown>> }
EventTypeOpts<unknown> & { validator?: Mixed }
>();
private readonly contextService: ContextService;
private readonly context$ = new BehaviorSubject<Partial<EventContext>>({});
Expand All @@ -88,7 +88,7 @@ export class AnalyticsClient implements IAnalyticsClient {
this.reportEnqueuedEventsWhenClientIsReady();
}

public reportEvent = <EventTypeData extends Record<string, unknown>>(
public reportEvent = <EventTypeData extends object>(
eventType: EventType,
eventData: EventTypeData
) => {
Expand Down Expand Up @@ -119,14 +119,18 @@ export class AnalyticsClient implements IAnalyticsClient {

// If the validator is registered (dev-mode only), perform the validation.
if (eventTypeOpts.validator) {
validateSchema(`Event Type '${eventType}'`, eventTypeOpts.validator, eventData);
validateSchema<EventTypeData>(
`Event Type '${eventType}'`,
eventTypeOpts.validator,
eventData
);
}

const event: Event = {
timestamp,
event_type: eventType,
context: this.context$.value,
properties: eventData,
properties: eventData as unknown as Record<string, unknown>,
};

// debug-logging before checking the opt-in status to help during development
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/client/src/analytics_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export interface IAnalyticsClient {
* @param eventType The event type registered via the `registerEventType` API.
* @param eventData The properties matching the schema declared in the `registerEventType` API.
*/
reportEvent: <EventTypeData extends Record<string, unknown>>(
reportEvent: <EventTypeData extends object>(
eventType: EventType,
eventData: EventTypeData
) => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/analytics/client/src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export interface TelemetryCounter {
/**
* Definition of the full event structure
*/
export interface Event {
export interface Event<Properties = Record<string, unknown>> {
/**
* The time the event was generated in ISO format.
*/
Expand All @@ -120,7 +120,7 @@ export interface Event {
/**
* The specific properties of the event type.
*/
properties: Record<string, unknown>;
properties: Properties;
/**
* The {@link EventContext} enriched during the processing pipeline.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const FULLSTORY_RESERVED_PROPERTIES = [
'pageName',
];

export function formatPayload(context: Record<string, unknown>): Record<string, unknown> {
export function formatPayload(context: object): Record<string, unknown> {
// format context keys as required for env vars, see docs: https://help.fullstory.com/hc/en-us/articles/360020623234
return Object.fromEntries(
Object.entries(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUNTIME_DEPS = [
"@npm//rxjs",
"@npm//uuid",
"//packages/analytics/client",
"//packages/kbn-ebt-tools",
"//packages/core/base/core-base-browser-mocks",
"//packages/core/injected-metadata/core-injected-metadata-browser-mocks",
]
Expand All @@ -41,6 +42,7 @@ TYPES_DEPS = [
"@npm//rxjs",
"//packages/kbn-logging:npm_module_types",
"//packages/analytics/client:npm_module_types",
"//packages/kbn-ebt-tools:npm_module_types",
"//packages/core/base/core-base-browser-internal:npm_module_types",
"//packages/core/injected-metadata/core-injected-metadata-browser-internal:npm_module_types",
"//packages/core/analytics/core-analytics-browser:npm_module_types",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,149 @@ 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$)
suchcodemuchwow marked this conversation as resolved.
Show resolved Hide resolved
).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),
});
});

test('should register the `metrics` and `clicks` event types on creation', () => {
suchcodemuchwow marked this conversation as resolved.
Show resolved Hide resolved
expect(analyticsClientMock.registerEventType).toHaveBeenCalledTimes(2);
expect(analyticsClientMock.registerEventType.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"eventType": "performance_metric",
"schema": Object {
"duration": Object {
"_meta": Object {
"description": "The main event duration in ms",
},
"type": "integer",
},
"eventName": Object {
"_meta": Object {
"description": "Type of the event",
},
"type": "keyword",
},
"key1": Object {
"_meta": Object {
"description": "Performance metric label 1",
"optional": true,
},
"type": "keyword",
},
"key2": Object {
"_meta": Object {
"description": "Performance metric label 2",
"optional": true,
},
"type": "keyword",
},
"key3": Object {
"_meta": Object {
"description": "Performance metric label 3",
"optional": true,
},
"type": "keyword",
},
"key4": Object {
"_meta": Object {
"description": "Performance metric label 4",
"optional": true,
},
"type": "keyword",
},
"key5": Object {
"_meta": Object {
"description": "Performance metric label 5",
"optional": true,
},
"type": "keyword",
},
"meta": Object {
"_meta": Object {
"description": "Meta data that is searchable but not aggregatable",
"optional": true,
},
"type": "pass_through",
},
"value1": Object {
"_meta": Object {
"description": "Performance metric value 1",
"optional": true,
},
"type": "long",
},
"value2": Object {
"_meta": Object {
"description": "Performance metric value 2",
"optional": true,
},
"type": "long",
},
"value3": Object {
"_meta": Object {
"description": "Performance metric value 3",
"optional": true,
},
"type": "long",
},
"value4": Object {
"_meta": Object {
"description": "Performance metric value 4",
"optional": true,
},
"type": "long",
},
"value5": Object {
"_meta": Object {
"description": "Performance metric value 5",
"optional": true,
},
"type": "long",
},
},
},
]
`);
expect(analyticsClientMock.registerEventType.mock.calls[1]).toMatchInlineSnapshot(`
Array [
Object {
"eventType": "click",
"schema": Object {
"target": Object {
"items": Object {
"_meta": Object {
"description": "The attributes of the clicked element and all its parents in the form \`{attr.name}={attr.value}\`. It allows finding the clicked elements by looking up its attributes like \\"data-test-subj=my-button\\".",
},
"type": "keyword",
},
"type": "array",
},
},
},
]
`);
});

test('setup should expose all the register APIs, reportEvent and opt-in', () => {
const injectedMetadata = injectedMetadataServiceMock.createSetupContract();
expect(analyticsService.setup({ injectedMetadata })).toStrictEqual({
Expand All @@ -61,8 +181,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 @@ -74,14 +194,14 @@ describe('AnalyticsService', () => {
});
analyticsService.setup({ injectedMetadata });
await expect(
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).resolves.toMatchInlineSnapshot(`
Object {
"cluster_name": "cluster_name",
"cluster_uuid": "cluster_uuid",
"cluster_version": "version",
}
`);
await firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$)
).toMatchInlineSnapshot(`
Object {
"cluster_name": "cluster_name",
"cluster_uuid": "cluster_uuid",
"cluster_version": "version",
}
`);
});

test('setup should expose only the APIs report and opt-in', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { of } from 'rxjs';
import type { AnalyticsClient } from '@kbn/analytics-client';
import { createAnalytics } from '@kbn/analytics-client';
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 @@ -34,6 +35,8 @@ export class AnalyticsService {
});

this.registerBuildInfoAnalyticsContext(core);
// Register special `performance_metrics` type
suchcodemuchwow marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -28,13 +28,15 @@ NPM_MODULE_EXTRA_FILES = [
RUNTIME_DEPS = [
"@npm//rxjs",
"//packages/analytics/client",
"//packages/kbn-ebt-tools",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//rxjs",
"//packages/analytics/client:npm_module_types",
"//packages/kbn-ebt-tools:npm_module_types",
"//packages/core/base/core-base-server-internal:npm_module_types",
"//packages/core/analytics/core-analytics-server:npm_module_types",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 { AnalyticsClient } from '@kbn/analytics-client';
import { Subject } from 'rxjs';

export const analyticsClientMock: jest.Mocked<AnalyticsClient> = {
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
registerContextProvider: jest.fn(),
removeContextProvider: jest.fn(),
registerShipper: jest.fn(),
telemetryCounter$: new Subject(),
shutdown: jest.fn(),
};

jest.doMock('@kbn/analytics-client', () => ({
createAnalytics: () => analyticsClientMock,
}));
Loading