-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* first pass of basic osquery usage stats collection * updates, linting * updated exported metrics * clean up comments, add description fields to metric fields * reworked types * actually use the updated types * added tests around the route usage recoder functions * review comments * update aggregate types Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Bryan Clement <bclement01@gmail.com>
- Loading branch information
1 parent
22762a8
commit e496e85
Showing
16 changed files
with
859 additions
and
24 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
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export * from './recorder'; |
135 changes: 135 additions & 0 deletions
135
x-pack/plugins/osquery/server/routes/usage/recorder.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,135 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; | ||
|
||
import { usageMetricSavedObjectType } from '../../../common/types'; | ||
|
||
import { | ||
CounterValue, | ||
createMetricObjects, | ||
getRouteMetric, | ||
incrementCount, | ||
RouteString, | ||
routeStrings, | ||
} from './recorder'; | ||
|
||
const savedObjectsClient = savedObjectsClientMock.create(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function checkGetCalls(calls: any[]) { | ||
expect(calls.length).toEqual(routeStrings.length); | ||
for (let i = 0; i < routeStrings.length; ++i) { | ||
expect(calls[i]).toEqual([usageMetricSavedObjectType, routeStrings[i]]); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function checkCreateCalls(calls: any[], expectedCallRoutes: string[] = routeStrings) { | ||
expect(calls.length).toEqual(expectedCallRoutes.length); | ||
for (let i = 0; i < expectedCallRoutes.length; ++i) { | ||
expect(calls[i][0]).toEqual(usageMetricSavedObjectType); | ||
expect(calls[i][2].id).toEqual(expectedCallRoutes[i]); | ||
} | ||
} | ||
|
||
describe('Usage metric recorder', () => { | ||
describe('Metric initalizer', () => { | ||
const get = savedObjectsClient.get as jest.Mock; | ||
const create = savedObjectsClient.create as jest.Mock; | ||
afterEach(() => { | ||
get.mockClear(); | ||
create.mockClear(); | ||
}); | ||
it('should seed route metrics objects', async () => { | ||
get.mockRejectedValueOnce('stub value'); | ||
create.mockReturnValueOnce('stub value'); | ||
const result = await createMetricObjects(savedObjectsClient); | ||
checkGetCalls(get.mock.calls); | ||
checkCreateCalls(create.mock.calls); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should handle previously seeded objects properly', async () => { | ||
get.mockReturnValueOnce('stub value'); | ||
create.mockRejectedValueOnce('stub value'); | ||
const result = await createMetricObjects(savedObjectsClient); | ||
checkGetCalls(get.mock.calls); | ||
checkCreateCalls(create.mock.calls, []); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should report failure to create the metrics object', async () => { | ||
get.mockRejectedValueOnce('stub value'); | ||
create.mockRejectedValueOnce('stub value'); | ||
const result = await createMetricObjects(savedObjectsClient); | ||
checkGetCalls(get.mock.calls); | ||
checkCreateCalls(create.mock.calls); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Incrementation', () => { | ||
let counterMap: { [key: string]: CounterValue }; | ||
const get = savedObjectsClient.get as jest.Mock; | ||
const update = savedObjectsClient.update as jest.Mock; | ||
update.mockImplementation( | ||
async (objectType: string, route: RouteString, newVal: CounterValue) => { | ||
counterMap[`${objectType}-${route}`] = newVal; | ||
} | ||
); | ||
get.mockImplementation(async (objectType: string, route: RouteString) => ({ | ||
attributes: counterMap[`${objectType}-${route}`], | ||
})); | ||
beforeEach(() => { | ||
counterMap = routeStrings.reduce((acc, route) => { | ||
acc[`${usageMetricSavedObjectType}-${route}`] = { | ||
count: 0, | ||
errors: 0, | ||
}; | ||
return acc; | ||
}, {} as { [key: string]: CounterValue }); | ||
get.mockClear(); | ||
update.mockClear(); | ||
}); | ||
it('should increment the route counter', async () => { | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 0, | ||
errors: 0, | ||
}); | ||
await incrementCount(savedObjectsClient, 'live_query'); | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 1, | ||
errors: 0, | ||
}); | ||
}); | ||
|
||
it('should allow incrementing the error counter', async () => { | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 0, | ||
errors: 0, | ||
}); | ||
await incrementCount(savedObjectsClient, 'live_query', 'errors'); | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 0, | ||
errors: 1, | ||
}); | ||
}); | ||
|
||
it('should allow adjustment of the increment', async () => { | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 0, | ||
errors: 0, | ||
}); | ||
await incrementCount(savedObjectsClient, 'live_query', 'count', 2); | ||
expect(await getRouteMetric(savedObjectsClient, 'live_query')).toEqual({ | ||
count: 2, | ||
errors: 0, | ||
}); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'kibana/server'; | ||
import { usageMetricSavedObjectType } from '../../../common/types'; | ||
import { LiveQuerySessionUsage } from '../../usage/types'; | ||
|
||
export interface RouteUsageMetric { | ||
queries: number; | ||
errors: number; | ||
} | ||
|
||
export type RouteString = 'live_query'; | ||
|
||
export const routeStrings: RouteString[] = ['live_query']; | ||
|
||
export async function createMetricObjects(soClient: SavedObjectsClientContract) { | ||
const res = await Promise.allSettled( | ||
routeStrings.map(async (route) => { | ||
try { | ||
await soClient.get(usageMetricSavedObjectType, route); | ||
} catch (e) { | ||
await soClient.create( | ||
usageMetricSavedObjectType, | ||
{ | ||
errors: 0, | ||
count: 0, | ||
}, | ||
{ | ||
id: route, | ||
} | ||
); | ||
} | ||
}) | ||
); | ||
return !res.some((e) => e.status === 'rejected'); | ||
} | ||
|
||
export async function getCount(soClient: SavedObjectsClientContract, route: RouteString) { | ||
return await soClient.get<LiveQuerySessionUsage>(usageMetricSavedObjectType, route); | ||
} | ||
|
||
export interface CounterValue { | ||
count: number; | ||
errors: number; | ||
} | ||
|
||
export async function incrementCount( | ||
soClient: SavedObjectsClientContract, | ||
route: RouteString, | ||
key: keyof CounterValue = 'count', | ||
increment = 1 | ||
) { | ||
const metric = await soClient.get<CounterValue>(usageMetricSavedObjectType, route); | ||
metric.attributes[key] += increment; | ||
await soClient.update(usageMetricSavedObjectType, route, metric.attributes); | ||
} | ||
|
||
export async function getRouteMetric(soClient: SavedObjectsClientContract, route: RouteString) { | ||
return (await getCount(soClient, route)).attributes; | ||
} |
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/osquery/server/routes/usage/saved_object_mappings.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,28 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { SavedObjectsType } from '../../../../../../src/core/server'; | ||
|
||
import { usageMetricSavedObjectType } from '../../../common/types'; | ||
|
||
export const usageMetricSavedObjectMappings: SavedObjectsType['mappings'] = { | ||
properties: { | ||
count: { | ||
type: 'long', | ||
}, | ||
errors: { | ||
type: 'long', | ||
}, | ||
}, | ||
}; | ||
|
||
export const usageMetricType: SavedObjectsType = { | ||
name: usageMetricSavedObjectType, | ||
hidden: false, | ||
namespaceType: 'single', | ||
mappings: usageMetricSavedObjectMappings, | ||
}; |
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
Oops, something went wrong.