Skip to content

Commit 06221ac

Browse files
author
Elad Ben-Israel
authored
refactor(logs): API cleanups (#2909)
* CrossAccountDestination now extends `Resource` BREAKING CHANGE: `newStream` renamed to `addStream` and doesn't need a scope * **logs:** `newSubscriptionFilter` renamed to `addSubscriptionFilter` and doesn't need a scope * **logs:** `newMetricFilter` renamed to `addMetricFilter` and doesn't need a scope * **logs:** `NewSubscriptionFilterProps` renamed to `SubscriptionProps` * **logs:** `NewLogStreamProps` renamed to `LogStreamOptions` * **logs:** `NewMetricFilterProps` renamed to `MetricFilterOptions` * **logs:** `JSONPattern` renamed to `JsonPattern`
1 parent 53e1191 commit 06221ac

File tree

6 files changed

+37
-93
lines changed

6 files changed

+37
-93
lines changed

packages/@aws-cdk/aws-logs/lib/cross-account-destination.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ export interface CrossAccountDestinationProps {
3939
* subscribe a Kinesis stream using the integration class in the
4040
* `@aws-cdk/aws-logs-destinations` package; if necessary, a
4141
* `CrossAccountDestination` will be created automatically.
42+
*
43+
* @resource AWS::Logs::Destination
4244
*/
43-
export class CrossAccountDestination extends cdk.Construct implements ILogSubscriptionDestination {
45+
export class CrossAccountDestination extends cdk.Resource implements ILogSubscriptionDestination {
4446
/**
4547
* Policy object of this CrossAccountDestination object
4648
*/
4749
public readonly policyDocument: iam.PolicyDocument = new iam.PolicyDocument();
4850

4951
/**
5052
* The name of this CrossAccountDestination object
53+
* @attribute
5154
*/
5255
public readonly destinationName: string;
5356

5457
/**
5558
* The ARN of this CrossAccountDestination object
59+
* @attribute
5660
*/
5761
public readonly destinationArn: string;
5862

packages/@aws-cdk/aws-logs/lib/log-group.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,26 @@ export interface ILogGroup extends IResource {
2323
/**
2424
* Create a new Log Stream for this Log Group
2525
*
26-
* @param scope Parent construct
2726
* @param id Unique identifier for the construct in its parent
2827
* @param props Properties for creating the LogStream
2928
*/
30-
newStream(scope: Construct, id: string, props?: NewLogStreamProps): LogStream;
29+
addStream(id: string, props?: StreamOptions): LogStream;
3130

3231
/**
3332
* Create a new Subscription Filter on this Log Group
3433
*
35-
* @param scope Parent construct
3634
* @param id Unique identifier for the construct in its parent
3735
* @param props Properties for creating the SubscriptionFilter
3836
*/
39-
newSubscriptionFilter(scope: Construct, id: string, props: NewSubscriptionFilterProps): SubscriptionFilter;
37+
addSubscriptionFilter(id: string, props: SubscriptionFilterOptions): SubscriptionFilter;
4038

4139
/**
4240
* Create a new Metric Filter on this Log Group
4341
*
44-
* @param scope Parent construct
4542
* @param id Unique identifier for the construct in its parent
4643
* @param props Properties for creating the MetricFilter
4744
*/
48-
newMetricFilter(scope: Construct, id: string, props: NewMetricFilterProps): MetricFilter;
45+
addMetricFilter(id: string, props: MetricFilterOptions): MetricFilter;
4946

5047
/**
5148
* Extract a metric from structured log events in the LogGroup
@@ -91,12 +88,11 @@ abstract class LogGroupBase extends Resource implements ILogGroup {
9188
/**
9289
* Create a new Log Stream for this Log Group
9390
*
94-
* @param scope Parent construct
9591
* @param id Unique identifier for the construct in its parent
9692
* @param props Properties for creating the LogStream
9793
*/
98-
public newStream(scope: Construct, id: string, props: NewLogStreamProps = {}): LogStream {
99-
return new LogStream(scope, id, {
94+
public addStream(id: string, props: StreamOptions = {}): LogStream {
95+
return new LogStream(this, id, {
10096
logGroup: this,
10197
...props
10298
});
@@ -105,12 +101,11 @@ abstract class LogGroupBase extends Resource implements ILogGroup {
105101
/**
106102
* Create a new Subscription Filter on this Log Group
107103
*
108-
* @param scope Parent construct
109104
* @param id Unique identifier for the construct in its parent
110105
* @param props Properties for creating the SubscriptionFilter
111106
*/
112-
public newSubscriptionFilter(scope: Construct, id: string, props: NewSubscriptionFilterProps): SubscriptionFilter {
113-
return new SubscriptionFilter(scope, id, {
107+
public addSubscriptionFilter(id: string, props: SubscriptionFilterOptions): SubscriptionFilter {
108+
return new SubscriptionFilter(this, id, {
114109
logGroup: this,
115110
...props
116111
});
@@ -119,12 +114,11 @@ abstract class LogGroupBase extends Resource implements ILogGroup {
119114
/**
120115
* Create a new Metric Filter on this Log Group
121116
*
122-
* @param scope Parent construct
123117
* @param id Unique identifier for the construct in its parent
124118
* @param props Properties for creating the MetricFilter
125119
*/
126-
public newMetricFilter(scope: Construct, id: string, props: NewMetricFilterProps): MetricFilter {
127-
return new MetricFilter(scope, id, {
120+
public addMetricFilter(id: string, props: MetricFilterOptions): MetricFilter {
121+
return new MetricFilter(this, id, {
128122
logGroup: this,
129123
...props
130124
});
@@ -353,7 +347,7 @@ export class LogGroup extends LogGroupBase {
353347
/**
354348
* Properties for a new LogStream created from a LogGroup
355349
*/
356-
export interface NewLogStreamProps {
350+
export interface StreamOptions {
357351
/**
358352
* The name of the log stream to create.
359353
*
@@ -367,7 +361,7 @@ export interface NewLogStreamProps {
367361
/**
368362
* Properties for a new SubscriptionFilter created from a LogGroup
369363
*/
370-
export interface NewSubscriptionFilterProps {
364+
export interface SubscriptionFilterOptions {
371365
/**
372366
* The destination to send the filtered events to.
373367
*
@@ -384,7 +378,7 @@ export interface NewSubscriptionFilterProps {
384378
/**
385379
* Properties for a MetricFilter created from a LogGroup
386380
*/
387-
export interface NewMetricFilterProps {
381+
export interface MetricFilterOptions {
388382
/**
389383
* Pattern to search for log events.
390384
*/

packages/@aws-cdk/aws-logs/lib/metric-filter.ts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
11
import { Construct, Resource } from '@aws-cdk/cdk';
2-
import { ILogGroup } from './log-group';
2+
import { ILogGroup, MetricFilterOptions } from './log-group';
33
import { CfnMetricFilter } from './logs.generated';
4-
import { IFilterPattern } from './pattern';
54

65
/**
76
* Properties for a MetricFilter
87
*/
9-
export interface MetricFilterProps {
8+
export interface MetricFilterProps extends MetricFilterOptions {
109
/**
1110
* The log group to create the filter on.
1211
*/
1312
readonly logGroup: ILogGroup;
14-
15-
/**
16-
* Pattern to search for log events.
17-
*/
18-
readonly filterPattern: IFilterPattern;
19-
20-
/**
21-
* The namespace of the metric to emit.
22-
*/
23-
readonly metricNamespace: string;
24-
25-
/**
26-
* The name of the metric to emit.
27-
*/
28-
readonly metricName: string;
29-
30-
/**
31-
* The value to emit for the metric.
32-
*
33-
* Can either be a literal number (typically "1"), or the name of a field in the structure
34-
* to take the value from the matched event. If you are using a field value, the field
35-
* value must have been matched using the pattern.
36-
*
37-
* If you want to specify a field from a matched JSON structure, use '$.fieldName',
38-
* and make sure the field is in the pattern (if only as '$.fieldName = *').
39-
*
40-
* If you want to specify a field from a matched space-delimited structure,
41-
* use '$fieldName'.
42-
*
43-
* @default "1"
44-
*/
45-
readonly metricValue?: string;
46-
47-
/**
48-
* The value to emit if the pattern does not match a particular event.
49-
*
50-
* @default No metric emitted.
51-
*/
52-
readonly defaultValue?: number;
5313
}
5414

5515
/**

packages/@aws-cdk/aws-logs/lib/pattern.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ export interface IFilterPattern {
1010
/**
1111
* Base class for patterns that only match JSON log events.
1212
*/
13-
export abstract class JSONPattern implements IFilterPattern {
13+
export abstract class JsonPattern implements IFilterPattern {
1414
// This is a separate class so we have some type safety where users can't
1515
// combine text patterns and JSON patterns with an 'and' operation.
16-
constructor(public readonly jsonPatternString: string) {
17-
}
16+
constructor(public readonly jsonPatternString: string) { }
1817

1918
public get logPatternString(): string {
2019
return '{ ' + this.jsonPatternString + ' }';
@@ -92,7 +91,7 @@ export class FilterPattern {
9291
* @param comparison Comparison to carry out. Either = or !=.
9392
* @param value The string value to compare to. May use '*' as wildcard at start or end of string.
9493
*/
95-
public static stringValue(jsonField: string, comparison: string, value: string): JSONPattern {
94+
public static stringValue(jsonField: string, comparison: string, value: string): JsonPattern {
9695
return new JSONStringPattern(jsonField, comparison, value);
9796
}
9897

@@ -114,7 +113,7 @@ export class FilterPattern {
114113
* @param comparison Comparison to carry out. One of =, !=, <, <=, >, >=.
115114
* @param value The numerical value to compare to
116115
*/
117-
public static numberValue(jsonField: string, comparison: string, value: number): JSONPattern {
116+
public static numberValue(jsonField: string, comparison: string, value: number): JsonPattern {
118117
return new JSONNumberPattern(jsonField, comparison, value);
119118
}
120119

@@ -123,7 +122,7 @@ export class FilterPattern {
123122
*
124123
* @param jsonField Field inside JSON. Example: "$.myField"
125124
*/
126-
public static isNull(jsonField: string): JSONPattern {
125+
public static isNull(jsonField: string): JsonPattern {
127126
return new JSONPostfixPattern(jsonField, 'IS NULL');
128127
}
129128

@@ -132,7 +131,7 @@ export class FilterPattern {
132131
*
133132
* @param jsonField Field inside JSON. Example: "$.myField"
134133
*/
135-
public static notExists(jsonField: string): JSONPattern {
134+
public static notExists(jsonField: string): JsonPattern {
136135
return new JSONPostfixPattern(jsonField, 'NOT EXISTS');
137136
}
138137

@@ -143,7 +142,7 @@ export class FilterPattern {
143142
*
144143
* @param jsonField Field inside JSON. Example: "$.myField"
145144
*/
146-
public static exists(jsonField: string): JSONPattern {
145+
public static exists(jsonField: string): JsonPattern {
147146
return new JSONStringPattern(jsonField, '=', '*');
148147
}
149148

@@ -153,14 +152,14 @@ export class FilterPattern {
153152
* @param jsonField Field inside JSON. Example: "$.myField"
154153
* @param value The value to match
155154
*/
156-
public static booleanValue(jsonField: string, value: boolean): JSONPattern {
155+
public static booleanValue(jsonField: string, value: boolean): JsonPattern {
157156
return new JSONPostfixPattern(jsonField, value ? 'IS TRUE' : 'IS FALSE');
158157
}
159158

160159
/**
161160
* A JSON log pattern that matches if all given JSON log patterns match
162161
*/
163-
public static all(...patterns: JSONPattern[]): JSONPattern {
162+
public static all(...patterns: JsonPattern[]): JsonPattern {
164163
if (patterns.length === 0) { throw new Error('Must supply at least one pattern, or use allEvents() to match all events.'); }
165164
if (patterns.length === 1) { return patterns[0]; }
166165
return new JSONAggregatePattern('&&', patterns);
@@ -169,7 +168,7 @@ export class FilterPattern {
169168
/**
170169
* A JSON log pattern that matches if any of the given JSON log patterns match
171170
*/
172-
public static any(...patterns: JSONPattern[]): JSONPattern {
171+
public static any(...patterns: JsonPattern[]): JsonPattern {
173172
if (patterns.length === 0) { throw new Error('Must supply at least one pattern'); }
174173
if (patterns.length === 1) { return patterns[0]; }
175174
return new JSONAggregatePattern('||', patterns);
@@ -220,7 +219,7 @@ class TextLogPattern implements IFilterPattern {
220219
/**
221220
* A string comparison for JSON values
222221
*/
223-
class JSONStringPattern extends JSONPattern {
222+
class JSONStringPattern extends JsonPattern {
224223
public constructor(jsonField: string, comparison: string, value: string) {
225224
comparison = validateStringOperator(comparison);
226225
super(`${jsonField} ${comparison} ${quoteTerm(value)}`);
@@ -230,7 +229,7 @@ class JSONStringPattern extends JSONPattern {
230229
/**
231230
* A number comparison for JSON values
232231
*/
233-
class JSONNumberPattern extends JSONPattern {
232+
class JSONNumberPattern extends JsonPattern {
234233
public constructor(jsonField: string, comparison: string, value: number) {
235234
comparison = validateNumericalOperator(comparison);
236235
super(`${jsonField} ${comparison} ${value}`);
@@ -240,7 +239,7 @@ class JSONNumberPattern extends JSONPattern {
240239
/**
241240
* A postfix operator for JSON patterns
242241
*/
243-
class JSONPostfixPattern extends JSONPattern {
242+
class JSONPostfixPattern extends JsonPattern {
244243
public constructor(jsonField: string, postfix: string) {
245244
// No validation, we assume these are generated by trusted factory functions
246245
super(`${jsonField} ${postfix}`);
@@ -250,8 +249,8 @@ class JSONPostfixPattern extends JSONPattern {
250249
/**
251250
* Combines multiple other JSON patterns with an operator
252251
*/
253-
class JSONAggregatePattern extends JSONPattern {
254-
public constructor(operator: string, patterns: JSONPattern[]) {
252+
class JSONAggregatePattern extends JsonPattern {
253+
public constructor(operator: string, patterns: JsonPattern[]) {
255254
if (operator !== '&&' && operator !== '||') {
256255
throw new Error('Operator must be one of && or ||');
257256
}

packages/@aws-cdk/aws-logs/lib/subscription-filter.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import iam = require('@aws-cdk/aws-iam');
22
import { Construct, Resource } from '@aws-cdk/cdk';
3-
import { ILogGroup } from './log-group';
3+
import { ILogGroup, SubscriptionFilterOptions } from './log-group';
44
import { CfnSubscriptionFilter } from './logs.generated';
5-
import { IFilterPattern } from './pattern';
65

76
/**
87
* Interface for classes that can be the destination of a log Subscription
@@ -41,23 +40,11 @@ export interface LogSubscriptionDestinationConfig {
4140
/**
4241
* Properties for a SubscriptionFilter
4342
*/
44-
export interface SubscriptionFilterProps {
43+
export interface SubscriptionFilterProps extends SubscriptionFilterOptions {
4544
/**
4645
* The log group to create the subscription on.
4746
*/
4847
readonly logGroup: ILogGroup;
49-
50-
/**
51-
* The destination to send the filtered events to.
52-
*
53-
* For example, a Kinesis stream or a Lambda function.
54-
*/
55-
readonly destination: ILogSubscriptionDestination;
56-
57-
/**
58-
* Log events matching this pattern will be sent to the destination.
59-
*/
60-
readonly filterPattern: IFilterPattern;
6148
}
6249

6350
/**

packages/@aws-cdk/aws-logs/test/test.loggroup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export = {
8888

8989
// WHEN
9090
const imported = LogGroup.fromLogGroupArn(stack2, 'lg', 'arn:aws:logs:us-east-1:123456789012:log-group:my-log-group');
91-
imported.newStream(stack2, 'MakeMeAStream');
91+
imported.addStream('MakeMeAStream');
9292

9393
// THEN
9494
expect(stack2).to(haveResource('AWS::Logs::LogStream', {

0 commit comments

Comments
 (0)