-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Telemetry for Visualizations by type #28793
Merged
tsullivan
merged 16 commits into
elastic:master
from
tsullivan:telemetry/visualization-types
Jan 30, 2019
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c64cba9
task runner and usage collector for visualizations by type
tsullivan 6e7214a
Merge branch 'master' into telemetry/visualization-types
tsullivan e5c0004
type is always just "visualization"
tsullivan 2ae0e31
Merge branch 'master' into telemetry/visualization-types
tsullivan 7e8adde
drop the I- prefix for interfaces
tsullivan c999577
bug fixes
tsullivan 6e14bd8
ts fix
tsullivan ddb9685
comment perfection
tsullivan d648e79
just usage.
tsullivan dad1216
const for task numworkers
tsullivan b26dda4
use mapValues
tsullivan 75eeb16
get next midnight module
tsullivan ed77b19
move to oss_telemtry
tsullivan fef2cb9
test fix
tsullivan 21b010a
Merge branch 'master' into telemetry/visualization-types
tsullivan 37a0f2f
errMessage.includes(NotInitialized)
tsullivan 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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 const PLUGIN_ID = 'oss_telemetry'; // prefix used for registering properties with services from this plugin | ||
export const VIS_TELEMETRY_TASK = 'vis_telemetry'; // suffix for the _id of our task instance, which must be `get`-able | ||
export const VIS_USAGE_TYPE = 'visualization_types'; // suffix for the properties of data registered with the usage service | ||
|
||
export const VIS_TELEMETRY_TASK_NUM_WORKERS = 10; // by default it's 100% their workers. Users can scale up and set task manager's numWorkers higher for other tasks to be able to run concurrently in a single Kibana instance with this one |
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,66 @@ | ||
/* | ||
* 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 interface VisState { | ||
type: string; | ||
} | ||
|
||
export interface Visualization { | ||
visState: string; | ||
} | ||
|
||
export interface SavedObjectDoc { | ||
_id: string; | ||
_source: { | ||
visualization: Visualization; | ||
type: string; | ||
}; | ||
} | ||
|
||
export interface ESQueryResponse { | ||
hits: { | ||
hits: SavedObjectDoc[]; | ||
}; | ||
} | ||
|
||
export interface TaskInstance { | ||
state: { | ||
runs: number; | ||
stats: any; | ||
}; | ||
error?: any; | ||
} | ||
|
||
export interface HapiServer { | ||
taskManager: { | ||
registerTaskDefinitions: (opts: any) => void; | ||
schedule: (opts: any) => Promise<void>; | ||
fetch: ( | ||
opts: any | ||
) => Promise<{ | ||
docs: TaskInstance[]; | ||
}>; | ||
}; | ||
plugins: { | ||
xpack_main: any; | ||
elasticsearch: { | ||
getCluster: ( | ||
cluster: string | ||
) => { | ||
callWithInternalUser: () => Promise<ESQueryResponse>; | ||
}; | ||
}; | ||
}; | ||
usage: { | ||
collectorSet: { | ||
register: (collector: any) => void; | ||
makeUsageCollector: (collectorOpts: any) => void; | ||
}; | ||
}; | ||
config: () => { | ||
get: (prop: string) => any; | ||
}; | ||
} |
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,22 @@ | ||
/* | ||
* 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 { registerCollectors } from './server/lib/collectors'; | ||
import { registerTasks, scheduleTasks } from './server/lib/tasks'; | ||
import { PLUGIN_ID } from './constants'; | ||
|
||
export const ossTelemetry = (kibana) => { | ||
return new kibana.Plugin({ | ||
id: PLUGIN_ID, | ||
require: ['elasticsearch', 'xpack_main', 'task_manager'], | ||
|
||
init(server) { | ||
registerCollectors(server); | ||
registerTasks(server); | ||
scheduleTasks(server); | ||
} | ||
}); | ||
}; |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/oss_telemetry/server/lib/collectors/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,12 @@ | ||
/* | ||
* 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 { HapiServer } from '../../../'; | ||
import { registerVisualizationsCollector } from './visualizations/register_usage_collector'; | ||
|
||
export function registerCollectors(server: HapiServer) { | ||
registerVisualizationsCollector(server); | ||
} |
58 changes: 58 additions & 0 deletions
58
...ck/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.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,58 @@ | ||
/* | ||
* 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 sinon from 'sinon'; | ||
import { HapiServer } from '../../../../'; | ||
import { | ||
getMockCallWithInternal, | ||
getMockKbnServer, | ||
getMockTaskFetch, | ||
} from '../../../../test_utils'; | ||
import { getUsageCollector } from './get_usage_collector'; | ||
|
||
describe('getVisualizationsCollector#fetch', () => { | ||
let mockKbnServer: HapiServer; | ||
|
||
beforeEach(() => { | ||
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), getMockTaskFetch()); | ||
}); | ||
|
||
test('can return empty stats', async () => { | ||
const { type, fetch } = getUsageCollector(mockKbnServer); | ||
expect(type).toBe('visualization_types'); | ||
const fetchResult = await fetch(); | ||
expect(fetchResult).toEqual({}); | ||
}); | ||
|
||
test('provides known stats', async () => { | ||
const mockTaskFetch = getMockTaskFetch([ | ||
{ | ||
state: { | ||
runs: 1, | ||
stats: { comic_books: { total: 16, max: 12, min: 2, avg: 6 } }, | ||
}, | ||
}, | ||
]); | ||
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch); | ||
|
||
const { type, fetch } = getUsageCollector(mockKbnServer); | ||
expect(type).toBe('visualization_types'); | ||
const fetchResult = await fetch(); | ||
expect(fetchResult).toEqual({ comic_books: { avg: 6, max: 12, min: 2, total: 16 } }); | ||
}); | ||
|
||
describe('Error handling', () => { | ||
// In real life, the CollectorSet calls fetch and handles errors | ||
test('defers the errors', async () => { | ||
const mockTaskFetch = sinon.stub(); | ||
mockTaskFetch.rejects(new Error('BOOM')); | ||
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch); | ||
|
||
const { fetch } = getUsageCollector(mockKbnServer); | ||
await expect(fetch()).rejects.toMatchObject(new Error('BOOM')); | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.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,39 @@ | ||
/* | ||
* 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 { get } from 'lodash'; | ||
import { HapiServer } from '../../../../'; | ||
import { PLUGIN_ID, VIS_TELEMETRY_TASK, VIS_USAGE_TYPE } from '../../../../constants'; | ||
|
||
export function getUsageCollector(server: HapiServer) { | ||
const { taskManager } = server; | ||
return { | ||
type: VIS_USAGE_TYPE, | ||
fetch: async () => { | ||
let docs; | ||
try { | ||
({ docs } = await taskManager.fetch({ | ||
query: { bool: { filter: { term: { _id: `${PLUGIN_ID}-${VIS_TELEMETRY_TASK}` } } } }, | ||
})); | ||
} catch (err) { | ||
const errMessage = err && err.message ? err.message : err.toString(); | ||
/* | ||
* The usage service WILL to try to fetch from this collector before the task manager has been initialized, because the task manager | ||
* has to wait for all plugins to initialize first. | ||
* It's fine to ignore it as next time around it will be initialized (or it will throw a different type of error) | ||
*/ | ||
if (errMessage.indexOf('NotInitialized') >= 0) { | ||
docs = {}; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
|
||
// get the accumulated state from the recurring task | ||
return get(docs, '[0].state.stats'); | ||
}, | ||
}; | ||
} |
14 changes: 14 additions & 0 deletions
14
...ck/plugins/oss_telemetry/server/lib/collectors/visualizations/register_usage_collector.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,14 @@ | ||
/* | ||
* 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 { HapiServer } from '../../../../'; | ||
import { getUsageCollector } from './get_usage_collector'; | ||
|
||
export function registerVisualizationsCollector(server: HapiServer): void { | ||
const { usage } = server; | ||
const collector = usage.collectorSet.makeUsageCollector(getUsageCollector(server)); | ||
usage.collectorSet.register(collector); | ||
} |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/oss_telemetry/server/lib/get_next_midnight.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,19 @@ | ||
/* | ||
* 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 moment from 'moment'; | ||
import { getNextMidnight } from './get_next_midnight'; | ||
|
||
describe('getNextMidnight', () => { | ||
test('Returns the next time and date of midnight as an iso string', () => { | ||
const nextMidnightMoment = moment() | ||
.add(1, 'days') | ||
.startOf('day') | ||
.toISOString(); | ||
|
||
expect(getNextMidnight()).toEqual(nextMidnightMoment); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/oss_telemetry/server/lib/get_next_midnight.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,12 @@ | ||
/* | ||
* 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 function getNextMidnight() { | ||
const nextMidnight = new Date(); | ||
nextMidnight.setHours(0, 0, 0, 0); | ||
nextMidnight.setDate(nextMidnight.getDate() + 1); | ||
return nextMidnight.toISOString(); | ||
} |
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,39 @@ | ||
/* | ||
* 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 { HapiServer } from '../../../'; | ||
import { PLUGIN_ID, VIS_TELEMETRY_TASK, VIS_TELEMETRY_TASK_NUM_WORKERS } from '../../../constants'; | ||
import { visualizationsTaskRunner } from './visualizations/task_runner'; | ||
|
||
export function registerTasks(server: HapiServer) { | ||
const { taskManager } = server; | ||
|
||
taskManager.registerTaskDefinitions({ | ||
[VIS_TELEMETRY_TASK]: { | ||
title: 'X-Pack telemetry calculator for Visualizations', | ||
type: VIS_TELEMETRY_TASK, | ||
numWorkers: VIS_TELEMETRY_TASK_NUM_WORKERS, // by default it's 100% their workers | ||
createTaskRunner({ taskInstance, kbnServer }: { kbnServer: any; taskInstance: any }) { | ||
return { | ||
run: visualizationsTaskRunner(taskInstance, kbnServer), | ||
}; | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export function scheduleTasks(server: HapiServer) { | ||
const { taskManager } = server; | ||
const { kbnServer } = server.plugins.xpack_main.status.plugin; | ||
|
||
kbnServer.afterPluginsInit(() => { | ||
taskManager.schedule({ | ||
id: `${PLUGIN_ID}-${VIS_TELEMETRY_TASK}`, | ||
taskType: VIS_TELEMETRY_TASK, | ||
state: { stats: {}, runs: 0 }, | ||
}); | ||
}); | ||
} |
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.
errMessage.includes('NotInitialized')
might read a bit better here (might also be sub-ms faster as well?)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.
Also might want a test for this since it's interesting behavior (not sure how difficult that is)