通过Python CDK筛选AWS日志 – 过滤日志模式错误。

huangapple go评论73阅读模式
英文:

Filtering aws logs via python cdk - filter log pattern errors

问题

我能够在CloudWatch日志中运行查询,没有问题...

filter eventName = 'SomeEvent' and responseElements.someId in ['string1', 'string2']

现在我正在尝试在Python中复制相同的操作,因为我必须为许多ID执行此操作。

通过...

result = cw.filter_log_events(
    logGroupName='/some-log-group',
    startTime=start_time,
    endTime=end_time,
    filterPattern=filter_pattern,
    limit=1000
)

其中过滤模式如下定义。

这个有效...

filter_pattern = { $.name = "SomeName" }

这个无效...

ids = ['string1','string2','string3']
ids_pattern = '|'.join(ids)
filter_pattern = f'{{ $.name = "SomeName" && $.responseElements.someId like /{ids_pattern}/ }}'

这个也无效...

ids = ['string1','string2','string3']
ids_pattern = ','.join(ids)
filter_pattern = f'{{ $.name = "SomeName" && $.responseElements.someId in [{ids_pattern}] }}'

错误 - 调用FilterLogEvents操作时: 无效的过滤模式

文档链接在这里...
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html

不清楚是否支持likein,但想知道是否这是问题的原因?

英文:

I am able to run a query in CloudWatch logs with no issues...

filter eventName = 'SomeEvent' and responseElements.someId in ['string1', 'string2']

I am now trying to replicate the same in python because I have to do this for many ids.

via...

result = cw.filter_log_events(
    logGroupName='/some-log-group',
    startTime=start_time,
    endTime=end_time,
    filterPattern=filter_pattern,
    limit=1000
)

where the filter patterns are defined below.

This works...

filter_pattern = { $.name = "SomeName" }

This does not...

ids = ['string1','string2','string3']
ids_pattern = '|'.join(ids)
filter_pattern = f'{{ $.name = "SomeName" && $.responseElements.someId like /{ids_pattern}/ }}'

Nor does this...

ids = ['string1','string2','string3']
ids_pattern = ','.join(ids)
filter_pattern = f'{{ $.name = "SomeName" && $.responseElements.someId in [{ids_pattern}] }}'

Error - when calling the FilterLogEvents operation: Invalid filter pattern

Documentation here...
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html

Does not make it clear if either like or in is supported but wondering if this is the issue?

答案1

得分: 0

糟糕的文档,因为似乎不支持inlike,而错误没有帮助解决这个问题。

改为...

{ $.name = "SomeName" && ($.responseElements.someId = "string1" || $.responseElements.someId = "string2") }
英文:

Bad documentation since it looks like in or like may not be supported while the errors do nothing to help figure that out.

Instead went with...

{ $.name = "SomeName" && ($.responseElements.someId = "string1" || $.responseElements.someId = "string2") }

</details>



huangapple
  • 本文由 发表于 2023年8月5日 03:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定