Datadog Metrics Aren' t在我的Python应用程序上工作

发布于 2025-01-21 16:37:20 字数 4660 浏览 2 评论 0原文

我在Datadog网站上提到了一些文档,但它仍然没有收集任何指标并将其发送到Datadog。该功能完美地执行,我认为我配置了正确的所有内容,但它不起作用。

这是我的应用程序代码,其中包括所需的模块和函数“包装器”,它提到要放在我的lambda函数模块上方,

from botocore.vendored import requests
import os
import smtplib
from datetime import datetime
from datadog_lambda.metric import lambda_metric
from datadog_lambda.wrapper import datadog_lambda_wrapper

lat = '33.4483771'
lon = '-112.0740373'
exclude = 'minutely,hourly,alerts'

url = (
    'https://api.openweathermap.org/data/2.5/onecall?' +
    'lat={lat}&lon={lon}&exclude={exclude}&appid={API_key}&units=imperial'
)

if os.path.isfile('.env'):
    from dotenv import load_dotenv
    load_dotenv()

def __send_email(msg: str) -> None:
    gmail_user = os.getenv('EMAIL_USER')
    gmail_password = os.getenv('EMAIL_PASSWORD')

    mail_from = gmail_user
    mail_to = gmail_user

    mail_subject = f'Weather Today {datetime.today().strftime("%m/%d/%Y")}'
    mail_message = f'Subject: {mail_subject}\n\n{msg}'

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(gmail_user, gmail_password)
    server.sendmail(mail_from, mail_to, mail_message)
    server.close()

@datadog_lambda_wrapper
def handler(event, context):
    response = requests.get(url.format(
        lat=lat,
        lon=lon,
        exclude=exclude,
        API_key=os.getenv('WEATHER_API_KEY')
    ))

    data = response.json()

    rain_conditions = ['rain', 'thunderstorm', 'drizzle']
    snow_conditions = ['snow']

    today_weather = data['daily'][0]['weather'][0]['main'].lower()

    if today_weather in rain_conditions:
        msg = 'Pack an umbrella!'
    elif today_weather in snow_conditions:
        msg = 'Pack your snow boots!'
    else:
        msg = 'Clear skies today!'

    __send_email(msg) 

        # submit a custom metric
    lambda_metric(
        metric_name='coffee_house.order_value',
        value=12.45,
        tags=['product:latte', 'order:online']
    )
    
handler(None, None)

我创建了一个lambda层,并从他们的网站上找到了正确的ARN,

arn:aws:lambda:us-west-2:464622532012:layer:Datadog-Python37:56

我还为我的函数添加了一个名为dd_api_key的变量,称为dd_api_key文档并使用我的API密钥。非常感谢任何帮助或指导!

更新:

包括功能日志:

