Skip to content
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

refactor(CLI): Modern logs for metrics command #10051

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/plugins/aws/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require('chalk');
const _ = require('lodash');
const dayjs = require('dayjs');
const validate = require('./lib/validate');
const { legacy, writeText, style } = require('@serverless/utils/log');

const LocalizedFormat = require('dayjs/plugin/localizedFormat');

Expand Down Expand Up @@ -100,16 +101,20 @@ class AwsMetrics {

showMetrics(metrics) {
let message = '';
const modernMessageTokens = [];

if (this.options.function) {
message += `${chalk.yellow.underline(this.options.function)}\n`;
modernMessageTokens.push(this.options.function);
} else {
message += `${chalk.yellow.underline('Service wide metrics')}\n`;
modernMessageTokens.push('Service wide metrics');
}

const formattedStartTime = dayjs(this.options.startTime).format('LLL');
const formattedEndTime = dayjs(this.options.endTime).format('LLL');
message += `${formattedStartTime} - ${formattedEndTime}\n\n`;
modernMessageTokens.push(`${formattedStartTime} - ${formattedEndTime}\n`);

if (metrics && metrics.length > 0) {
const getDatapointsByLabel = (Label) =>
Expand All @@ -129,11 +134,19 @@ class AwsMetrics {
message += `${chalk.yellow('Throttles:', throttlesCount, '\n')}`;
message += `${chalk.yellow('Errors:', errorsCount, '\n')}`;
message += `${chalk.yellow('Duration (avg.):', `${Number(durationAverage.toFixed(2))}ms`)}`;
modernMessageTokens.push(
`${style.aside('invocations')}: ${invocationsCount}`,
`${style.aside('throttles')}: ${throttlesCount}`,
`${style.aside('errors')}: ${errorsCount}`,
`${style.aside('duration (avg.)')}: ${Number(durationAverage.toFixed(2))}ms`
);
} else {
message += `${chalk.yellow('There are no metrics to show for these options')}`;
modernMessageTokens.push('There are no metrics to show for these options');
}

this.serverless.cli.consoleLog(message);
legacy.consoleLog(message);
writeText(modernMessageTokens);
}
}

Expand Down
194 changes: 0 additions & 194 deletions test/unit/lib/plugins/aws/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const AwsProvider = require('../../../../../lib/plugins/aws/provider');
const AwsMetrics = require('../../../../../lib/plugins/aws/metrics');
const Serverless = require('../../../../../lib/Serverless');
const CLI = require('../../../../../lib/classes/CLI');
const chalk = require('chalk');
const dayjs = require('dayjs');

const LocalizedFormat = require('dayjs/plugin/localizedFormat');
Expand Down Expand Up @@ -371,197 +370,4 @@ describe('AwsMetrics', () => {
).to.equal(true);
});
});

