# MetricFilterProps

Struct · Stable

Properties for a MetricFilter.

Extends: [MetricFilterOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.MetricFilterOptions.html)
Used as props by: [MetricFilter](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.MetricFilter.html)

- Source: [`aws-logs/lib/metric-filter.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-logs/lib/metric-filter.ts#L15)
- Type last changed: v2.235.0 · 15 Jan 2026

## Example

TypeScript:

```ts
const repository = new ecr.Repository(this, 'TestRepository');

const runtime = new agentcore.Runtime(this, 'Runtime', {
  agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, 'v1.0.0'),
});

new logs.MetricFilter(this, 'ToolErrors', {
  logGroup: runtime.applicationLogGroup,
  filterPattern: logs.FilterPattern.stringValue('$.tool_status', '=', 'error'),
  metricNamespace: 'MyApp',
  metricName: 'ToolExecutionErrors',
});
```

Python:

```python
repository = ecr.Repository(self, "TestRepository")

runtime = agentcore.Runtime(self, "Runtime",
    agent_runtime_artifact=agentcore.AgentRuntimeArtifact.from_ecr_repository(repository, "v1.0.0")
)

logs.MetricFilter(self, "ToolErrors",
    log_group=runtime.application_log_group,
    filter_pattern=logs.FilterPattern.string_value("$.tool_status", "=", "error"),
    metric_namespace="MyApp",
    metric_name="ToolExecutionErrors"
)
```

Java:

```java
Repository repository = new Repository(this, "TestRepository");

Runtime runtime = Runtime.Builder.create(this, "Runtime")
        .agentRuntimeArtifact(AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"))
        .build();

MetricFilter.Builder.create(this, "ToolErrors")
        .logGroup(runtime.getApplicationLogGroup())
        .filterPattern(FilterPattern.stringValue("$.tool_status", "=", "error"))
        .metricNamespace("MyApp")
        .metricName("ToolExecutionErrors")
        .build();
```

C#:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var repository = new Repository(this, "TestRepository");

var runtime = new Runtime(this, "Runtime", new RuntimeProps {
    AgentRuntimeArtifact = AgentRuntimeArtifact.FromEcrRepository(repository, "v1.0.0")
});

new MetricFilter(this, "ToolErrors", new MetricFilterProps {
    LogGroup = runtime.ApplicationLogGroup,
    FilterPattern = FilterPattern.StringValue("$.tool_status", "=", "error"),
    MetricNamespace = "MyApp",
    MetricName = "ToolExecutionErrors"
});
```

Go:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
repository := ecr.NewRepository(this, jsii.String("TestRepository"))

runtime := agentcore.NewRuntime(this, jsii.String("Runtime"), &RuntimeProps{
	AgentRuntimeArtifact: agentcore.AgentRuntimeArtifact_FromEcrRepository(repository, jsii.String("v1.0.0")),
})

logs.NewMetricFilter(this, jsii.String("ToolErrors"), &MetricFilterProps{
	LogGroup: runtime.applicationLogGroup,
	FilterPattern: logs.FilterPattern_StringValue(jsii.String("$.tool_status"), jsii.String("="), jsii.String("error")),
	MetricNamespace: jsii.String("MyApp"),
	MetricName: jsii.String("ToolExecutionErrors"),
})
```

## Properties

- `applyOnTransformedLogs`: boolean = 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`: number — The value to emit if the pattern does not match a particular event.

  **Default:** No metric emitted.

- `dimensions`: { [key: string]: string } — The fields to use as dimensions for the metric.

  **Default:** No dimensions attached to metrics.

  One metric filter can include as many as three dimensions.

- `filterName`: string — The name of the metric filter.

  **Default:** Cloudformation generated name.

- `filterPattern`: [IFilterPattern](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_logs.IFilterPattern.html) (required) — Pattern to search for log events.

- `logGroup`: [ILogGroupRef](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_logs.ILogGroupRef.html) (required) — The log group to create the filter on.

- `metricName`: string (required) — The name of the metric to emit.

- `metricNamespace`: string (required) — The namespace of the metric to emit.

- `metricValue`: string = "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`: [Unit](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.Unit.html) — The unit to assign to the metric.

  **Default:** No unit attached to metrics.
