def test_lambda_metric_flush_to_log(self): os.environ["DD_FLUSH_TO_LOG"] = 'True' lambda_metric('test', 1) self.mock_metric_lambda_stats.distribution.assert_not_called() del os.environ["DD_FLUSH_TO_LOG"]
def handle(event, context): # Parse request ID and record ids out of the event to include in the response request_id = event.get("requestContext", {}).get("requestId") event_records = event.get("Records", []) record_ids = [] for record in event_records: # SQS if record.get("messageId"): record_ids.append(record["messageId"]) # SNS if record.get("Sns", {}).get("MessageId"): record_ids.append(record["Sns"]["MessageId"]) lambda_metric("hello.dog", 1, tags=["team:serverless", "role:hello"]) lambda_metric( "tests.integration.count", 21, tags=["test:integration", "role:hello"] ) return { "statusCode": 200, "body": { "message": "hello, dog!", "request_id": request_id, "event_record_ids": record_ids, }, }
def report_metrics(metric_name: str, value, tags: list = None): tags = tags if tags else [] lambda_metric( metric_name=metric_name, value=value, timestamp=int(time.time()), # optional, must be within last 20 mins tags=tags)
def write_datadog_metric(self, direction, new_desired_count): lambda_metric("aws.lambda.auto_scaling.desired_count", new_desired_count, tags=[ f"service:{self.service}", f"cluster:{self.cluster}", f"direction:{direction}" ])
def get_oxt_balance(address=get_secret(key=os.environ['PAC_FUNDER_PUBKEY_SECRET'])) -> float: token_addr = w3.toChecksumAddress(os.environ['TOKEN']) token_contract = w3.eth.contract( abi=token_abi, address=token_addr, ) token_name = get_token_name(token_addr) token_symbol = get_token_symbol(token_addr) token_decimals = get_token_decimals(token_addr) DECIMALS = 10 ** token_decimals raw_balance = token_contract.functions.balanceOf(address).call() balance = raw_balance / DECIMALS logging.info( f"Balance of {address}: {balance} {token_name} ({token_symbol})") if is_true(os.environ.get('ENABLE_MONITORING', '')): lambda_metric( f"orchid.pac.balance.{token_symbol.lower()}", balance, tags=[ f'account:{address}', f'token_name:{token_name}', f'token_symbol:{token_symbol}', f'token_decimals:{token_decimals}', ] ) return balance
def timeout(event, context): global should_send_metric if should_send_metric: lambda_metric(metric_name='serverless.lambda-extension.integration-test.count', value=1) should_send_metric = False time.sleep(15 * 60) return {"statusCode": 200, "body": "ok"}
def metric(metric_name: str, value: float, timestamp=None, tags=None): if is_true(os.environ.get('ENABLE_MONITORING', '')): lambda_metric( metric_name=metric_name, value=value, timestamp=timestamp, tags=tags, )
def handle(event, context): lambda_metric("hello.dog", 1, tags=["team:serverless", "role:hello"]) lambda_metric("tests.integration.count", 21, tags=["test:integration", "role:hello"]) requests.get("https://httpstat.us/400/") return {"statusCode": 200, "body": {"message": "hello, dog!"}}
def index(): if request.method == 'POST': request_string = request.get_json()[ 'string'] # expecting {"string": "blahhh"} xray_recorder.put_annotation('request_body', repr(request.get_json())) app.logger.info(f"Reverser called with {request_string}") lambda_metric( "reverse_service.string_length", # metric len(request_string), # value tags=['serverless:reverser']) response = {'response': request_string.swapcase()} return response return {'hello': 'world'}
def get_eth_balance(address=get_secret( key=os.environ['PAC_FUNDER_PUBKEY_SECRET'])) -> float: token_name = 'Ethereum' token_symbol = 'ETH' token_decimals = 18 DECIMALS = 10**token_decimals raw_balance = w3.eth.getBalance(address) balance = raw_balance / DECIMALS logging.info( f"Balance of {address}: {balance} {token_name} ({token_symbol})") if is_true(os.environ.get('ENABLE_MONITORING', '')): lambda_metric(f"orchid.pac.balance.{token_symbol.lower()}", balance, tags=[ f'account:{address}', f'token_name:{token_name}', f'token_symbol:{token_symbol}', f'token_decimals:{token_decimals}', ]) return balance
def test_lambda_metric_tagged_with_dd_lambda_layer(self): lambda_metric('test', 1) lambda_metric('test', 1, 123, []) lambda_metric('test', 1, tags=['tag1:test']) expected_tag = _format_dd_lambda_layer_tag() self.mock_metric_lambda_stats.distribution.assert_has_calls([ call('test', 1, timestamp=None, tags=[expected_tag]), call('test', 1, timestamp=123, tags=[expected_tag]), call('test', 1, timestamp=None, tags=['tag1:test', expected_tag]), ])
def lambda_handler(event, context): lambda_metric("test.metric", 100)