-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[telemetry] Analytics Package #41113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
810f94a
kbn-analytics
Bamieh 1983529
kbn-analytics
Bamieh b41e18b
expose provider
Bamieh 932a189
add logger
Bamieh bc8e470
performance and stats reporters
Bamieh 3dff33d
merge master
Bamieh 77d6cad
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 32fcbe4
finalize ui stats metric
Bamieh ea44a6c
functional tests
Bamieh 1d4a663
remove readme file for now
Bamieh 69006f9
update readme
Bamieh 1c4a0d0
add types file into to tsconfigs
Bamieh ccd7d69
Update packages/kbn-analytics/src/report.ts
Bamieh 6bc1caf
fix typechecks
Bamieh 4c46ff0
Merge branch 'telemetry/move_ui_metric' of github.com:Bamieh/kibana i…
Bamieh 75bd6ba
use enum instead of strings for metric types
Bamieh 5a0b833
getUiStatsReporter -> createUiStatsReporter
Bamieh 44b0ec4
fix special typo in README
Bamieh aa434f6
remove unused stop method
Bamieh 1665a22
fix tests
Bamieh 121fcd3
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 321cceb
josh meeting fixes
Bamieh 920b222
default debug to false
Bamieh b3baca6
use chrome.getInjected
Bamieh eabe9e3
add METRIC_TYPE to jest module mocks
Bamieh 210b8a3
mock create fn
Bamieh 131e829
handle enabled:false
Bamieh 7a5db03
init ui_metric in test setup env
Bamieh c128c7b
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 8bd4ef4
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 3dfd554
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh ee3fc7d
regenerator runtime
Bamieh 331f900
transform-regenerator
Bamieh 4d86435
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 1efc619
update lock file
Bamieh 257ebea
update babel configs
Bamieh 94de087
runtime dep
Bamieh 12626e2
add regenerator
Bamieh 2e76e78
babel configs
Bamieh 6925b29
fix conflicts
Bamieh e1b7cde
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh 1996c25
use env-preset
Bamieh 9d63270
merge conflicts
Bamieh a0780e4
fix workpad telemetry tests
Bamieh 4a20e69
regeneratorRuntime attempt to fix number 30000
Bamieh d42a807
env targets
Bamieh c564076
remove module config
Bamieh 1987e8d
try again
Bamieh 0278589
try without regenerator
Bamieh 5c52b30
use kbn/babel-preset/webpack_preset only
Bamieh 487677e
runtime
Bamieh 79fd372
just use typescript
Bamieh fb18982
update tsconfig
Bamieh eb60ea8
Caches trackers by app value for infra useTrackMetric hook
jasonrhodes e02e22a
Merge branch 'master' of github.com:elastic/kibana into telemetry/mov…
Bamieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "@kbn/analytics", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "description": "Kibana Analytics tool", | ||
| "main": "target/index.js", | ||
| "types": "target/index.d.ts", | ||
| "author": "Ahmad Bamieh <ahmadbamieh@gmail.com>", | ||
| "license": "Apache-2.0", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "kbn:bootstrap": "yarn build", | ||
| "kbn:watch": "yarn build --watch" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "3.5.1" | ||
| }, | ||
| "dependencies": { | ||
| "@kbn/dev-utils": "1.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| export { createReporter, ReportHTTP, Reporter, ReporterConfig } from './reporter'; | ||
| export { UiStatsMetricType, METRIC_TYPE } from './metrics'; | ||
| export { Report, ReportManager } from './report'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { UiStatsMetric, UiStatsMetricType } from './ui_stats'; | ||
|
|
||
| export { | ||
| UiStatsMetric, | ||
| createUiStatsMetric, | ||
| UiStatsMetricReport, | ||
| UiStatsMetricType, | ||
| } from './ui_stats'; | ||
| export { Stats } from './stats'; | ||
|
|
||
| export type Metric = UiStatsMetric<UiStatsMetricType>; | ||
| export type MetricType = keyof typeof METRIC_TYPE; | ||
|
|
||
| export enum METRIC_TYPE { | ||
| COUNT = 'count', | ||
| LOADED = 'loaded', | ||
| CLICK = 'click', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { Stats } from './stats'; | ||
| import { METRIC_TYPE } from './'; | ||
|
|
||
| export type UiStatsMetricType = METRIC_TYPE.CLICK | METRIC_TYPE.LOADED | METRIC_TYPE.COUNT; | ||
| export interface UiStatsMetricConfig<T extends UiStatsMetricType> { | ||
| type: T; | ||
| appName: string; | ||
| eventName: string; | ||
| count?: number; | ||
| } | ||
|
|
||
| export interface UiStatsMetric<T extends UiStatsMetricType = UiStatsMetricType> { | ||
| type: T; | ||
| appName: string; | ||
| eventName: string; | ||
| count: number; | ||
| } | ||
|
|
||
| export function createUiStatsMetric<T extends UiStatsMetricType>({ | ||
| type, | ||
| appName, | ||
| eventName, | ||
| count = 1, | ||
| }: UiStatsMetricConfig<T>): UiStatsMetric<T> { | ||
| return { type, appName, eventName, count }; | ||
| } | ||
|
|
||
| export interface UiStatsMetricReport { | ||
| key: string; | ||
| appName: string; | ||
| eventName: string; | ||
| type: UiStatsMetricType; | ||
| stats: Stats; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { UnreachableCaseError } from './util'; | ||
| import { Metric, Stats, UiStatsMetricReport, METRIC_TYPE } from './metrics'; | ||
|
|
||
| export interface Report { | ||
| uiStatsMetrics: { | ||
| [key: string]: UiStatsMetricReport; | ||
| }; | ||
| } | ||
|
|
||
| export class ReportManager { | ||
| public report: Report; | ||
| constructor(report?: Report) { | ||
| this.report = report || ReportManager.createReport(); | ||
| } | ||
| static createReport() { | ||
| return { uiStatsMetrics: {} }; | ||
| } | ||
| public clearReport() { | ||
| this.report = ReportManager.createReport(); | ||
| } | ||
| public isReportEmpty(): boolean { | ||
| return Object.keys(this.report.uiStatsMetrics).length === 0; | ||
| } | ||
| private incrementStats(count: number, stats?: Stats): Stats { | ||
| const { min = 0, max = 0, sum = 0 } = stats || {}; | ||
| const newMin = Math.min(min, count); | ||
| const newMax = Math.max(max, count); | ||
| const newAvg = newMin + newMax / 2; | ||
| const newSum = sum + count; | ||
|
|
||
| return { | ||
| min: newMin, | ||
| max: newMax, | ||
| avg: newAvg, | ||
| sum: newSum, | ||
| }; | ||
| } | ||
| assignReports(newMetrics: Metric[]) { | ||
| newMetrics.forEach(newMetric => this.assignReport(this.report, newMetric)); | ||
| } | ||
| static createMetricKey(metric: Metric): string { | ||
| switch (metric.type) { | ||
| case METRIC_TYPE.CLICK: | ||
| case METRIC_TYPE.LOADED: | ||
| case METRIC_TYPE.COUNT: { | ||
| const { appName, type, eventName } = metric; | ||
| return `${appName}-${type}-${eventName}`; | ||
| } | ||
| default: | ||
| throw new UnreachableCaseError(metric.type); | ||
| } | ||
| } | ||
| private assignReport(report: Report, metric: Metric) { | ||
| switch (metric.type) { | ||
| case METRIC_TYPE.CLICK: | ||
| case METRIC_TYPE.LOADED: | ||
| case METRIC_TYPE.COUNT: { | ||
| const { appName, type, eventName, count } = metric; | ||
| const key = ReportManager.createMetricKey(metric); | ||
| const existingStats = (report.uiStatsMetrics[key] || {}).stats; | ||
| this.report.uiStatsMetrics[key] = { | ||
| key, | ||
| appName, | ||
| eventName, | ||
| type, | ||
| stats: this.incrementStats(count, existingStats), | ||
| }; | ||
| return; | ||
| } | ||
| default: | ||
| throw new UnreachableCaseError(metric.type); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { wrapArray } from './util'; | ||
| import { Metric, UiStatsMetric, createUiStatsMetric } from './metrics'; | ||
|
|
||
| import { Storage, ReportStorageManager } from './storage'; | ||
| import { Report, ReportManager } from './report'; | ||
|
|
||
| export interface ReporterConfig { | ||
| http: ReportHTTP; | ||
| storage?: Storage; | ||
| checkInterval?: number; | ||
| debug?: boolean; | ||
| storageKey?: string; | ||
| } | ||
|
|
||
| export type ReportHTTP = (report: Report) => Promise<void>; | ||
|
|
||
| export class Reporter { | ||
| checkInterval: number; | ||
| private interval: any; | ||
| private http: ReportHTTP; | ||
| private reportManager: ReportManager; | ||
| private storageManager: ReportStorageManager; | ||
| private debug: boolean; | ||
|
|
||
| constructor(config: ReporterConfig) { | ||
| const { http, storage, debug, checkInterval = 10000, storageKey = 'analytics' } = config; | ||
|
|
||
| this.http = http; | ||
| this.checkInterval = checkInterval; | ||
| this.interval = null; | ||
| this.storageManager = new ReportStorageManager(storageKey, storage); | ||
| const storedReport = this.storageManager.get(); | ||
| this.reportManager = new ReportManager(storedReport); | ||
| this.debug = !!debug; | ||
| } | ||
|
|
||
| private saveToReport(newMetrics: Metric[]) { | ||
| this.reportManager.assignReports(newMetrics); | ||
| this.storageManager.store(this.reportManager.report); | ||
| } | ||
|
|
||
| private flushReport() { | ||
| this.reportManager.clearReport(); | ||
| this.storageManager.store(this.reportManager.report); | ||
| } | ||
|
|
||
| public start() { | ||
| if (!this.interval) { | ||
| this.interval = setTimeout(() => { | ||
| this.interval = null; | ||
| this.sendReports(); | ||
| }, this.checkInterval); | ||
| } | ||
| } | ||
|
|
||
| private log(message: any) { | ||
| if (this.debug) { | ||
| // eslint-disable-next-line | ||
| console.debug(message); | ||
| } | ||
| } | ||
|
|
||
| public reportUiStats( | ||
| appName: string, | ||
| type: UiStatsMetric['type'], | ||
| eventNames: string | string[], | ||
| count?: number | ||
| ) { | ||
| const metrics = wrapArray(eventNames).map(eventName => { | ||
| if (this) this.log(`${type} Metric -> (${appName}:${eventName}):`); | ||
| const report = createUiStatsMetric({ type, appName, eventName, count }); | ||
| this.log(report); | ||
| return report; | ||
| }); | ||
| this.saveToReport(metrics); | ||
| } | ||
|
|
||
| public async sendReports() { | ||
| if (!this.reportManager.isReportEmpty()) { | ||
| try { | ||
| await this.http(this.reportManager.report); | ||
| this.flushReport(); | ||
| } catch (err) { | ||
| this.log(`Error Sending Metrics Report ${err}`); | ||
| } | ||
| } | ||
| this.start(); | ||
| } | ||
| } | ||
|
|
||
| export function createReporter(reportedConf: ReporterConfig) { | ||
| const reporter = new Reporter(reportedConf); | ||
| reporter.start(); | ||
| return reporter; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this suppose to be logging in Kibana development mode? How does one go about turning this off via the
configobject that's being passed in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can override the default by adding
ui_metric.debug:falsein the kibana.yml file. Note that it defaults totrueonly if kibana is in development mode.