Skip to content

Commit

Permalink
docs(logs): make examples compile (aws#17168)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored and TikiTDO committed Feb 21, 2022
1 parent d6c920e commit 2689102
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
54 changes: 29 additions & 25 deletions packages/@aws-cdk/aws-logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Here's a simple example of creating an encrypted Log Group using a KMS CMK.
```ts
import * as kms from '@aws-cdk/aws-kms';

new LogGroup(this, 'LogGroup', {
new logs.LogGroup(this, 'LogGroup', {
encryptionKey: new kms.Key(this, 'Key'),
});
```
Expand All @@ -83,13 +83,14 @@ Create a `SubscriptionFilter`, initialize it with an appropriate `Pattern` (see
below) and supply the intended destination:

```ts
const fn = new lambda.Function(this, 'Lambda', { ... });
const logGroup = new LogGroup(this, 'LogGroup', { ... });
import * as destinations from '@aws-cdk/aws-logs-destinations';
declare const fn: lambda.Function;
declare const logGroup: logs.LogGroup;

new SubscriptionFilter(this, 'Subscription', {
logGroup,
destination: new LogsDestinations.LambdaDestination(fn),
filterPattern: FilterPattern.allTerms("ERROR", "MainThread")
new logs.SubscriptionFilter(this, 'Subscription', {
logGroup,
destination: new destinations.LambdaDestination(fn),
filterPattern: logs.FilterPattern.allTerms("ERROR", "MainThread"),
});
```

Expand All @@ -114,6 +115,7 @@ A very simple MetricFilter can be created by using the `logGroup.extractMetric()
helper function:

```ts
declare const logGroup: logs.LogGroup;
logGroup.extractMetric('$.jsonField', 'Namespace', 'MetricName');
```

Expand All @@ -127,19 +129,20 @@ You can expose a metric on a metric filter by calling the `MetricFilter.metric()
This has a default of `statistic = 'avg'` if the statistic is not set in the `props`.

```ts
const mf = new MetricFilter(this, 'MetricFilter', {
declare const logGroup: logs.LogGroup;
const mf = new logs.MetricFilter(this, 'MetricFilter', {
logGroup,
metricNamespace: 'MyApp',
metricName: 'Latency',
filterPattern: FilterPattern.exists('$.latency'),
filterPattern: logs.FilterPattern.exists('$.latency'),
metricValue: '$.latency',
});

//expose a metric from the metric filter
const metric = mf.metric();

//you can use the metric to create a new alarm
new Alarm(this, 'alarm from metric filter', {
new cloudwatch.Alarm(this, 'alarm from metric filter', {
metric,
threshold: 100,
evaluationPeriods: 2,
Expand Down Expand Up @@ -175,7 +178,7 @@ line.
(substrings) appear in the log event.
* `FilterPattern.anyTerm(term, term, ...)`: matches if all of the given terms
(substrings) appear in the log event.
* `FilterPattern.anyGroup([term, term, ...], [term, term, ...], ...)`: matches if
* `FilterPattern.anyTermGroup([term, term, ...], [term, term, ...], ...)`: matches if
all of the terms in any of the groups (specified as arrays) matches. This is
an OR match.

Expand All @@ -184,14 +187,14 @@ Examples:

```ts
// Search for lines that contain both "ERROR" and "MainThread"
const pattern1 = FilterPattern.allTerms('ERROR', 'MainThread');
const pattern1 = logs.FilterPattern.allTerms('ERROR', 'MainThread');

// Search for lines that either contain both "ERROR" and "MainThread", or
// both "WARN" and "Deadlock".
const pattern2 = FilterPattern.anyGroup(
['ERROR', 'MainThread'],
['WARN', 'Deadlock'],
);
const pattern2 = logs.FilterPattern.anyTermGroup(
['ERROR', 'MainThread'],
['WARN', 'Deadlock'],
);
```

## JSON Patterns
Expand Down Expand Up @@ -235,12 +238,13 @@ Example:
// Search for all events where the component field is equal to
// "HttpServer" and either error is true or the latency is higher
// than 1000.
const pattern = FilterPattern.all(
FilterPattern.stringValue('$.component', '=', 'HttpServer'),
FilterPattern.any(
FilterPattern.booleanValue('$.error', true),
FilterPattern.numberValue('$.latency', '>', 1000)
));
const pattern = logs.FilterPattern.all(
logs.FilterPattern.stringValue('$.component', '=', 'HttpServer'),
logs.FilterPattern.any(
logs.FilterPattern.booleanValue('$.error', true),
logs.FilterPattern.numberValue('$.latency', '>', 1000),
),
);
```

## Space-delimited table patterns
Expand Down Expand Up @@ -274,9 +278,9 @@ Example:
```ts
// Search for all events where the component is "HttpServer" and the
// result code is not equal to 200.
const pattern = FilterPattern.spaceDelimited('time', 'component', '...', 'result_code', 'latency')
.whereString('component', '=', 'HttpServer')
.whereNumber('result_code', '!=', 200);
const pattern = logs.FilterPattern.spaceDelimited('time', 'component', '...', 'result_code', 'latency')
.whereString('component', '=', 'HttpServer')
.whereNumber('result_code', '!=', 200);
```

## Notes
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-logs/rosetta/default.ts-fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Fixture with packages imported, but nothing else
import { Construct } from 'constructs';
import { Stack } from '@aws-cdk/core';
import * as logs from '@aws-cdk/aws-logs';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as lambda from '@aws-cdk/aws-lambda';

class Fixture extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

/// here
}
}

0 comments on commit 2689102

Please sign in to comment.