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

[7.x] [Telemetry] fix bug where uiStatsMetrics is not getting reported #54045 #54072

Merged
merged 1 commit into from
Jan 7, 2020
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
24 changes: 12 additions & 12 deletions packages/kbn-analytics/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ReportManager {
}
assignReports(newMetrics: Metric | Metric[]) {
wrapArray(newMetrics).forEach(newMetric => this.assignReport(this.report, newMetric));
return { report: this.report };
}
static createMetricKey(metric: Metric): string {
switch (metric.type) {
Expand All @@ -101,7 +102,7 @@ export class ReportManager {
case METRIC_TYPE.USER_AGENT: {
const { appName, type, userAgent } = metric;
if (userAgent) {
this.report.userAgent = {
report.userAgent = {
[key]: {
key,
appName,
Expand All @@ -110,23 +111,22 @@ export class ReportManager {
},
};
}

return;
}
case METRIC_TYPE.CLICK:
case METRIC_TYPE.LOADED:
case METRIC_TYPE.COUNT: {
const { appName, type, eventName, count } = metric;
if (report.uiStatsMetrics) {
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
this.report.uiStatsMetrics = this.report.uiStatsMetrics || {};
this.report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
}
report.uiStatsMetrics = report.uiStatsMetrics || {};
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
return;
}
default:
Expand Down
26 changes: 9 additions & 17 deletions test/api_integration/apis/ui_metric/ui_metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ export default function({ getService }) {
const es = getService('legacyEs');

const createStatsMetric = eventName => ({
key: ReportManager.createMetricKey({ appName: 'myApp', type: METRIC_TYPE.CLICK, eventName }),
eventName,
appName: 'myApp',
type: METRIC_TYPE.CLICK,
stats: { sum: 1, avg: 1, min: 1, max: 1 },
count: 1,
});

const createUserAgentMetric = appName => ({
key: ReportManager.createMetricKey({ appName, type: METRIC_TYPE.USER_AGENT }),
appName,
type: METRIC_TYPE.USER_AGENT,
userAgent:
Expand All @@ -42,12 +40,9 @@ export default function({ getService }) {

describe('ui_metric API', () => {
it('increments the count field in the document defined by the {app}/{action_type} path', async () => {
const reportManager = new ReportManager();
const uiStatsMetric = createStatsMetric('myEvent');
const report = {
uiStatsMetrics: {
[uiStatsMetric.key]: uiStatsMetric,
},
};
const { report } = reportManager.assignReports([uiStatsMetric]);
await supertest
.post('/api/ui_metric/report')
.set('kbn-xsrf', 'kibana')
Expand All @@ -61,21 +56,18 @@ export default function({ getService }) {
});

it('supports multiple events', async () => {
const reportManager = new ReportManager();
const userAgentMetric = createUserAgentMetric('kibana');
const uiStatsMetric1 = createStatsMetric('myEvent');
const hrTime = process.hrtime();
const nano = hrTime[0] * 1000000000 + hrTime[1];
const uniqueEventName = `myEvent${nano}`;
const uiStatsMetric2 = createStatsMetric(uniqueEventName);
const report = {
userAgent: {
[userAgentMetric.key]: userAgentMetric,
},
uiStatsMetrics: {
[uiStatsMetric1.key]: uiStatsMetric1,
[uiStatsMetric2.key]: uiStatsMetric2,
},
};
const { report } = reportManager.assignReports([
userAgentMetric,
uiStatsMetric1,
uiStatsMetric2,
]);
await supertest
.post('/api/ui_metric/report')
.set('kbn-xsrf', 'kibana')
Expand Down