-
Notifications
You must be signed in to change notification settings - Fork 21
/
list-kinesis-shards.js
188 lines (167 loc) · 4.89 KB
/
list-kinesis-shards.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const _ = require("lodash");
const AWS = require("aws-sdk");
const Table = require("cli-table");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const uuid = require("uuid/v4");
const { track } = require("../lib/analytics");
require("colors");
const ONE_HOUR_IN_SECONDS = 60 * 60;
class ListKinesisShardsCommand extends Command {
async run() {
const { flags } = this.parse(ListKinesisShardsCommand);
const { streamName, region, profile } = flags;
AWS.config.region = region;
if (profile) {
const credentials = new AWS.SharedIniFileCredentials({ profile });
AWS.config.credentials = credentials;
}
checkVersion();
track("list-kinesis-shards", { region, hasName: !_.isEmpty(streamName) });
this.log(`checking Kinesis stream [${streamName}] in [${region}]`);
const stream = await this.describeStream(streamName);
if (_.isEmpty(stream.enhancedMonitoring)) {
this.log("enhanced monitoring is", "disabled".red.bold);
this.log(
"hint: enable enhanced monitoring to see shard level metrics".italic
);
} else {
this.log("enhanced monitoring is", "enabled".green.bold);
const shardIds = stream.shards.map(x => x.ShardId);
const metrics = await this.getUsageMetrics(streamName, shardIds);
stream.shards.forEach(shard => {
shard.LastHourMetrics = _.get(metrics, shard.ShardId);
});
}
this.log(`arn: ${stream.arn}`);
this.log("status:", stream.status.green.bold);
this.show(stream.shards);
}
async describeStream(streamName) {
const Kinesis = new AWS.Kinesis();
const resp = await Kinesis.describeStream({
StreamName: streamName
}).promise();
return {
arn: resp.StreamDescription.StreamARN,
status: resp.StreamDescription.StreamStatus,
shards: resp.StreamDescription.Shards,
enhancedMonitoring:
resp.StreamDescription.EnhancedMonitoring[0].ShardLevelMetrics
};
}
async getUsageMetrics(streamName, shardsIds) {
const CloudWatch = new AWS.CloudWatch();
const metricNames = [
["IncomingBytes", "Average"],
["IncomingRecords", "Average"],
["OutgoingRecords", "Average"],
["OutgoingBytes", "Average"],
["ReadProvisionedThroughputExceeded", "Sum"],
["WriteProvisionedThroughputExceeded", "Sum"],
["IteratorAgeMilliseconds", "Average"]
];
const oneHourAgo = new Date();
oneHourAgo.setHours(oneHourAgo.getHours() - 1);
const queries = _.flatMap(shardsIds, shardId =>
metricNames.map(([metricName, stat]) => ({
Id:
metricName.toLowerCase() +
uuid()
.replace(/-/g, "")
.substr(0, 5),
Label: `${shardId}:${metricName}:${stat}`,
MetricStat: {
Metric: {
Dimensions: [
{
Name: "StreamName",
Value: streamName
},
{
Name: "ShardId",
Value: shardId
}
],
MetricName: metricName,
Namespace: "AWS/Kinesis"
},
Period: ONE_HOUR_IN_SECONDS,
Stat: stat
},
ReturnData: true
}))
);
// each GetMetricData request can send as many as 100 queries
const chunks = _.chunk(queries, 100);
const promises = chunks.map(async metricDataQueries => {
const resp = await CloudWatch.getMetricData({
StartTime: oneHourAgo,
EndTime: new Date(),
MetricDataQueries: metricDataQueries
}).promise();
return resp.MetricDataResults.map(res => {
const [shardId, metricName, stat] = res.Label.split(":");
const dataPoint = res.Values[0] || 0;
if (stat === "Sum") {
return {
shardId,
metricName: metricName + "Count",
dataPoint
};
} else {
// convert from per min average to per second
const perSecAverage = dataPoint / 60.0;
return {
shardId,
metricName: metricName + "PerSecond",
dataPoint: perSecAverage
};
}
});
});
// array of {shardId, metricName, dataPoint}
const results = _.flatMap(await Promise.all(promises));
const byShardId = _.groupBy(results, res => res.shardId);
return _.mapValues(byShardId, shardMetrics => {
const kvp = shardMetrics.map(({ metricName, dataPoint }) => [
metricName,
dataPoint
]);
return _.fromPairs(kvp);
});
}
show(shards) {
const table = new Table({
head: ["ShardId", "Details"]
});
shards.forEach(x => {
const details = _.clone(x);
delete details.ShardId;
table.push([
x.ShardId.replace("shardId-", ""),
JSON.stringify(details, undefined, 2)
]);
});
this.log(table.toString());
}
}
ListKinesisShardsCommand.description = "Lists the shards of a Kinesis stream";
ListKinesisShardsCommand.flags = {
streamName: flags.string({
char: "n",
description: "name of the Kinesis stream, e.g. event-stream-dev",
required: true
}),
region: flags.string({
char: "r",
description: "AWS region, e.g. us-east-1",
required: true
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
})
};
module.exports = ListKinesisShardsCommand;