EMF
CLASS | DESCRIPTION |
---|---|
AmazonCloudWatchEMFProvider |
AmazonCloudWatchEMFProvider creates metrics asynchronously via CloudWatch Embedded Metric Format (EMF). |
AmazonCloudWatchEMFProvider ¶
AmazonCloudWatchEMFProvider(
metric_set: dict[str, Any] | None = None,
dimension_set: dict | None = None,
namespace: str | None = None,
metadata_set: dict[str, Any] | None = None,
service: str | None = None,
default_dimensions: dict[str, Any] | None = None,
)
Bases: BaseProvider
AmazonCloudWatchEMFProvider creates metrics asynchronously via CloudWatch Embedded Metric Format (EMF).
CloudWatch EMF can create up to 100 metrics per EMF object and metrics, dimensions, and namespace created via AmazonCloudWatchEMFProvider will adhere to the schema, will be serialized and validated against EMF Schema.
Use aws_lambda_powertools.Metrics
or
aws_lambda_powertools.single_metric
to create EMF metrics.
Environment variables
POWERTOOLS_METRICS_NAMESPACE : str metric namespace to be set for all metrics POWERTOOLS_SERVICE_NAME : str service name used for default dimension
RAISES | DESCRIPTION |
---|---|
MetricUnitError
|
When metric unit isn't supported by CloudWatch |
MetricResolutionError
|
When metric resolution isn't supported by CloudWatch |
MetricValueError
|
When metric value isn't a number |
SchemaValidationError
|
When metric object fails EMF schema validation |
METHOD | DESCRIPTION |
---|---|
add_cold_start_metric |
Add cold start metric and function_name dimension |
add_dimension |
Adds given dimension to all metrics |
add_metadata |
Adds high cardinal metadata for metrics object |
add_metric |
Adds given metric |
flush_metrics |
Manually flushes the metrics. This is normally not necessary, |
log_metrics |
Decorator to serialize and publish metrics at the end of a function execution. |
serialize_metric_set |
Serializes metric and dimensions set |
set_default_dimensions |
Persist dimensions across Lambda invocations |
set_timestamp |
Set the timestamp for the metric. |
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
add_cold_start_metric ¶
add_cold_start_metric(context: LambdaContext) -> None
Add cold start metric and function_name dimension
PARAMETER | DESCRIPTION |
---|---|
context
|
Lambda context
TYPE:
|
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
add_dimension ¶
add_dimension(name: str, value: str) -> None
Adds given dimension to all metrics
Example
Add a metric dimensions
1 |
|
PARAMETER | DESCRIPTION |
---|---|
name
|
Dimension name
TYPE:
|
value
|
Dimension value
TYPE:
|
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
add_metadata ¶
add_metadata(key: str, value: Any) -> None
Adds high cardinal metadata for metrics object
This will not be available during metrics visualization. Instead, this will be searchable through logs.
If you're looking to add metadata to filter metrics, then use add_dimension method.
Example
Add metrics metadata
1 |
|
PARAMETER | DESCRIPTION |
---|---|
key
|
Metadata key
TYPE:
|
value
|
Metadata value
TYPE:
|
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
add_metric ¶
add_metric(
name: str,
unit: MetricUnit | str,
value: float,
resolution: MetricResolution | int = 60,
) -> None
Adds given metric
Example
Add given metric using MetricUnit enum
1 |
|
Add given metric using plain string as value unit
1 |
|
Add given metric with MetricResolution non default value
1 |
|
PARAMETER | DESCRIPTION |
---|---|
name
|
Metric name
TYPE:
|
unit
|
TYPE:
|
value
|
Metric value
TYPE:
|
resolution
|
TYPE:
|
RAISES | DESCRIPTION |
---|---|
MetricUnitError
|
When metric unit is not supported by CloudWatch |
MetricResolutionError
|
When metric resolution is not supported by CloudWatch |
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
flush_metrics ¶
flush_metrics(raise_on_empty_metrics: bool = False) -> None
Manually flushes the metrics. This is normally not necessary, unless you're running on other runtimes besides Lambda, where the @log_metrics decorator already handles things for you.
PARAMETER | DESCRIPTION |
---|---|
raise_on_empty_metrics
|
raise exception if no metrics are emitted, by default False
TYPE:
|
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
|
log_metrics ¶
log_metrics(
lambda_handler: AnyCallableT | None = None,
capture_cold_start_metric: bool = False,
raise_on_empty_metrics: bool = False,
**kwargs
)
Decorator to serialize and publish metrics at the end of a function execution.
Be aware that the log_metrics *does call the decorated function (e.g. lambda_handler).
Example
Lambda function using tracer and metrics decorators
1 2 3 4 5 6 7 8 9 |
|
PARAMETER | DESCRIPTION |
---|---|
lambda_handler
|
lambda function handler, by default None |
capture_cold_start_metric
|
captures cold start metric, by default False
TYPE:
|
raise_on_empty_metrics
|
raise exception if no metrics are emitted, by default False
TYPE:
|
**kwargs
|
DEFAULT:
|
RAISES | DESCRIPTION |
---|---|
e
|
Propagate error received |
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
|
serialize_metric_set ¶
serialize_metric_set(
metrics: dict | None = None,
dimensions: dict | None = None,
metadata: dict | None = None,
) -> CloudWatchEMFOutput
Serializes metric and dimensions set
PARAMETER | DESCRIPTION |
---|---|
metrics
|
Dictionary of metrics to serialize, by default None
TYPE:
|
dimensions
|
Dictionary of dimensions to serialize, by default None
TYPE:
|
metadata
|
Dictionary of metadata to serialize, by default None
TYPE:
|
Example
Serialize metrics into EMF format
1 2 3 |
|
RETURNS | DESCRIPTION |
---|---|
CloudWatchEMFOutput
|
Serialized metrics following EMF specification |
RAISES | DESCRIPTION |
---|---|
SchemaValidationError
|
Raised when serialization fail schema validation |
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
set_default_dimensions ¶
set_default_dimensions(**dimensions) -> None
Persist dimensions across Lambda invocations
PARAMETER | DESCRIPTION |
---|---|
dimensions
|
metric dimensions as key=value |
Example
Sets some default dimensions that will always be present across metrics and invocations
1 2 3 4 5 6 7 8 |
|
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
set_timestamp ¶
set_timestamp(timestamp: int | datetime.datetime)
Set the timestamp for the metric.
PARAMETER | DESCRIPTION |
---|---|
timestamp
|
The timestamp to create the metric. If an integer is provided, it is assumed to be the epoch time in milliseconds. If a datetime object is provided, it will be converted to epoch time in milliseconds. |
Source code in aws_lambda_powertools/metrics/provider/cloudwatch_emf/cloudwatch.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|