You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda函数无法向New Relic API发送自定义事件求助

问题排查与修复方案

核心问题:New Relic API URL格式错误

你当前使用的API URL是占位符格式,未替换为实际的New Relic账号ID,这是触发404错误的直接原因:

# 错误URL
url = "https://insights-collector.newrelic.com/v1/newrelic_account_name/newrelic_account_id/events"

New Relic Insights Events API的正确URL格式为:

https://insights-collector.newrelic.com/v1/accounts/{YOUR_ACCOUNT_ID}/events

其中{YOUR_ACCOUNT_ID}是你的New Relic数字账号ID,无需包含账号名称。

其他可能的问题与修复步骤

1. 确认API密钥类型

确保从Secrets Manager获取的是Insights Insert Key,而非Admin Key或其他类型密钥。只有Insert Key具备向Events API发送数据的权限。

2. 适配区域端点

若你的New Relic账号属于欧盟区域,需使用欧盟专属端点:

url = "https://insights-collector.eu.newrelic.com/v1/accounts/{YOUR_ACCOUNT_ID}/events"

3. 代码优化与错误排查增强

  • 修复get_new_relic_api_key调用冗余:函数参数为secret_name,但调用时重复传入硬编码字符串,改为使用变量:
    secret_name = "aws_secret/newrelicapikey"
    api_key = get_new_relic_api_key(secret_name)
    
  • 增强异常处理,打印具体错误信息便于排查:
    except Exception as e:
        print(f"New Relic请求失败: {str(e)}")
        return False
    
  • 完善payload信息,增加子网ID、可用IP数等关键字段,方便在New Relic中筛选分析:
    payload = [
        {
            "eventType": "SubnetAlert",
            "alertMessage": alert_message,
            "subnetId": subnet_info['SubnetId'],
            "availableIps": available_ips,
            "vpcId": subnet_info['VpcId']
        }
    ]
    

修复后的完整send_newrelic_alert_event函数示例

def send_newrelic_alert_event(alert_message, subnet_info, available_ips, api_key):
    # 替换为你的实际New Relic账号ID
    NEW_RELIC_ACCOUNT_ID = "12345678"
    # 根据账号区域选择端点
    url = f"https://insights-collector.newrelic.com/v1/accounts/{NEW_RELIC_ACCOUNT_ID}/events"

    headers = {
        "X-Insert-Key": api_key,
        "Content-Type": "application/json"
    }
    
    payload = [
        {
            "eventType": "SubnetAlert",
            "alertMessage": alert_message,
            "subnetId": subnet_info['SubnetId'],
            "availableIps": available_ips,
            "vpcId": subnet_info['VpcId'],
            "subnetCidr": subnet_info['CidrBlock']
        }
    ]
    payload_bytes = json.dumps(payload).encode('utf-8')
    req = urllib.request.Request(url, data=payload_bytes, headers=headers, method='POST')
    
    try:
        print("Sending request to New Relic:")
        print("URL:", req.full_url)
        print("Headers:", req.headers)
        print("Payload:", payload)
        with urllib.request.urlopen(req) as response:
            if response.status == 200:
                print("New Relic告警发送成功")
                return True
            else:
                print(f"New Relic响应异常,状态码: {response.status}")
                return False
    except Exception as e:
        print(f"New Relic请求失败: {str(e)}")
        return False

验证步骤

  1. 替换NEW_RELIC_ACCOUNT_ID为你的实际账号ID
  2. 确认Secrets Manager中存储的是正确的Insights Insert Key
  3. 测试Lambda函数,查看CloudWatch日志中的请求URL和错误信息

内容的提问来源于stack exchange,提问作者Nihit Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:30:22