Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Add overall frontend metrics for actions (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu authored Jan 6, 2021
1 parent e116bda commit 89a5fcf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
7 changes: 5 additions & 2 deletions kibana-reports/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report', 'create', 'count');
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
let report = request.body;
Expand Down Expand Up @@ -111,6 +112,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report', 'download', 'count');
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
let report: any;
Expand Down Expand Up @@ -171,6 +173,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report', 'create_from_definition', 'count');
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
const reportDefinitionId = request.params.reportDefinitionId;
Expand Down Expand Up @@ -235,6 +238,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report', 'list', 'count');
const { fromIndex, maxItems } = request.query as {
fromIndex: number;
maxItems: number;
Expand All @@ -254,7 +258,6 @@ export default function (router: IRouter) {
);

const reportsList = backendToUiReportsList(esResp.reportInstanceList);
addToMetric('report', 'list', 'count');

return response.ok({
body: {
Expand Down Expand Up @@ -287,6 +290,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report', 'info', 'count');
try {
// @ts-ignore
const esReportsClient: ILegacyScopedClusterClient = context.reporting_plugin.esReportsClient.asScoped(
Expand All @@ -301,7 +305,6 @@ export default function (router: IRouter) {
);

const report = backendToUiReport(esResp.reportInstance);
addToMetric('report', 'info', 'count');

return response.ok({
body: report,
Expand Down
10 changes: 5 additions & 5 deletions kibana-reports/server/routes/reportDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report_definition', 'create', 'count');
let reportDefinition = request.body;
//@ts-ignore
const logger = context.reporting_plugin.logger;
Expand All @@ -70,7 +71,6 @@ export default function (router: IRouter) {
reportDefinition
);

addToMetric('report_definition', 'create', 'count');
return response.ok({
body: {
state: 'Report definition created',
Expand Down Expand Up @@ -101,6 +101,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report_definition', 'update', 'count');
let reportDefinition = request.body;
//@ts-ignore
const logger = context.reporting_plugin.logger;
Expand All @@ -124,7 +125,6 @@ export default function (router: IRouter) {
reportDefinition
);

addToMetric('report_definition', 'update', 'count');
return response.ok({
body: {
state: 'Report definition updated',
Expand Down Expand Up @@ -155,6 +155,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report_definition', 'list', 'count');
const { fromIndex, maxItems } = request.query as {
fromIndex: number;
maxItems: number;
Expand All @@ -177,7 +178,6 @@ export default function (router: IRouter) {
const reportDefinitionsList = backendToUiReportDefinitionsList(
esResp.reportDefinitionDetailsList
);
addToMetric('report_definition', 'list', 'count');

return response.ok({
body: {
Expand Down Expand Up @@ -210,6 +210,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report_definition', 'info', 'count');
try {
// @ts-ignore
const esReportsClient: ILegacyScopedClusterClient = context.reporting_plugin.esReportsClient.asScoped(
Expand All @@ -226,7 +227,6 @@ export default function (router: IRouter) {
const reportDefinition = backendToUiReportDefinition(
esResp.reportDefinitionDetails
);
addToMetric('report_definition', 'info', 'count');

return response.ok({
body: { report_definition: reportDefinition },
Expand Down Expand Up @@ -257,6 +257,7 @@ export default function (router: IRouter) {
request,
response
): Promise<IKibanaResponse<any | ResponseError>> => {
addToMetric('report_definition', 'delete', 'count');
try {
// @ts-ignore
const esReportsClient: ILegacyScopedClusterClient = context.reporting_plugin.esReportsClient.asScoped(
Expand All @@ -269,7 +270,6 @@ export default function (router: IRouter) {
reportDefinitionId: request.params.reportDefinitionId,
}
);
addToMetric('report_definition', 'delete', 'count');

return response.ok({
body: {
Expand Down
47 changes: 39 additions & 8 deletions kibana-reports/server/routes/utils/metricHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,42 @@ const getPreKey = (milliseconds: number) => {
return getKey(milliseconds) - 1;
};

const isEntity = (arg: string): arg is EntityType => {
return (
arg === 'report' || arg === 'report_definition' || arg === 'report_source'
);
};

const buildMetrics = (rollingCounters: CountersType | undefined) => {
if (!rollingCounters) {
rollingCounters = DEFAULT_ROLLING_COUNTER;
}
return _.merge(rollingCounters, GLOBAL_BASIC_COUNTER);
const basicMetrics = _.merge(rollingCounters, GLOBAL_BASIC_COUNTER);
const overallActionMetrics = {
request_total: 0,
request_count: 0,
success_count: 0,
failed_request_count_system_error: 0,
failed_request_count_user_error: 0,
};
Object.keys(basicMetrics).forEach((keys) => {
if (isEntity(keys)) {
for (const [action, counters] of Object.entries(basicMetrics[keys])) {
overallActionMetrics.request_count += counters?.count || 0;
overallActionMetrics.request_total += counters?.total || 0;
overallActionMetrics.failed_request_count_system_error +=
counters?.system_error || 0;
overallActionMetrics.failed_request_count_user_error +=
counters?.user_error || 0;
}
}
});
overallActionMetrics.success_count =
overallActionMetrics.request_count -
(overallActionMetrics.failed_request_count_system_error +
overallActionMetrics.failed_request_count_user_error);

return { ...basicMetrics, ...overallActionMetrics };
};

const updateCounters = (
Expand Down Expand Up @@ -120,14 +151,14 @@ const updateCounters = (
'download'
]['total']++;
}
}
// update action metric per API
// @ts-ignore
rollingCounter[entity][action][counter] += count;
if (counter === 'count') {
} else {
// update action metric, per API
// @ts-ignore
GLOBAL_BASIC_COUNTER[entity][action]['total']++;
rollingCounter[entity][action][counter] += count;
if (counter === 'count') {
// @ts-ignore
GLOBAL_BASIC_COUNTER[entity][action]['total']++;
}
}

return rollingCounter;
};

0 comments on commit 89a5fcf

Please sign in to comment.