Skip to content

Commit

Permalink
updates, linting
Browse files Browse the repository at this point in the history
  • Loading branch information
lykkin committed May 27, 2021
1 parent fa6c921 commit 21add4c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
CreateActionRequestBodySchema,
} from '../../../common/schemas/routes/action/create_action_request_body_schema';

import {getUsageRecorder} from '../usage'
import { getUsageRecorder } from '../usage';

export const createActionRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => {
router.post(
Expand All @@ -41,10 +41,10 @@ export const createActionRoute = (router: IRouter, osqueryContext: OsqueryAppCon
osqueryContext,
agentSelection
);
const usageRecorder = getUsageRecorder()
usageRecorder.incrementCallCount('live_query')
const usageRecorder = getUsageRecorder();
usageRecorder.incrementCallCount('live_query');
if (!selectedAgents.length) {
usageRecorder.incrementErrorCount('live_query')
usageRecorder.incrementErrorCount('live_query');
return response.badRequest({ body: new Error('No agents found for selection') });
}

Expand Down Expand Up @@ -73,12 +73,11 @@ export const createActionRoute = (router: IRouter, osqueryContext: OsqueryAppCon
},
});
} catch (error) {
usageRecorder.incrementErrorCount('live_query')
usageRecorder.incrementErrorCount('live_query');
return response.customError({
statusCode: 500,
body: new Error(`Error occurred whlie processing ${error}`) ,
body: new Error(`Error occurred whlie processing ${error}`),
});

}
}
);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/osquery/server/routes/usage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export * from './recorder'
export * from './recorder';
58 changes: 29 additions & 29 deletions x-pack/plugins/osquery/server/routes/usage/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@
*/

export interface RouteUsageMetric {
call_count: number;
error_count: number;
call_count: number;
error_count: number;
}

// TODO: use ES for this recording
class UsageRecorder {
private counts = new Map<string, number>()
public incrementCallCount(route: string, increment: number = 1) {
const count = this.counts.get(route) ?? 0
this.counts.set(route, count + increment)
}
public getCallCount(route: string): number {
return this.counts.get(route) ?? 0;
}
private counts = new Map<string, number>();
public incrementCallCount(route: string, increment = 1) {
const count = this.counts.get(route) ?? 0;
this.counts.set(route, count + increment);
}
public getCallCount(route: string): number {
return this.counts.get(route) ?? 0;
}

private errors = new Map<string, number>()
public incrementErrorCount(route: string, increment: number = 1) {
const count = this.errors.get(route) ?? 0
this.errors.set(route, count + increment)
}
public getErrorCount(route: string): number {
return this.errors.get(route) ?? 0;
}
private errors = new Map<string, number>();
public incrementErrorCount(route: string, increment = 1) {
const count = this.errors.get(route) ?? 0;
this.errors.set(route, count + increment);
}
public getErrorCount(route: string): number {
return this.errors.get(route) ?? 0;
}

public getRouteMetric(route: string): RouteUsageMetric {
return {
call_count: this.getCallCount(route),
error_count: this.getErrorCount(route)
}
}
public getRouteMetric(route: string): RouteUsageMetric {
return {
call_count: this.getCallCount(route),
error_count: this.getErrorCount(route),
};
}
}

let usageRecorder: UsageRecorder;

export const getUsageRecorder = (): UsageRecorder => {
if (usageRecorder == null) {
usageRecorder = new UsageRecorder()
}
return usageRecorder
}
if (usageRecorder == null) {
usageRecorder = new UsageRecorder();
}
return usageRecorder;
};
2 changes: 2 additions & 0 deletions x-pack/plugins/osquery/server/usage/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export const registerCollector: RegisterCollector = ({ usageCollection }) => {
if (!usageCollection) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const collector = usageCollection.makeUsageCollector<any>({
type: 'osquery',
schema: usageSchema,
isReady: () => true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fetch: async ({ esClient }: CollectorFetchContext): Promise<any> => {
return {
beat_metrics: {
Expand Down
23 changes: 12 additions & 11 deletions x-pack/plugins/osquery/server/usage/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

import {
SingleBucketAggregate,
TopHitsAggregate,
ValueAggregate,
SingleBucketAggregate,
TopHitsAggregate,
ValueAggregate,
} from '@elastic/elasticsearch/api/types';
import {getUsageRecorder} from '../routes/usage'
import { getUsageRecorder } from '../routes/usage';
import { ElasticsearchClient } from '../../../../../src/core/server';
import { METRICS_INDICES } from './constants';

Expand All @@ -27,8 +27,8 @@ export interface BeatMetricAggregation {

// TODO: pipe this through ES
export function getLiveQueryUsage() {
const usageRecorder = getUsageRecorder()
return usageRecorder.getRouteMetric('live_query')
const usageRecorder = getUsageRecorder();
return usageRecorder.getRouteMetric('live_query');
}

export async function getBeatUsage(esClient: ElasticsearchClient) {
Expand Down Expand Up @@ -96,23 +96,24 @@ export async function getBeatUsage(esClient: ElasticsearchClient) {

// XXX: discrimating the union types gets hairy when attempting to genericize, figure out a fix!
if ('max_rss' in lastDayAggs) {
result.rss.max = (lastDayAggs.max_rss as ValueAggregate).value
result.rss.max = (lastDayAggs.max_rss as ValueAggregate).value;
}

if ('avg_rss' in lastDayAggs) {
result.rss.avg = (lastDayAggs.max_rss as ValueAggregate).value
result.rss.avg = (lastDayAggs.max_rss as ValueAggregate).value;
}

if ('max_cpu' in lastDayAggs) {
result.cpuMs.max = (lastDayAggs.max_cpu as ValueAggregate).value
result.cpuMs.max = (lastDayAggs.max_cpu as ValueAggregate).value;
}

if ('avg_cpu' in lastDayAggs) {
result.cpuMs.avg = (lastDayAggs.max_cpu as ValueAggregate).value
result.cpuMs.avg = (lastDayAggs.max_cpu as ValueAggregate).value;
}

if ('latest' in lastDayAggs) {
const latest = (lastDayAggs.latest as TopHitsAggregate).hits.hits[0]?._source?.monitoring.metrics.beat;
const latest = (lastDayAggs.latest as TopHitsAggregate).hits.hits[0]?._source?.monitoring
.metrics.beat;
result.cpuMs.latest = latest.cpu.total.time.ms;
result.rss.latest = latest.memstats.rss;
}
Expand Down

0 comments on commit 21add4c

Please sign in to comment.