Skip to content

Commit 60a7167

Browse files
ran-isenbergRan Isenberg
and
Ran Isenberg
authored
feature: add business Metrics (#9)
* feature: add business metrics * fix test Co-authored-by: Ran Isenberg <ran.isenberg@cyberark.com>
1 parent 4c2a227 commit 60a7167

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ This repository is the complementary code examples of my blog series "AWS Lambda
4242

4343

4444
## The Blog Series
45-
- First blog post - Logging: https://isenberg-ran.medium.com/aws-lambda-cookbook-elevate-your-handlers-code-part-1-bc160a10679a
46-
- Second blog post- Observability: Monitoring and Tracing: https://isenberg-ran.medium.com/aws-lambda-cookbook-elevate-your-handlers-code-part-2-observability-eff158307180
45+
- First blog post - Logging: https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-elevate-your-handler-s-code-part-1-logging
46+
- Second blog post- Observability: Monitoring and Tracing: https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-elevate-your-handler-s-code-part-2-observability
47+
- Third blog post- Observability: Business KPIs Metrics: https://isenberg-ran.medium.com/aws-lambda-cookbook-elevate-your-handlers-code-part-2-observability-eff158307180
4748

4849

4950
## Getting started

cdk/aws_lambda_handler_cookbook/service_stack/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
API_HANDLER_LAMBDA_TIMEOUT = 10 # seconds
1010
POWERTOOLS_SERVICE_NAME = 'POWERTOOLS_SERVICE_NAME'
1111
SERVICE_NAME = 'CookBook'
12+
METRICS_NAMESPACE = 'my_product_kpi'
1213
POWERTOOLS_TRACE_DISABLED = 'POWERTOOLS_TRACE_DISABLED'
1314
POWER_TOOLS_LOG_LEVEL = 'LOG_LEVEL'
1415
BUILD_FOLDER = '.build/lambdas/'

cdk/aws_lambda_handler_cookbook/service_stack/cookbook_construct.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
COMMION_LAYER_BUILD_FOLDER,
1717
GW_RESOURCE,
1818
LAMBDA_BASIC_EXECUTION_ROLE,
19+
METRICS_NAMESPACE,
1920
POWER_TOOLS_LOG_LEVEL,
2021
POWERTOOLS_SERVICE_NAME,
2122
POWERTOOLS_TRACE_DISABLED,

service/handlers/my_handler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
from http import HTTPStatus
33
from typing import Any, Dict
44

5+
from aws_lambda_powertools.metrics.metrics import MetricUnit
56
from aws_lambda_powertools.utilities.typing import LambdaContext
67

7-
from service.utils.infra import logger, tracer
8+
from service.utils.infra import logger, metrics, tracer
89

910

1011
@tracer.capture_method(capture_response=False)
1112
def inner_function_example(event: Dict[str, Any]) -> Dict[str, Any]:
1213
return {}
1314

1415

16+
@metrics.log_metrics
1517
@tracer.capture_lambda_handler(capture_response=False)
1618
def my_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
1719
logger.set_correlation_id(context.aws_request_id)
1820
logger.debug('my_handler is called, calling inner_function_example')
1921
inner_function_example(event)
2022
logger.debug('inner_function_example finished successfully')
23+
metrics.add_metric(name='ValidEvents', unit=MetricUnit.Count, value=1)
2124
return {'statusCode': HTTPStatus.OK, 'headers': {'Content-Type': 'application/json'}, 'body': json.dumps({'message': 'success'})}

service/utils/infra.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from aws_lambda_powertools.logging.logger import Logger
2+
from aws_lambda_powertools.metrics.metrics import Metrics
23
from aws_lambda_powertools.tracing.tracer import Tracer
34

4-
SERVICE_NAME = 'my_service'
5+
METRICS_NAMESPACE = 'my_product_kpi'
56

67
# JSON output format, service name can be set by environment variable "POWERTOOLS_SERVICE_NAME"
78
logger: Logger = Logger()
89

910
# service name can be set by environment variable "POWERTOOLS_SERVICE_NAME". Disabled by setting POWERTOOLS_TRACE_DISABLED to "True"
1011
tracer: Tracer = Tracer()
12+
13+
# namespace and service name can be set by environment variable "POWERTOOLS_METRICS_NAMESPACE" and "POWERTOOLS_SERVICE_NAME" accordingly
14+
metrics = Metrics(namespace=METRICS_NAMESPACE)

tests/unit/test_my_handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import json
2+
import os
23
from http import HTTPStatus
34

5+
import pytest
6+
7+
from cdk.aws_lambda_handler_cookbook.service_stack.constants import POWER_TOOLS_LOG_LEVEL, POWERTOOLS_SERVICE_NAME, SERVICE_NAME
48
from service.handlers.my_handler import my_handler
59
from tests.utils import generate_api_gw_event, generate_context
610

711

12+
@pytest.fixture(scope='module', autouse=True)
13+
def init():
14+
os.environ[POWERTOOLS_SERVICE_NAME] = SERVICE_NAME
15+
os.environ[POWER_TOOLS_LOG_LEVEL] = 'DEBUG'
16+
17+
818
def test_handler_200_ok():
919
response = my_handler(generate_api_gw_event(), generate_context())
1020
assert response['statusCode'] == HTTPStatus.OK

0 commit comments

Comments
 (0)