Skip to content
v2.275.0

MathExpression Class ​

A math expression built with metric(s) emitted by a service.

The math expression is a combination of an expression (x+y) and metrics to apply expression on. It also contains metadata which is used only in graphs, such as color and label. It makes sense to embed this in here, so that compound constructs can attach that metadata to metrics they expose.

MathExpression can also be used for search expressions. In this case, it also optionally accepts a searchRegion and searchAccount property for cross-environment search expressions.

This class does not represent a resource, so hence is not a construct. Instead, MathExpression is an abstraction that makes it easy to specify metrics for use in both alarms and graphs.

Example ​

ts
declare const matchmakingRuleSet: gamelift.MatchmakingRuleSet;
// Alarm that triggers when the per-second average of not placed matches exceed 10%
const ruleEvaluationRatio = new cloudwatch.MathExpression({
  expression: '1 - (ruleEvaluationsPassed / ruleEvaluationsFailed)',
  usingMetrics: {
    ruleEvaluationsPassed: matchmakingRuleSet.metricRuleEvaluationsPassed({ statistic: cloudwatch.Statistic.SUM }),
    ruleEvaluationsFailed: matchmakingRuleSet.metric('ruleEvaluationsFailed'),
  },
});
new cloudwatch.Alarm(this, 'Alarm', {
  metric: ruleEvaluationRatio,
  threshold: 0.1,
  evaluationPeriods: 3,
});
python
# matchmaking_rule_set: gamelift.MatchmakingRuleSet

# Alarm that triggers when the per-second average of not placed matches exceed 10%
rule_evaluation_ratio = cloudwatch.MathExpression(
    expression="1 - (ruleEvaluationsPassed / ruleEvaluationsFailed)",
    using_metrics={
        "rule_evaluations_passed": matchmaking_rule_set.metric_rule_evaluations_passed(statistic=cloudwatch.Statistic.SUM),
        "rule_evaluations_failed": matchmaking_rule_set.metric("ruleEvaluationsFailed")
    }
)
cloudwatch.Alarm(self, "Alarm",
    metric=rule_evaluation_ratio,
    threshold=0.1,
    evaluation_periods=3
)
java
MatchmakingRuleSet matchmakingRuleSet;

// Alarm that triggers when the per-second average of not placed matches exceed 10%
MathExpression ruleEvaluationRatio = MathExpression.Builder.create()
        .expression("1 - (ruleEvaluationsPassed / ruleEvaluationsFailed)")
        .usingMetrics(Map.of(
                "ruleEvaluationsPassed", matchmakingRuleSet.metricRuleEvaluationsPassed(MetricOptions.builder().statistic(Statistic.SUM).build()),
                "ruleEvaluationsFailed", matchmakingRuleSet.metric("ruleEvaluationsFailed")))
        .build();
Alarm.Builder.create(this, "Alarm")
        .metric(ruleEvaluationRatio)
        .threshold(0.1)
        .evaluationPeriods(3)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
MatchmakingRuleSet matchmakingRuleSet;