START RequestId: 7370df11-49d7-4759-a8e4-eff2916a620a Version: $LATEST
Traceback (most recent call last):
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/wrapper.py", line 156, in _before
submit_invocations_metric(context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/metric.py", line 124, in submit_invocations_metric
submit_enhanced_metric("invocations", lambda_context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/metric.py", line 112, in submit_enhanced_metric
tags = get_enhanced_metrics_tags(lambda_context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/tags.py", line 87, in get_enhanced_metrics_tags
return parse_lambda_tags_from_arn(lambda_context) + [
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/tags.py", line 37, in parse_lambda_tags_from_arn
split_arn = lambda_context.invoked_function_arn.split(":")
AttributeError: 'NoneType' object has no attribute 'invoked_function_arn'
/opt/python/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.get() function instead.
DeprecationWarning
Traceback (most recent call last):
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/wrapper.py", line 190, in _after
status_code = extract_http_status_code_tag(self.trigger_tags, self.response)
AttributeError: '_LambdaDecorator' object has no attribute 'trigger_tags'
{"m": "aws.lambda.enhanced.invocations", "v": 1, "e": 1649914335, "t": ["region:us-west-2", "account_id:775362094965", "functionname:DataDog", "resource:DataDog", "cold_start:false", "memorysize:128", "runtime:python3.7", "datadog_lambda:v3.56.0", "dd_lambda_layer:datadog-python37_3.56.0"]}
/opt/python/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.get() function instead.
DeprecationWarning
END RequestId: 7370df11-49d7-4759-a8e4-eff2916a620a

I referred to a few docs on DataDog's website and it's still not collecting any metrics and sending them to DataDog. The function executes perfectly and I thought I configured everything correctly but it's not working.

Here's my application code that includes the required modules and the function "wrapper" it mentions to put above my lambda function module

from botocore.vendored import requests
import os
import smtplib
from datetime import datetime
from datadog_lambda.metric import lambda_metric
from datadog_lambda.wrapper import datadog_lambda_wrapper

lat = '33.4483771'
lon = '-112.0740373'
exclude = 'minutely,hourly,alerts'

url = (
    'https://api.openweathermap.org/data/2.5/onecall?' +
    'lat={lat}&lon={lon}&exclude={exclude}&appid={API_key}&units=imperial'
)

if os.path.isfile('.env'):
    from dotenv import load_dotenv
    load_dotenv()

def __send_email(msg: str) -> None:
    gmail_user = os.getenv('EMAIL_USER')
    gmail_password = os.getenv('EMAIL_PASSWORD')

    mail_from = gmail_user
    mail_to = gmail_user

    mail_subject = f'Weather Today {datetime.today().strftime("%m/%d/%Y")}'
    mail_message = f'Subject: {mail_subject}\n\n{msg}'

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(gmail_user, gmail_password)
    server.sendmail(mail_from, mail_to, mail_message)
    server.close()

@datadog_lambda_wrapper
def handler(event, context):
    response = requests.get(url.format(
        lat=lat,
        lon=lon,
        exclude=exclude,
        API_key=os.getenv('WEATHER_API_KEY')
    ))

    data = response.json()

    rain_conditions = ['rain', 'thunderstorm', 'drizzle']
    snow_conditions = ['snow']

    today_weather = data['daily'][0]['weather'][0]['main'].lower()

    if today_weather in rain_conditions:
        msg = 'Pack an umbrella!'
    elif today_weather in snow_conditions:
        msg = 'Pack your snow boots!'
    else:
        msg = 'Clear skies today!'

    __send_email(msg) 

        # submit a custom metric
    lambda_metric(
        metric_name='coffee_house.order_value',
        value=12.45,
        tags=['product:latte', 'order:online']
    )
    
handler(None, None)

I created a lambda layer with the correct ARN found from their website

arn:aws:lambda:us-west-2:464622532012:layer:Datadog-Python37:56

I also added a variable for my function called DD_API_KEY as per the docs and used my API key for it. Any help or guidance is greatly appreciated!

Update:

Including function log:

START RequestId: 7370df11-49d7-4759-a8e4-eff2916a620a Version: $LATEST
Traceback (most recent call last):
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/wrapper.py", line 156, in _before
submit_invocations_metric(context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/metric.py", line 124, in submit_invocations_metric
submit_enhanced_metric("invocations", lambda_context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/metric.py", line 112, in submit_enhanced_metric
tags = get_enhanced_metrics_tags(lambda_context)
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/tags.py", line 87, in get_enhanced_metrics_tags
return parse_lambda_tags_from_arn(lambda_context) + [
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/tags.py", line 37, in parse_lambda_tags_from_arn
split_arn = lambda_context.invoked_function_arn.split(":")
AttributeError: 'NoneType' object has no attribute 'invoked_function_arn'
/opt/python/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.get() function instead.
DeprecationWarning
Traceback (most recent call last):
File "/opt/python/lib/python3.7/site-packages/datadog_lambda/wrapper.py", line 190, in _after
status_code = extract_http_status_code_tag(self.trigger_tags, self.response)
AttributeError: '_LambdaDecorator' object has no attribute 'trigger_tags'
{"m": "aws.lambda.enhanced.invocations", "v": 1, "e": 1649914335, "t": ["region:us-west-2", "account_id:775362094965", "functionname:DataDog", "resource:DataDog", "cold_start:false", "memorysize:128", "runtime:python3.7", "datadog_lambda:v3.56.0", "dd_lambda_layer:datadog-python37_3.56.0"]}
/opt/python/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'.  This is not a public API in botocore and will be removed in the future. Additionally, this version of requests is out of date.  We recommend you install the requests package, 'import requests' directly, and use the requests.get() function instead.
DeprecationWarning
END RequestId: 7370df11-49d7-4759-a8e4-eff2916a620a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文