Cloudwatch Insight 查询 Lambda

发布于 2025-01-12 19:23:53 字数 2310 浏览 3 评论 0原文

我遇到问题,因为我试图自动化 cloudwatch Logs 洞察查询。我找到了这段代码

def getInsightLogCounts():
print("starting cloudwatch insight queries")
msg = ''
#STEP 1 - create a dict with query message as key and query as value. This can be stored in the db as well
query_Dict = getQueryDict()
log_group = '/the/loggroup/you/want/to/'
print("starting cloudwatch insight queries for " + str(log_group))
for x in query_Dict.items():
    query_key = x[0]
    query = x[1]
    print("query key : " + str(query_key) + " \n query : " + str(query))
#STEP 2 - Create a query response object using the start_query method of the logs. Here we are fetching data for the last 24 hours
start_query_response = logs_client.start_query(
        logGroupName=log_group,
        queryString=query,
        startTime=int((datetime.today() - timedelta(hours=24)).timestamp()),
        endTime=int(datetime.now().timestamp()),
        limit=1)
query_id = start_query_response['queryId']
    response = None
#STEP3 - run a while loop and waith for the query to complete.
while response == None or response['status'] == 'Running':
        time.sleep(1)
        response = logs_client.get_query_results(queryId=query_id)
#STEP4 - Extract the result and proceed to the next query
if response and response['results'] and response['results'][0] and response['results'][0][1]:
        #response found update msg
        msg = msg + str(query_key) + " : " + str(response['results'][0][1].get('value'))  + "; \n"
        print("query value returned for " + str(query_key) + " is : " + str(response['results'][0][1].get('value')))
    else:
        msg = msg + str(query_key) + " : 0"  + "; \n"
        print("no query data returned for " + str(query_key))
return msg

现在我需要将我的查询放入这段代码中,但我对 Lambda (Python) 代码很陌生。

我的查询是(示例一)

fields @timestamp, @message
| filter @message like 'END RequestId'
| sort @timestamp desc 

如果有人有一些想法,如果您能帮助我或只是给我一些建议,我将非常感激,

这是我从中获取代码的原始帖子的链接。

https://medium.com/xebia-engineering/accessing-cloudwatch-metrics-and-insights-from-aws-lambda-1119c40ff80b#5833

抱歉各位,之前的帖子不清楚。我设法将我的代码放在这里,希望有爱心的人可以帮助我

I have a problem, as I trying to automate the cloudwatch Logs insight query. I found this code

def getInsightLogCounts():
print("starting cloudwatch insight queries")
msg = ''
#STEP 1 - create a dict with query message as key and query as value. This can be stored in the db as well
query_Dict = getQueryDict()
log_group = '/the/loggroup/you/want/to/'
print("starting cloudwatch insight queries for " + str(log_group))
for x in query_Dict.items():
    query_key = x[0]
    query = x[1]
    print("query key : " + str(query_key) + " \n query : " + str(query))
#STEP 2 - Create a query response object using the start_query method of the logs. Here we are fetching data for the last 24 hours
start_query_response = logs_client.start_query(
        logGroupName=log_group,
        queryString=query,
        startTime=int((datetime.today() - timedelta(hours=24)).timestamp()),
        endTime=int(datetime.now().timestamp()),
        limit=1)
query_id = start_query_response['queryId']
    response = None
#STEP3 - run a while loop and waith for the query to complete.
while response == None or response['status'] == 'Running':
        time.sleep(1)
        response = logs_client.get_query_results(queryId=query_id)
#STEP4 - Extract the result and proceed to the next query
if response and response['results'] and response['results'][0] and response['results'][0][1]:
        #response found update msg
        msg = msg + str(query_key) + " : " + str(response['results'][0][1].get('value'))  + "; \n"
        print("query value returned for " + str(query_key) + " is : " + str(response['results'][0][1].get('value')))
    else:
        msg = msg + str(query_key) + " : 0"  + "; \n"
        print("no query data returned for " + str(query_key))
return msg

Now I need to fit my query into this code, but I'm new to the Lambda (Python) codes.

My query is (sample one)

fields @timestamp, @message
| filter @message like 'END RequestId'
| sort @timestamp desc 

If anyone has some ideas I would be very grateful if you can help me with this or just give me some advice

this is a link for the original post I took the code from.

https://medium.com/xebia-engineering/accessing-cloudwatch-metrics-and-insights-from-aws-lambda-1119c40ff80b#5833

Sorry guys, for the not clear post before. I manage to fit my code here so hopefully, someone with a big heart can help me

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

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

发布评论

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