Skip to content

Do not recalculate enhanced metric setting each time. #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions datadog_lambda/extension.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from os import path
import os

AGENT_URL = "http://127.0.0.1:8124"
FLUSH_PATH = "/lambda/flush"
Expand All @@ -9,9 +9,7 @@


def is_extension_present():
if path.exists(EXTENSION_PATH):
return True
return False
return os.path.exists(EXTENSION_PATH)


def flush_extension():
Expand Down
16 changes: 5 additions & 11 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true"
lambda_stats = ThreadStatsWriter(flush_in_thread)

enhanced_metrics_enabled = (
os.environ.get("DD_ENHANCED_METRICS", "true").lower() == "true"
)


def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=False):
"""
Expand Down Expand Up @@ -89,24 +93,14 @@ def flush_stats():
lambda_stats.flush()


def are_enhanced_metrics_enabled():
"""Check env var to find if enhanced metrics should be submitted

Returns:
boolean for whether enhanced metrics are enabled
"""
# DD_ENHANCED_METRICS defaults to true
return os.environ.get("DD_ENHANCED_METRICS", "true").lower() == "true"


def submit_enhanced_metric(metric_name, lambda_context):
"""Submits the enhanced metric with the given name

Args:
metric_name (str): metric name w/o enhanced prefix i.e. "invocations" or "errors"
lambda_context (dict): Lambda context dict passed to the function by AWS
"""
if not are_enhanced_metrics_enabled():
if not enhanced_metrics_enabled:
logger.debug(
"Not submitting enhanced metric %s because enhanced metrics are disabled",
metric_name,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ def lambda_handler(event, context):
)

def test_no_enhanced_metrics_without_env_var(self):
os.environ["DD_ENHANCED_METRICS"] = "false"
patcher = patch("datadog_lambda.metric.enhanced_metrics_enabled", False)
patcher.start()
self.addCleanup(patcher.stop)

@wrapper.datadog_lambda_wrapper
def lambda_handler(event, context):
Expand All @@ -478,8 +480,6 @@ def lambda_handler(event, context):

self.mock_write_metric_point_to_stdout.assert_not_called()

del os.environ["DD_ENHANCED_METRICS"]

def test_only_one_wrapper_in_use(self):
patcher = patch("datadog_lambda.wrapper.submit_invocations_metric")
self.mock_submit_invocations_metric = patcher.start()
Expand Down