// Alarm that triggers when the per-second average of not placed matches exceed 10%
var ruleEvaluationRatio = new MathExpression(new MathExpressionProps {
    Expression = "1 - (ruleEvaluationsPassed / ruleEvaluationsFailed)",
    UsingMetrics = new Dictionary<string, IMetric> {
        { "ruleEvaluationsPassed", matchmakingRuleSet.MetricRuleEvaluationsPassed(new MetricOptions { Statistic = Statistic.SUM }) },
        { "ruleEvaluationsFailed", matchmakingRuleSet.Metric("ruleEvaluationsFailed") }
    }
});
new Alarm(this, "Alarm", new AlarmProps {
    Metric = ruleEvaluationRatio,
    Threshold = 0.1,
    EvaluationPeriods = 3
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var matchmakingRuleSet MatchmakingRuleSet

// Alarm that triggers when the per-second average of not placed matches exceed 10%
ruleEvaluationRatio := cloudwatch.NewMathExpression(&MathExpressionProps{
	Expression: jsii.String("1 - (ruleEvaluationsPassed / ruleEvaluationsFailed)"),
	UsingMetrics: map[string]IMetric{
		"ruleEvaluationsPassed": matchmakingRuleSet.metricRuleEvaluationsPassed(&MetricOptions{
			"statistic": cloudwatch.Statistic_SUM,
		}),
		"ruleEvaluationsFailed": matchmakingRuleSet.metric(jsii.String("ruleEvaluationsFailed")),
	},
})
cloudwatch.NewAlarm(this, jsii.String("Alarm"), &AlarmProps{
	Metric: ruleEvaluationRatio,
	Threshold: jsii.Number(0.1),
	EvaluationPeriods: jsii.Number(3),
})

Initializer ​

ts
new MathExpression(props: MathExpressionProps)
python
MathExpression(props: MathExpressionProps)
java
new MathExpression(MathExpressionProps props)
csharp
new MathExpression(MathExpressionProps props)
go
NewMathExpression(Props MathExpressionProps)

Properties ​

colormay be undefinedstringcolormay be undefinedstrcolormay be undefinedStringColormay be undefinedstringColormay be undefined*string#
The hex color code, prefixed with '#' (e.g. '#00ff00'), to use when this metric is rendered on a graph. The `Color` class has a set of standard colors that can be used here.
expressionstringexpressionstrexpressionStringExpressionstringExpression*string#
The expression defining the metric.
labelmay be undefinedstringlabelmay be undefinedstrlabelmay be undefinedStringLabelmay be undefinedstringLabelmay be undefined*string#
Label for this metric when added to a Graph.
periodDurationperiodDurationperiodDurationPeriodDurationPeriodDuration#
Aggregation period of this metric.
searchAccountmay be undefinedstringsearch_accountmay be undefinedstrsearchAccountmay be undefinedStringSearchAccountmay be undefinedstringSearchAccountmay be undefined*string#
Account to evaluate search expressions within.
searchRegionmay be undefinedstringsearch_regionmay be undefinedstrsearchRegionmay be undefinedStringSearchRegionmay be undefinedstringSearchRegionmay be undefined*string#
Region to evaluate search expressions within.
usingMetrics{ [key: string]: IMetric }using_metricstyping.Mapping[str, IMetric]usingMetricsMap<String, IMetric>UsingMetricsIDictionary<string, IMetric>UsingMetrics*map[string]IMetric#
The metrics used in the expression as KeyValuePair <id, metric>.
warningsmay be undefinedstring[]warningsmay be undefinedtyping.List[str]warningsmay be undefinedList<String>Warningsmay be undefinedstring[]Warningsmay be undefined*[]*stringdeprecated#
- use warningsV2
warningsV2may be undefined{ [key: string]: string }warnings_v2may be undefinedtyping.Mapping[str, str]warningsV2may be undefinedMap<String, String>WarningsV2may be undefinedIDictionary<string, string>WarningsV2may be undefined*map[string]*string#
Warnings generated by this math expression.

Methods ​

createAlarm(scope: Construct, id: string, props: CreateAlarmOptions): Alarmdef create_alarm(self, scope: Construct, id: str, props: CreateAlarmOptions) -> Alarmpublic Alarm createAlarm(Construct scope, String id, CreateAlarmOptions props)public Alarm CreateAlarm(Construct scope, string id, CreateAlarmOptions props)func CreateAlarm(Scope Construct, Id *string, Props CreateAlarmOptions) Alarm#
Make a new Alarm for this metric.

Combines both properties that may adjust the metric (aggregation) as well as alarm properties.

toMetricConfig(): MetricConfigdef to_metric_config(self, ) -> MetricConfigpublic MetricConfig toMetricConfig()public MetricConfig ToMetricConfig()func ToMetricConfig() MetricConfig#
Inspect the details of the metric object.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of an object.
with(props: MathExpressionOptions): MathExpression // chainabledef with(self, props: MathExpressionOptions) -> MathExpressionpublic MathExpression with(MathExpressionOptions props)public MathExpression With(MathExpressionOptions props)func With(Props MathExpressionOptions) MathExpression#
Return a copy of Metric with properties changed.

All properties except namespace and metricName can be changed.

1 parameter

Parameters

  • propspropspropspropsProps — The set of properties to change.