describe('#showMetrics()', () => {
let consoleLogStub;

beforeEach(() => {
awsMetrics.serverless.service.functions = {
function1: {
name: 'func1',
},
function2: {
name: 'func2',
},
};
awsMetrics.options.startTime = '1970-01-01';
awsMetrics.options.endTime = '1970-01-02';
consoleLogStub = sinon.stub(serverless.cli, 'consoleLog').returns();
});

afterEach(() => {
serverless.cli.consoleLog.restore();
});

it('should display service wide metrics if no function option is specified', () => {
const metrics = [
[
{
ResponseMetadata: {
RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func1',
},
Label: 'Invocations',
Datapoints: [{ Sum: 12 }, { Sum: 8 }],
},
{
ResponseMetadata: {
RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func1',
},
Label: 'Throttles',
Datapoints: [{ Sum: 15 }, { Sum: 15 }],
},
{
ResponseMetadata: {
RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func1',
},
Label: 'Errors',
Datapoints: [{ Sum: 0 }],
},
{
ResponseMetadata: {
RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func1',
},
Label: 'Duration',
Datapoints: [{ Average: 1000 }],
},
],
[
{
ResponseMetadata: {
RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func2',
},
Label: 'Invocations',
Datapoints: [{ Sum: 12 }, { Sum: 8 }],
},
{
ResponseMetadata: {
RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func2',
},
Label: 'Throttles',
Datapoints: [{ Sum: 15 }, { Sum: 15 }],
},
{
ResponseMetadata: {
RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func2',
},
Label: 'Errors',
Datapoints: [{ Sum: 0 }],
},
{
ResponseMetadata: {
RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func2',
},
Label: 'Duration',
Datapoints: [{ Average: 1000 }],
},
],
];

let expectedMessage = '';
expectedMessage += `${chalk.yellow.underline('Service wide metrics')}\n`;
expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n';
expectedMessage += `${chalk.yellow('Invocations: 40 \n')}`;
expectedMessage += `${chalk.yellow('Throttles: 60 \n')}`;
expectedMessage += `${chalk.yellow('Errors: 0 \n')}`;
expectedMessage += `${chalk.yellow('Duration (avg.): 1000ms')}`;

awsMetrics.showMetrics(metrics);

expect(consoleLogStub.calledOnce).to.equal(true);
expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage);
});

it('should display correct average of service wide average function duration', () => {
const metrics = [
[
{
Label: 'Duration',
Datapoints: [{ Average: 100 }, { Average: 200 }, { Average: 300 }],
},
],
[
{
Label: 'Duration',
Datapoints: [{ Average: 400 }, { Average: 500 }],
},
],
];

awsMetrics.showMetrics(metrics);

expect(consoleLogStub.getCall(0).args[0]).to.include('Duration (avg.): 300ms');
});

it('should display 0 as average function duration if no data by given period', () => {
const metrics = [[], []];

awsMetrics.showMetrics(metrics);

expect(consoleLogStub.getCall(0).args[0]).to.include('Duration (avg.): 0ms');
});

it('should display function metrics if function option is specified', () => {
awsMetrics.options.function = 'function1';

const metrics = [
[
{
ResponseMetadata: {
RequestId: '1f50045b-b569-11e6-86c6-eb54d1aaa755-func1',
},
Label: 'Invocations',
Datapoints: [{ Sum: 12 }, { Sum: 8 }],
},
{
ResponseMetadata: {
RequestId: '1f59059b-b569-11e6-aa18-c7bab68810d2-func1',
},
Label: 'Throttles',
Datapoints: [{ Sum: 15 }, { Sum: 15 }],
},
{
ResponseMetadata: {
RequestId: '1f50c7b1-b569-11e6-b1b6-ab86694b617b-func1',
},
Label: 'Errors',
Datapoints: [{ Sum: 0 }],
},
{
ResponseMetadata: {
RequestId: '1f63db14-b569-11e6-8501-d98a275ce164-func1',
},
Label: 'Duration',
Datapoints: [{ Average: 1000 }],
},
],
];

let expectedMessage = '';
expectedMessage += `${chalk.yellow.underline(awsMetrics.options.function)}\n`;
expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n';
expectedMessage += `${chalk.yellow('Invocations: 20 \n')}`;
expectedMessage += `${chalk.yellow('Throttles: 30 \n')}`;
expectedMessage += `${chalk.yellow('Errors: 0 \n')}`;
expectedMessage += `${chalk.yellow('Duration (avg.): 1000ms')}`;

awsMetrics.showMetrics(metrics);

expect(consoleLogStub.calledOnce).to.equal(true);
expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage);
});

it('should resolve with an error message if no metrics are available', () => {
awsMetrics.options.function = 'function1';

let expectedMessage = '';
expectedMessage += `${chalk.yellow.underline(awsMetrics.options.function)}\n`;
expectedMessage += 'January 1, 1970 12:00 AM - January 2, 1970 12:00 AM\n\n';
expectedMessage += `${chalk.yellow('There are no metrics to show for these options')}`;

awsMetrics.showMetrics();

expect(consoleLogStub.calledOnce).to.equal(true);
expect(consoleLogStub.getCall(0).args[0]).to.equal(expectedMessage);
});
});
});