Package: aws-embedded-metrics
aws-embedded-metricsGenerate CloudWatch Metrics embedded within structured log events. The embedded metrics will be extracted so you can visualize and alarm on them for real-time incident detection. This allows you to monitor aggregated values while preserving the detailed event context that generated them.Use CasesInstallationUsageAPIExamplesDevelopmentUse CasesGenerate custom metrics across compute environmentsEasily generate custom metrics from Lambda functions without requiring custom batching code, making blocking network requests or relying on 3rd party software.Other compute environments (EC2, On-prem, ECS, EKS, and other container environments) are supported by installing theCloudWatch Agent.Examples can be found inexamples/README.mdLinking metrics to high cardinality contextUsing the Embedded Metric Format, you will be able to visualize and alarm on custom metrics, but also retain the original, detailed and high-cardinality context which is queryable usingCloudWatch Logs Insights. For example, the library automatically injects environment metadata such as Lambda Function version, EC2 instance and image ids into the structured log event data.Installationpip3 install aws-embedded-metricsUsageTo get a metric logger, you can decorate your function with ametric_scope:fromaws_embedded_metricsimportmetric_scopefromaws_embedded_metrics.storage_resolutionimportStorageResolution@metric_scopedefmy_handler(metrics):metrics.put_dimensions({"Foo":"Bar"})metrics.put_metric("ProcessingLatency",100,"Milliseconds",StorageResolution.STANDARD)metrics.put_metric("Memory.HeapUsed",1600424.0,"Bytes",StorageResolution.HIGH)metrics.set_property("AccountId","123456789012")metrics.set_property("RequestId","422b1569-16f6-4a03")metrics.set_property("DeviceId","61270781-c6ac-46f1")return{"message":"Hello!"}APIMetricsLoggerTheMetricsLoggeris the interface you will use to publish embedded metrics.put_metric(key: str, value: float, unit: str = "None", storage_resolution: int = 60) -> MetricsLoggerAdds a new metric to the current logger context. Multiple metrics using the same key will be appended to an array of values. Multiple metrics cannot have same key and different storage resolution. The Embedded Metric Format supports a maximum of 100 values per key. If more metric values are added than are supported by the format, the logger will be flushed to allow for new metric values to be captured.Requirements:Name Length 1-255 charactersName must be ASCII characters onlyValues must be in the range of 8.515920e-109 to 1.174271e+108. In addition, special values (for example, NaN, +Infinity, -Infinity) are not supported.Metrics must meet CloudWatch Metrics requirements, otherwise aInvalidMetricErrorwill be thrown. SeeMetricDatumfor valid values.Storage ResolutionAn OPTIONAL value representing the storage resolution for the corresponding metric. Setting this toHighspecifies this metric as a high-resolution metric, so that CloudWatch stores the metric with sub-minute resolution down to one second. Setting this toStandardspecifies this metric as a standard-resolution metric, which CloudWatch stores at 1-minute resolution. If a value is not provided, then a default value ofStandardis assumed. SeeCloud Watch High-Resolution metricsExamples:# Standard Resolution exampleput_metric("Latency",200,"Milliseconds")put_metric("Latency",201,"Milliseconds",StorageResolution.STANDARD)# High Resolution exampleput_metric("Memory.HeapUsed",1600424.0,"Bytes",StorageResolution.HIGH)set_property(key: str, value: Any) -> MetricsLoggerAdds or updates the value for a given property on this context. This value is not submitted to CloudWatch Metrics but is searchable by CloudWatch Logs Insights. This is useful for contextual and potentially high-cardinality data that is not appropriate for CloudWatch Metrics dimensions.Requirements:Length 1-255 charactersExamples:set_property("RequestId","422b1569-16f6-4a03-b8f0-fe3fd9b100f8")set_property("InstanceId","i-1234567890")set_property("Device",{"Id":"61270781-c6ac-46f1-baf7-22c808af8162","Name":"Transducer","Model":"PT-1234"})put_dimensions(dimensions: Dict[str, str]) -> MetricsLoggerAdds a new set of dimensions that will be associated to all metric values.WARNING: Every distinct value will result in a new CloudWatch Metric.
If the cardinality of a particular value is expected to be high, you should consider
usingsetPropertyinstead.Requirements:Length 1-255 charactersASCII characters onlyDimensions must meet CloudWatch Dimensions requirements, otherwise aInvalidDimensionErrororDimensionSetExceededErrorwill be thrown. SeeDimensionsfor valid values.Examples:put_dimensions({"Operation":"Aggregator"})put_dimensions({"Operation":"Aggregator","DeviceType":"Actuator"})set_dimensions(*dimensions: Dict[str, str], use_default: bool = False) -> MetricsLoggerExplicitly override all dimensions. By default, this will disable the default dimensions, but can be configured using thekeyword-onlyparameteruse_default.WARNING: Every distinct value will result in a new CloudWatch Metric.
If the cardinality of a particular value is expected to be high, you should consider
usingsetPropertyinstead.Requirements:Length 1-255 charactersASCII characters onlyDimensions must meet CloudWatch Dimensions requirements, otherwise aInvalidDimensionErrororDimensionSetExceededErrorwill be thrown. SeeDimensionsfor valid values.Examples:set_dimensions({"Operation":"Aggregator"},{"Operation":"Aggregator","DeviceType":"Actuator"})set_dimensions({"Operation":"Aggregator"},use_default=True# default dimensions would be enabled)reset_dimensions(use_default: bool) -> MetricsLoggerExplicitly clear all custom dimensions. The behavior of whether default dimensions should be used can be configured with theuse_defaultparameter.Examples:reset_dimensions(False)# this will clear all custom dimensions as well as disable default dimensionsset_namespace(value: str) -> MetricsLoggerSets the CloudWatchnamespacethat extracted metrics should be published to. If not set, a default value of aws-embedded-metrics will be used.Requirements:Name Length 1-255 charactersName must be ASCII characters onlyNamespace must meet CloudWatch Namespace requirements, otherwise aInvalidNamespaceErrorwill be thrown. SeeNamespacesfor valid values.Examples:set_namespace("MyApplication")set_timestamp(timestamp: datetime) -> MetricsLoggerSets the timestamp of the metrics. If not set, current time of the client will be used.Timestamp must meet CloudWatch requirements, otherwise a InvalidTimestampError will be thrown. SeeTimestampsfor valid values.Examples:set_timestamp(datetime.datetime.now())flush()Flushes the current MetricsContext to the configured sink and resets all properties and metric values. The namespace and default dimensions will be preserved across flushes.
Custom dimensions arenotpreserved by default, but this behavior can be changed by settinglogger.flush_preserve_dimensions = True, so that custom dimensions would be preserved after each flushing thereafter.Example:logger.flush()# only default dimensions will be preserved after each flush()logger.flush_preserve_dimensions=Truelogger.flush()# custom dimensions and default dimensions will be preserved after each flush()logger.reset_dimensions(False)logger.flush()# default dimensions are disabled; no dimensions will be preserved after each flush()ConfigurationAll configuration values can be set using environment variables with the prefix (AWS_EMF_). Configuration should be performed as close to application start up as possible.ServiceName: Overrides the name of the service. For services where the name cannot be inferred (e.g. Java process running on EC2), a default value of Unknown will be used if not explicitly set.Requirements:Name Length 1-255 charactersName must be ASCII characters onlyExample:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.service_name="MyApp"# environmentAWS_EMF_SERVICE_NAME=MyAppServiceType: Overrides the type of the service. For services where the type cannot be inferred (e.g. Java process running on EC2), a default value of Unknown will be used if not explicitly set.Requirements:Name Length 1-255 charactersName must be ASCII characters onlyExample:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.service_type="NodeJSWebApp"# environmentAWS_EMF_SERVICE_TYPE=NodeJSWebAppLogGroupName: For agent-based platforms, you may optionally configure the destination log group that metrics should be delivered to. This value will be passed from the library to the agent in the Embedded Metric payload. If a LogGroup is not provided, the default value will be derived from the service name: -metricsRequirements:Name Length 1-512 charactersLog group names consist of the following characters: a-z, A-Z, 0-9, '_' (underscore), '-' (hyphen), '/' (forward slash), and '.' (period). Pattern: [.-_/#A-Za-z0-9]+Example:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.log_group_name="LogGroupName"# environmentAWS_EMF_LOG_GROUP_NAME=LogGroupNameLogStreamName: For agent-based platforms, you may optionally configure the destination log stream that metrics should be delivered to. This value will be passed from the library to the agent in the Embedded Metric payload. If a LogGroup is not provided, the default value will be derived by the agent (this will likely be the hostname).Requirements:Name Length 1-512 charactersThe ':' (colon) and '*' (asterisk) characters are not allowed. Pattern: [^:]*Example:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.log_stream_name="LogStreamName"# environmentAWS_EMF_LOG_STREAM_NAME=LogStreamNameNameSpace: Overrides the CloudWatchnamespace. If not set, a default value of aws-embedded-metrics will be used.Requirements:Name Length 1-512 charactersName must be ASCII characters onlyExample:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.namespace="MyApplication"# environmentAWS_EMF_NAMESPACE=MyApplicationDISABLE_METRIC_EXTRACTION: Disables extraction of metrics by CloudWatch, by omitting EMF
metadata from serialized log records.Example:# in processfromaws_embedded_metrics.configimportget_configConfig=get_config()Config.disable_metric_extraction=True# environmentAWS_EMF_DISABLE_METRIC_EXTRACTION=trueExamplesCheck out theexamplesdirectory to get started.DevelopmentInstall Test Dependenciespip install toxRun teststoxIntegration tests. These tests require Docker to run the CloudWatch Agent and valid AWS credentials. Tests can be run by:exportAWS_ACCESS_KEY_ID=exportAWS_SECRET_ACCESS_KEY=exportAWS_REGION=us-west-2
./bin/run-integ-tests.shLicenseThis project is licensed under the Apache-2.0 License.