Skip to content

Commit a2ce6f5

Browse files
humanzzmadeline-k
authored andcommitted
feat(neptune): introduce metric method to cluster and instance (aws#21995)
closes aws#20248 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1d46ffd commit a2ce6f5

14 files changed

+384
-66
lines changed

packages/@aws-cdk/aws-neptune/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const cluster = new neptune.DatabaseCluster(this, 'Database', {
121121
});
122122
```
123123

124-
Additionally it is also possible to add replicas using `DatabaseInstance` for an existing cluster.
124+
Additionally, it is also possible to add replicas using `DatabaseInstance` for an existing cluster.
125125

126126
```ts fixture=with-cluster
127127
const replica1 = new neptune.DatabaseInstance(this, 'Instance', {
@@ -143,3 +143,17 @@ new neptune.DatabaseCluster(this, 'Cluster', {
143143
autoMinorVersionUpgrade: true,
144144
});
145145
```
146+
147+
## Metrics
148+
149+
Both `DatabaseCluster` and `DatabaseInstance` provide a `metric()` method to help with cluster-level and instance-level monitoring.
150+
151+
```ts
152+
declare const cluster: neptune.DatabaseCluster;
153+
declare const instance: neptune.DatabaseInstance;
154+
155+
cluster.metric('SparqlRequestsPerSec'); // cluster-level SparqlErrors metric
156+
instance.metric('SparqlRequestsPerSec') // instance-level SparqlErrors metric
157+
```
158+
159+
For more details on the available metrics, refer to https://docs.aws.amazon.com/neptune/latest/userguide/cw-metrics.html

packages/@aws-cdk/aws-neptune/lib/cluster.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
12
import * as ec2 from '@aws-cdk/aws-ec2';
23
import * as iam from '@aws-cdk/aws-iam';
34
import * as kms from '@aws-cdk/aws-kms';
@@ -284,6 +285,14 @@ export interface IDatabaseCluster extends IResource, ec2.IConnectable {
284285
* Grant the given identity connection access to the database.
285286
*/
286287
grantConnect(grantee: iam.IGrantable): iam.Grant;
288+
289+
/**
290+
* Return the given named metric associated with this DatabaseCluster instance
291+
*
292+
* @see https://docs.aws.amazon.com/neptune/latest/userguide/cw-metrics.html
293+
* @see https://docs.aws.amazon.com/neptune/latest/userguide/cw-dimensions.html
294+
*/
295+
metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
287296
}
288297

289298
/**
@@ -398,6 +407,17 @@ export abstract class DatabaseClusterBase extends Resource implements IDatabaseC
398407
public grantConnect(grantee: iam.IGrantable): iam.Grant {
399408
return this.grant(grantee, 'neptune-db:*');
400409
}
410+
411+
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
412+
return new cloudwatch.Metric({
413+
namespace: 'AWS/Neptune',
414+
dimensionsMap: {
415+
DBClusterIdentifier: this.clusterIdentifier,
416+
},
417+
metricName,
418+
...props,
419+
});
420+
}
401421
}
402422

403423
/**

packages/@aws-cdk/aws-neptune/lib/instance.ts

+53-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
12
import * as ec2 from '@aws-cdk/aws-ec2';
23
import * as cdk from '@aws-cdk/core';
34
import { Construct } from 'constructs';
@@ -165,6 +166,14 @@ export interface IDatabaseInstance extends cdk.IResource {
165166
* @attribute Port
166167
*/
167168
readonly dbInstanceEndpointPort: string;
169+
170+
/**
171+
* Return the given named metric associated with this database instance
172+
*
173+
* @see https://docs.aws.amazon.com/neptune/latest/userguide/cw-metrics.html
174+
* @see https://docs.aws.amazon.com/neptune/latest/userguide/cw-dimensions.html
175+
*/
176+
metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
168177
}
169178

170179
/**
@@ -233,27 +242,64 @@ export interface DatabaseInstanceProps {
233242
}
234243

235244
/**
236-
* A database instance
237-
*
238-
* @resource AWS::Neptune::DBInstance
245+
* A new or imported database instance.
239246
*/
240-
export class DatabaseInstance extends cdk.Resource implements IDatabaseInstance {
241-
247+
export abstract class DatabaseInstanceBase extends cdk.Resource implements IDatabaseInstance {
242248
/**
243249
* Import an existing database instance.
244250
*/
245251
public static fromDatabaseInstanceAttributes(scope: Construct, id: string, attrs: DatabaseInstanceAttributes): IDatabaseInstance {
246-
class Import extends cdk.Resource implements IDatabaseInstance {
252+
class Import extends DatabaseInstanceBase implements IDatabaseInstance {
247253
public readonly defaultPort = ec2.Port.tcp(attrs.port);
248254
public readonly instanceIdentifier = attrs.instanceIdentifier;
249255
public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress;
250256
public readonly dbInstanceEndpointPort = attrs.port.toString();
251257
public readonly instanceEndpoint = new Endpoint(attrs.instanceEndpointAddress, attrs.port);
252258
}
253-
254259
return new Import(scope, id);
255260
}
256261

262+
/**
263+
* @inheritdoc
264+
*/
265+
public abstract readonly dbInstanceEndpointAddress: string;
266+
267+
/**
268+
* @inheritdoc
269+
*/
270+
public abstract readonly dbInstanceEndpointPort: string;
271+
272+
/**
273+
* @inheritdoc
274+
*/
275+
public abstract readonly instanceEndpoint: Endpoint;
276+
277+
/**
278+
* @inheritdoc
279+
*/
280+
public abstract readonly instanceIdentifier: string;
281+
282+
/**
283+
* @inheritdoc
284+
*/
285+
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
286+
return new cloudwatch.Metric({
287+
namespace: 'AWS/Neptune',
288+
dimensionsMap: {
289+
DBInstanceIdentifier: this.instanceIdentifier,
290+
},
291+
metricName,
292+
...props,
293+
});
294+
}
295+
}
296+
297+
/**
298+
* A database instance
299+
*
300+
* @resource AWS::Neptune::DBInstance
301+
*/
302+
export class DatabaseInstance extends DatabaseInstanceBase implements IDatabaseInstance {
257303

258304
/**
259305
* The instance's database cluster

packages/@aws-cdk/aws-neptune/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@
9090
"@types/jest": "^27.5.2"
9191
},
9292
"dependencies": {
93+
"@aws-cdk/aws-cloudwatch": "0.0.0",
9394
"@aws-cdk/aws-ec2": "0.0.0",
9495
"@aws-cdk/aws-iam": "0.0.0",
9596
"@aws-cdk/aws-kms": "0.0.0",
9697
"@aws-cdk/core": "0.0.0",
9798
"constructs": "^10.0.0"
9899
},
99100
"peerDependencies": {
101+
"@aws-cdk/aws-cloudwatch": "0.0.0",
100102
"@aws-cdk/aws-ec2": "0.0.0",
101103
"@aws-cdk/aws-iam": "0.0.0",
102104
"@aws-cdk/aws-kms": "0.0.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "21.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "ClusterTestDefaultTestDeployAssert6A1BBA9D.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}

packages/@aws-cdk/aws-neptune/test/cluster.integ.snapshot/aws-cdk-neptune-integ.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "21.0.0",
33
"files": {
4-
"315715ffe6004c7bd7c9874629785c10fd8f65a20d6995ea8eb20188dfb82b7d": {
4+
"c3aa283b33e47bc3d0cb943f014017d1742247b6577982270570cb6cbf5a778c": {
55
"source": {
66
"path": "aws-cdk-neptune-integ.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "315715ffe6004c7bd7c9874629785c10fd8f65a20d6995ea8eb20188dfb82b7d.json",
12+
"objectKey": "c3aa283b33e47bc3d0cb943f014017d1742247b6577982270570cb6cbf5a778c.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-neptune/test/cluster.integ.snapshot/aws-cdk-neptune-integ.template.json

+20
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,26 @@
538538
],
539539
"UpdateReplacePolicy": "Delete",
540540
"DeletionPolicy": "Delete"
541+
},
542+
"Alarm7103F465": {
543+
"Type": "AWS::CloudWatch::Alarm",
544+
"Properties": {
545+
"ComparisonOperator": "LessThanThreshold",
546+
"EvaluationPeriods": 1,
547+
"Dimensions": [
548+
{
549+
"Name": "DBClusterIdentifier",
550+
"Value": {
551+
"Ref": "DatabaseB269D8BB"
552+
}
553+
}
554+
],
555+
"MetricName": "SparqlRequestsPerSec",
556+
"Namespace": "AWS/Neptune",
557+
"Period": 300,
558+
"Statistic": "Average",
559+
"Threshold": 1
560+
}
541561
}
542562
},
543563
"Parameters": {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
22
"version": "21.0.0",
33
"testCases": {
4-
"integ.cluster": {
4+
"ClusterTest/DefaultTest": {
55
"stacks": [
66
"aws-cdk-neptune-integ"
77
],
8-
"diffAssets": false,
9-
"stackUpdateWorkflow": true
8+
"assertionStack": "ClusterTest/DefaultTest/DeployAssert",
9+
"assertionStackName": "ClusterTestDefaultTestDeployAssert6A1BBA9D"
1010
}
11-
},
12-
"synthContext": {},
13-
"enableLookups": false
11+
}
1412
}

0 commit comments

Comments
 (0)