Skip to content

Commit af5940a

Browse files
committed
feat(appsync): implements configuration that utilizes enhanced metrics for data source and resolver
1 parent d62f669 commit af5940a

File tree

18 files changed

+303
-23
lines changed

18 files changed

+303
-23
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-enhanced-metrics.js.snapshot/manifest.json

Lines changed: 30 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-enhanced-metrics.js.snapshot/stack.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-enhanced-metrics.js.snapshot/stack.template.json

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"EnhancedMetricsSchema3325364E"
3939
]
4040
},
41-
"EnhancedMetricsNoneDS6C39D5BE": {
41+
"EnhancedMetricsnoneEB4FD168": {
4242
"Type": "AWS::AppSync::DataSource",
4343
"Properties": {
4444
"ApiId": {
@@ -47,9 +47,30 @@
4747
"ApiId"
4848
]
4949
},
50-
"Name": "NoneDS",
50+
"Name": "None",
5151
"Type": "NONE"
5252
}
53+
},
54+
"EnhancedMetricsQuerygetServiceVersionD7E03714": {
55+
"Type": "AWS::AppSync::Resolver",
56+
"Properties": {
57+
"ApiId": {
58+
"Fn::GetAtt": [
59+
"EnhancedMetricsBBD0CBEA",
60+
"ApiId"
61+
]
62+
},
63+
"DataSourceName": "None",
64+
"FieldName": "getTests",
65+
"Kind": "UNIT",
66+
"RequestMappingTemplate": "{\"version\":\"2017-02-28\"}",
67+
"ResponseMappingTemplate": "{\"version\":\"v1\"}",
68+
"TypeName": "Query"
69+
},
70+
"DependsOn": [
71+
"EnhancedMetricsnoneEB4FD168",
72+
"EnhancedMetricsSchema3325364E"
73+
]
5374
}
5475
},
5576
"Parameters": {

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-enhanced-metrics.js.snapshot/tree.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-enhanced-metrics.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'path';
1+
import { join } from 'path';
22
import * as cdk from 'aws-cdk-lib';
33
import * as appsync from 'aws-cdk-lib/aws-appsync';
44
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
@@ -8,16 +8,30 @@ const stack = new cdk.Stack(app, 'stack');
88

99
const api = new appsync.GraphqlApi(stack, 'EnhancedMetrics', {
1010
name: 'EnhancedMetrics',
11-
definition: appsync.Definition.fromSchema(appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.test.graphql'))),
11+
definition: appsync.Definition.fromFile(join(__dirname, 'appsync.test.graphql')),
1212
enhancedMetricsConfig: {
1313
dataSourceLevelMetricsBehavior: appsync.DataSourceLevelMetricsBehavior.PER_DATA_SOURCE_METRICS,
1414
operationLevelMetricsEnabled: true,
1515
resolverLevelMetricsBehavior: appsync.ResolverLevelMetricsBehavior.PER_RESOLVER_METRICS,
1616
},
1717
});
1818

19-
api.addNoneDataSource('NoneDS', {
20-
name: cdk.Lazy.string({ produce(): string { return 'NoneDS'; } }),
19+
20+
const noneDS = api.addNoneDataSource('none', {
21+
name: 'None',
22+
enhancedMetricsEnabled: true,
23+
});
24+
25+
noneDS.createResolver('QuerygetServiceVersion', {
26+
typeName: 'Query',
27+
fieldName: 'getTests',
28+
requestMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
29+
version: '2017-02-28',
30+
})),
31+
responseMappingTemplate: appsync.MappingTemplate.fromString(JSON.stringify({
32+
version: 'v1',
33+
})),
34+
enhancedMetricsEnabled: true,
2135
});
2236

2337
new IntegTest(app, 'api', {

packages/aws-cdk-lib/aws-appsync/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,22 @@ const api = new appsync.GraphqlApi(this, 'OwnerContact', {
937937

938938
Enables and controls the enhanced metrics feature. Enhanced metrics emit granular data on API usage and performance such as AppSync request and error counts, latency, and cache hits/misses. All enhanced metric data is sent to your CloudWatch account, and you can configure the types of data that will be sent.
939939

940+
```ts
941+
const schema = new appsync.SchemaFile({ filePath: 'mySchemaFile' })
942+
new appsync.GraphqlApi(this, 'api', {
943+
name: 'myApi',
944+
definition: appsync.Definition.fromSchema(schema),
945+
enhancedMetricsConfig: {
946+
dataSourceLevelMetricsBehavior: appsync.DataSourceLevelMetricsBehavior.FULL_REQUEST_DATA_SOURCE_METRICS,
947+
operationLevelMetricsEnabled: true,
948+
resolverLevelMetricsBehavior: appsync.ResolverLevelMetricsBehavior. dataSourceLevelMetricsBehavior: appsync.DataSourceLevelMetricsBehavior.FULL_REQUEST_RESOLVER_METRICS,
949+
,
950+
},
951+
});
952+
```
953+
954+
If you wish to enable enhanced monitoring only for subset of data sources or resolvers you are use following configuration
955+
940956
```ts
941957
const schema = new appsync.SchemaFile({ filePath: 'mySchemaFile' })
942958
new appsync.GraphqlApi(this, 'api', {
@@ -948,6 +964,16 @@ new appsync.GraphqlApi(this, 'api', {
948964
resolverLevelMetricsBehavior: appsync.ResolverLevelMetricsBehavior.PER_RESOLVER_METRICS,
949965
},
950966
});
967+
968+
const noneDS = api.addNoneDataSource('none', {
969+
enhancedMetricsEnabled: true,
970+
});
971+
972+
noneDS.createResolver('noneResolver', {
973+
typeName: 'Mutation',
974+
fieldName: 'addDemoMetricsConfig',
975+
enhancedMetricsEnabled: true,
976+
});
951977
```
952978

953979
## Events

packages/aws-cdk-lib/aws-appsync/lib/data-source.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export interface BaseDataSourceProps {
3434
* @default - None
3535
*/
3636
readonly description?: string;
37+
38+
/**
39+
* the whether to enable enhanced metrics of the data source
40+
* Value will be ignored, if `enhancedMetricsConfig.dataSourceLevelMetricsBehavior` on AppSync GraphqlApi construct is set to `FULL_REQUEST_DATA_SOURCE_METRICS`
41+
*
42+
* @default - Enhance metrics are disabled
43+
*/
44+
readonly enhancedMetricsEnabled?: boolean;
3745
}
3846

3947
/**
@@ -128,11 +136,14 @@ export abstract class BaseDataSource extends Construct {
128136
// Replace unsupported characters from DataSource name. The only allowed pattern is: {[_A-Za-z][_0-9A-Za-z]*}
129137
const name = (props.name ?? id);
130138
const supportedName = Token.isUnresolved(name) ? name : name.replace(/[\W]+/g, '');
139+
const metricsConfig = props.enhancedMetricsEnabled === undefined ? undefined
140+
: (props.enhancedMetricsEnabled ? 'ENABLED': 'DISABLED');
131141
this.ds = new CfnDataSource(this, 'Resource', {
132142
apiId: props.api.apiId,
133143
name: supportedName,
134144
description: props.description,
135145
serviceRoleArn: this.serviceRole?.roleArn,
146+
metricsConfig: metricsConfig,
136147
...extended,
137148
});
138149
this.name = supportedName;

packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export interface DataSourceOptions {
3737
* @default - No description
3838
*/
3939
readonly description?: string;
40+
41+
/**
42+
* The whether to enable enhanced metrics of the data source
43+
* Value will be ignored, if `enhancedMetricsConfig.dataSourceLevelMetricsBehavior` on AppSync GraphqlApi construct is set to `FULL_REQUEST_DATA_SOURCE_METRICS`
44+
*
45+
* @default - Enhance metrics are disabled
46+
*/
47+
readonly enhancedMetricsEnabled?: boolean;
4048
}
4149

4250
/**
@@ -377,6 +385,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
377385
api: this,
378386
name: options?.name,
379387
description: options?.description,
388+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
380389
});
381390
}
382391

@@ -393,6 +402,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
393402
table,
394403
name: options?.name,
395404
description: options?.description,
405+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
396406
});
397407
}
398408

@@ -409,6 +419,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
409419
endpoint,
410420
name: options?.name,
411421
description: options?.description,
422+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
412423
authorizationConfig: options?.authorizationConfig,
413424
});
414425
}
@@ -426,6 +437,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
426437
lambdaFunction,
427438
name: options?.name,
428439
description: options?.description,
440+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
429441
});
430442
}
431443

@@ -448,6 +460,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
448460
api: this,
449461
name: options?.name,
450462
description: options?.description,
463+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
451464
serverlessCluster,
452465
secretStore,
453466
databaseName,
@@ -473,6 +486,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
473486
api: this,
474487
name: options?.name,
475488
description: options?.description,
489+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
476490
serverlessCluster,
477491
secretStore,
478492
databaseName,
@@ -492,6 +506,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
492506
api: this,
493507
name: options?.name,
494508
description: options?.description,
509+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
495510
domain,
496511
});
497512
}
@@ -508,6 +523,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
508523
eventBus,
509524
name: options?.name,
510525
description: options?.description,
526+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
511527
});
512528
}
513529

@@ -523,6 +539,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
523539
api: this,
524540
name: options?.name,
525541
description: options?.description,
542+
enhancedMetricsEnabled: options?.enhancedMetricsEnabled,
526543
domain,
527544
});
528545
}

0 commit comments

Comments
 (0)