class MetricFilter (construct)
| Language | Type name |
|---|---|
.NET | Amazon.CDK.AWS.Logs.MetricFilter |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awslogs#MetricFilter |
Java | software.amazon.awscdk.services.logs.MetricFilter |
Python | aws_cdk.aws_logs.MetricFilter |
TypeScript (source) | aws-cdk-lib » aws_logs » MetricFilter |
Implements
IConstruct, IDependable, IResource, IEnvironment
A filter that extracts information from CloudWatch Logs and emits to CloudWatch Metrics.
Example
new MetricFilter(this, 'MetricFilter', {
logGroup,
metricNamespace: 'MyApp',
metricName: 'Latency',
filterPattern: FilterPattern.all(
FilterPattern.exists('$.latency'),
FilterPattern.regexValue('$.message', '=', 'bind: address already in use'),
),
metricValue: '$.latency',
});
Initializer
new MetricFilter(scope: Construct, id: string, props: MetricFilterProps)
Parameters
- scope
Construct - id
string - props
MetricFilter Props
Construct Props
| Name | Type | Description |
|---|---|---|
| filter | IFilter | Pattern to search for log events. |
| log | ILog | The log group to create the filter on. |
| metric | string | The name of the metric to emit. |
| metric | string | The namespace of the metric to emit. |
| apply | boolean | Whether the metric filter is applied on the tranformed logs. |
| default | number | The value to emit if the pattern does not match a particular event. |
| dimensions? | { [string]: string } | The fields to use as dimensions for the metric. |
| filter | string | The name of the metric filter. |
| metric | string | The value to emit for the metric. |
| unit? | Unit | The unit to assign to the metric. |
filterPattern
Type:
IFilter
Pattern to search for log events.
logGroup
Type:
ILog
The log group to create the filter on.
metricName
Type:
string
The name of the metric to emit.
metricNamespace
Type:
string
The namespace of the metric to emit.
applyOnTransformedLogs?
Type:
boolean
(optional, default: false)
Whether the metric filter is applied on the tranformed logs.
This parameter is valid only for log groups that have an active log transformer. If this value is true, the metric filter is applied on the transformed version of the log events instead of the original ingested log events.
defaultValue?
Type:
number
(optional, default: No metric emitted.)
The value to emit if the pattern does not match a particular event.
dimensions?
Type:
{ [string]: string }
(optional, default: No dimensions attached to metrics.)
The fields to use as dimensions for the metric.
One metric filter can include as many as three dimensions.
filterName?
Type:
string
(optional, default: Cloudformation generated name.)
The name of the metric filter.
metricValue?
Type:
string
(optional, default: "1")
The value to emit for the metric.
Can either be a literal number (typically "1"), or the name of a field in the structure to take the value from the matched event. If you are using a field value, the field value must have been matched using the pattern.
If you want to specify a field from a matched JSON structure, use '$.fieldName', and make sure the field is in the pattern (if only as '$.fieldName = *').
If you want to specify a field from a matched space-delimited structure, use '$fieldName'.
unit?
Type:
Unit
(optional, default: No unit attached to metrics.)
The unit to assign to the metric.
Properties
| Name | Type | Description |
|---|---|---|
| env | Resource | The environment this resource belongs to. |
| node | Node | The tree node. |
| stack | Stack | The stack in which this resource is defined. |
| static PROPERTY_INJECTION_ID | string | Uniquely identifies this class. |
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed in a Stack (those created by
creating new class instances like new Role(), new Bucket(), etc.), this
is always the same as the environment of the stack they belong to.
For referenced resources (those obtained from referencing methods like
Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be
different than the stack they were imported into.
node
Type:
Node
The tree node.
stack
Type:
Stack
The stack in which this resource is defined.
static PROPERTY_INJECTION_ID
Type:
string
Uniquely identifies this class.
Methods
| Name | Description |
|---|---|
| apply | Apply the given removal policy to this resource. |
| metric(props?) | Return the given named metric for this Metric Filter. |
| to | Returns a string representation of this construct. |
applyRemovalPolicy(policy)
public applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
RemovalPolicy
Apply the given removal policy to this resource.
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS
account for data recovery and cleanup later (RemovalPolicy.RETAIN).
metric(props?)
public metric(props?: MetricOptions): Metric
Parameters
- props
MetricOptions
Returns
Return the given named metric for this Metric Filter.
toString()
public toString(): string
Returns
string
Returns a string representation of this construct.

.NET
Go
Java
Python
TypeScript (