forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI telemetry tracking to AS in Kibana (elastic#5)
* Set up Telemetry usageCollection, savedObjects, route, & shared helper - The Kibana UsageCollection plugin handles collecting our telemetry UI data (views, clicks, errors, etc.) and pushing it to elastic's telemetry servers - That data is stored in incremented in Kibana's savedObjects lib/plugin (as well as mapped) - When an end-user hits a certain view or action, the shared helper will ping the app search telemetry route which increments the savedObject store * Update client-side views/links to new shared telemetry helper * Write tests for new telemetry files
- Loading branch information
Showing
16 changed files
with
640 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/enterprise_search/public/applications/shared/telemetry/index.ts
This file contains 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,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { sendTelemetry } from './send_telemetry'; | ||
export { SendAppSearchTelemetry } from './send_telemetry'; |
56 changes: 56 additions & 0 deletions
56
...ck/plugins/enterprise_search/public/applications/shared/telemetry/send_telemetry.test.tsx
This file contains 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,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { httpServiceMock } from 'src/core/public/mocks'; | ||
import { mountWithKibanaContext } from '../../test_utils/helpers'; | ||
import { sendTelemetry, SendAppSearchTelemetry } from './'; | ||
|
||
describe('Shared Telemetry Helpers', () => { | ||
const httpMock = httpServiceMock.createSetupContract(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('sendTelemetry', () => { | ||
it('successfully calls the server-side telemetry endpoint', () => { | ||
sendTelemetry({ | ||
http: httpMock, | ||
product: 'enterprise_search', | ||
action: 'viewed', | ||
metric: 'setup_guide', | ||
}); | ||
|
||
expect(httpMock.put).toHaveBeenCalledWith('/api/enterprise_search/telemetry', { | ||
headers: { 'content-type': 'application/json' }, | ||
body: '{"action":"viewed","metric":"setup_guide"}', | ||
}); | ||
}); | ||
|
||
it('throws an error if the telemetry endpoint fails', () => { | ||
const httpRejectMock = { put: () => Promise.reject() }; | ||
|
||
expect(sendTelemetry({ http: httpRejectMock })).rejects.toThrow('Unable to send telemetry'); | ||
}); | ||
}); | ||
|
||
describe('React component helpers', () => { | ||
it('SendAppSearchTelemetry component', () => { | ||
const wrapper = mountWithKibanaContext( | ||
<SendAppSearchTelemetry action="clicked" metric="button" />, | ||
{ http: httpMock } | ||
); | ||
|
||
expect(httpMock.put).toHaveBeenCalledWith('/api/app_search/telemetry', { | ||
headers: { 'content-type': 'application/json' }, | ||
body: '{"action":"clicked","metric":"button"}', | ||
}); | ||
}); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/enterprise_search/public/applications/shared/telemetry/send_telemetry.tsx
This file contains 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,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useContext, useEffect } from 'react'; | ||
|
||
import { HttpHandler } from 'src/core/public'; | ||
import { KibanaContext, IKibanaContext } from '../../index'; | ||
|
||
interface ISendTelemetryProps { | ||
action: 'viewed' | 'error' | 'clicked'; | ||
metric: string; // e.g., 'setup_guide' | ||
} | ||
|
||
interface ISendTelemetry extends ISendTelemetryProps { | ||
http(): HttpHandler; | ||
product: 'app_search' | 'workplace_search' | 'enterprise_search'; | ||
} | ||
|
||
/** | ||
* Base function - useful for non-component actions, e.g. clicks | ||
*/ | ||
|
||
export const sendTelemetry = async ({ http, product, action, metric }: ISendTelemetry) => { | ||
try { | ||
await http.put(`/api/${product}/telemetry`, { | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ action, metric }), | ||
}); | ||
} catch (error) { | ||
throw new Error('Unable to send telemetry'); | ||
} | ||
}; | ||
|
||
/** | ||
* React component helpers - useful for on-page-load/views | ||
* TODO: SendWorkplaceSearchTelemetry and SendEnterpriseSearchTelemetry | ||
*/ | ||
|
||
export const SendAppSearchTelemetry: React.FC<ISendTelemetryProps> = ({ action, metric }) => { | ||
const { http } = useContext(KibanaContext) as IKibanaContext; | ||
|
||
useEffect(() => { | ||
sendTelemetry({ http, action, metric, product: 'app_search' }); | ||
}, [action, metric, http]); | ||
|
||
return null; | ||
}; |
118 changes: 118 additions & 0 deletions
118
x-pack/plugins/enterprise_search/server/collectors/app_search/telemetry.test.ts
This file contains 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,118 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { registerTelemetryUsageCollector, incrementUICounter } from './telemetry'; | ||
|
||
/** | ||
* Since these route callbacks are so thin, these serve simply as integration tests | ||
* to ensure they're wired up to the lib functions correctly. Business logic is tested | ||
* more thoroughly in the lib/telemetry tests. | ||
*/ | ||
describe('App Search Telemetry Usage Collector', () => { | ||
const makeUsageCollectorStub = jest.fn(); | ||
const registerStub = jest.fn(); | ||
|
||
const savedObjectsRepoStub = { | ||
get: () => ({ | ||
attributes: { | ||
'ui_viewed.setup_guide': 10, | ||
'ui_viewed.engines_overview': 20, | ||
'ui_error.cannot_connect': 3, | ||
'ui_error.no_as_account': 4, | ||
'ui_clicked.header_launch_button': 50, | ||
'ui_clicked.engine_table_link': 60, | ||
}, | ||
}), | ||
incrementCounter: jest.fn(), | ||
}; | ||
const dependencies = { | ||
usageCollection: { | ||
makeUsageCollector: makeUsageCollectorStub, | ||
registerCollector: registerStub, | ||
}, | ||
savedObjects: { | ||
createInternalRepository: jest.fn(() => savedObjectsRepoStub), | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('registerTelemetryUsageCollector', () => { | ||
it('should make and register the usage collector', () => { | ||
registerTelemetryUsageCollector(dependencies); | ||
|
||
expect(registerStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub.mock.calls[0][0].type).toBe('app_search_kibana_telemetry'); | ||
}); | ||
}); | ||
|
||
describe('fetchTelemetryMetrics', () => { | ||
it('should return existing saved objects data', async () => { | ||
registerTelemetryUsageCollector(dependencies); | ||
const savedObjectsCounts = await makeUsageCollectorStub.mock.calls[0][0].fetch(); | ||
|
||
expect(savedObjectsCounts).toEqual({ | ||
ui_viewed: { | ||
setup_guide: 10, | ||
engines_overview: 20, | ||
}, | ||
ui_error: { | ||
cannot_connect: 3, | ||
no_as_account: 4, | ||
}, | ||
ui_clicked: { | ||
header_launch_button: 50, | ||
engine_table_link: 60, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should not error & should return a default telemetry object if no saved data exists', async () => { | ||
registerTelemetryUsageCollector({ | ||
...dependencies, | ||
savedObjects: { createInternalRepository: () => ({}) }, | ||
}); | ||
const savedObjectsCounts = await makeUsageCollectorStub.mock.calls[0][0].fetch(); | ||
|
||
expect(savedObjectsCounts).toEqual({ | ||
ui_viewed: { | ||
setup_guide: 0, | ||
engines_overview: 0, | ||
}, | ||
ui_error: { | ||
cannot_connect: 0, | ||
no_as_account: 0, | ||
}, | ||
ui_clicked: { | ||
header_launch_button: 0, | ||
engine_table_link: 0, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('incrementUICounter', () => { | ||
it('should increment the saved objects internal repository', async () => { | ||
const { savedObjects } = dependencies; | ||
|
||
const response = await incrementUICounter({ | ||
savedObjects, | ||
uiAction: 'ui_clicked', | ||
metric: 'button', | ||
}); | ||
|
||
expect(savedObjectsRepoStub.incrementCounter).toHaveBeenCalledWith( | ||
'app_search_kibana_telemetry', | ||
'app_search_kibana_telemetry', | ||
'ui_clicked.button' | ||
); | ||
expect(response).toEqual({ success: true }); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.