Skip to content

Commit 2ab00a5

Browse files
feat: Allow group identifiers to be used in wr.cloudwatch queries (#2430)
1 parent ed14533 commit 2ab00a5

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

awswrangler/cloudwatch.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ def start_query(
4040
4141
Parameters
4242
----------
43-
query : str
43+
query: str
4444
The query string.
45-
log_group_names : str
46-
The list of log groups to be queried. You can include up to 20 log groups.
47-
start_time : datetime.datetime
45+
log_group_names: List[str]
46+
The list of log group names or ARNs to be queried. You can include up to 50 log groups.
47+
start_time: datetime.datetime
4848
The beginning of the time range to query.
49-
end_time : datetime.datetime
49+
end_time: datetime.datetime
5050
The end of the time range to query.
51-
limit : Optional[int]
51+
limit: Optional[int]
5252
The maximum number of log events to return in the query.
53-
boto3_session : boto3.Session(), optional
53+
boto3_session: boto3.Session(), optional
5454
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
5555
5656
Returns
@@ -79,15 +79,19 @@ def start_query(
7979
_logger.debug("start_timestamp: %s", start_timestamp)
8080
_logger.debug("end_timestamp: %s", end_timestamp)
8181

82-
_validate_args(start_timestamp=start_timestamp, end_timestamp=end_timestamp)
82+
_validate_args(
83+
start_timestamp=start_timestamp,
84+
end_timestamp=end_timestamp,
85+
)
8386
args: Dict[str, Any] = {
84-
"logGroupNames": log_group_names,
87+
"logGroupIdentifiers": log_group_names,
8588
"startTime": start_timestamp,
8689
"endTime": end_timestamp,
8790
"queryString": query,
8891
}
8992
if limit is not None:
9093
args["limit"] = limit
94+
9195
client_logs = _utils.client(service_name="logs", session=boto3_session)
9296
response = client_logs.start_query(**args)
9397
return response["queryId"]
@@ -159,8 +163,8 @@ def run_query(
159163
----------
160164
query : str
161165
The query string.
162-
log_group_names : str
163-
The list of log groups to be queried. You can include up to 20 log groups.
166+
log_group_names: List[str]
167+
The list of log group names or ARNs to be queried. You can include up to 50 log groups.
164168
start_time : datetime.datetime
165169
The beginning of the time range to query.
166170
end_time : datetime.datetime
@@ -210,17 +214,17 @@ def read_logs(
210214
211215
Parameters
212216
----------
213-
query : str
217+
query: str
214218
The query string.
215-
log_group_names : str
216-
The list of log groups to be queried. You can include up to 20 log groups.
217-
start_time : datetime.datetime
219+
log_group_names: List[str]
220+
The list of log group names or ARNs to be queried. You can include up to 50 log groups.
221+
start_time: datetime.datetime
218222
The beginning of the time range to query.
219-
end_time : datetime.datetime
223+
end_time: datetime.datetime
220224
The end of the time range to query.
221-
limit : Optional[int]
225+
limit: Optional[int]
222226
The maximum number of log events to return in the query.
223-
boto3_session : boto3.Session(), optional
227+
boto3_session: boto3.Session(), optional
224228
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
225229
226230
Returns
@@ -384,15 +388,15 @@ def filter_log_events(
384388
385389
Note
386390
----
387-
Cannot call filter_log_events with both log_stream_names and log_stream_name_prefix.
391+
Cannot call ``filter_log_events`` with both ``log_stream_names`` and ``log_stream_name_prefix``.
388392
389393
Parameters
390394
----------
391-
log_group_name : str
395+
log_group_name: str
392396
The name of the log group.
393-
log_stream_name_prefix : str
397+
log_stream_name_prefix: str, optional
394398
Filters the results to include only events from log streams that have names starting with this prefix.
395-
log_stream_names: List[str]
399+
log_stream_names: List[str], optional
396400
Filters the results to only logs from the log streams in this list.
397401
filter_pattern : str
398402
The filter pattern to use. If not provided, all the events are matched.
@@ -432,7 +436,7 @@ def filter_log_events(
432436
"""
433437
if log_stream_name_prefix and log_stream_names:
434438
raise exceptions.InvalidArgumentCombination(
435-
"Cannot call filter_log_events with both log_stream_names and log_stream_name_prefix"
439+
"Cannot call `filter_log_events` with both `log_stream_names` and `log_stream_name_prefix`"
436440
)
437441
_logger.debug("log_group_name: %s", log_group_name)
438442

tests/unit/test_cloudwatch.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
pytestmark = pytest.mark.distributed
1313

1414

15-
def test_query_cancelled(loggroup):
15+
def test_query_cancelled(loggroup: str) -> None:
1616
client_logs = boto3.client("logs")
1717
with pytest.raises(exceptions.QueryCancelled):
1818
while True:
@@ -28,7 +28,7 @@ def test_query_cancelled(loggroup):
2828
wr.cloudwatch.wait_query(query_id=query_id)
2929

3030

31-
def test_start_and_wait_query(loggroup):
31+
def test_start_and_wait_query(loggroup: str) -> None:
3232
query_id = wr.cloudwatch.start_query(
3333
log_group_names=[loggroup], query="fields @timestamp, @message | sort @timestamp desc | limit 5"
3434
)
@@ -38,23 +38,32 @@ def test_start_and_wait_query(loggroup):
3838
assert len(results[0]) == 3
3939

4040

41-
def test_query(loggroup):
41+
def test_query(loggroup: str) -> None:
4242
results = wr.cloudwatch.run_query(
4343
log_group_names=[loggroup], query="fields @timestamp, @message | sort @timestamp desc | limit 5"
4444
)
4545
assert len(results) == 5
4646
assert len(results[0]) == 3
4747

4848

49-
def test_read_logs(loggroup):
49+
def test_read_logs(loggroup: str) -> None:
5050
df = wr.cloudwatch.read_logs(
5151
log_group_names=[loggroup], query="fields @timestamp, @message | sort @timestamp desc | limit 5", limit=5
5252
)
5353
assert len(df.index) == 5
5454
assert len(df.columns) == 3
5555

5656

57-
def test_describe_log_streams_and_filter_log_events(loggroup):
57+
def test_read_logs_by_log_group_arn(loggroup: str, account_id: str, region: str) -> None:
58+
loggroup_arn = f"arn:aws:logs:{region}:{account_id}:log-group:{loggroup}"
59+
df = wr.cloudwatch.read_logs(
60+
log_group_names=[loggroup_arn], query="fields @timestamp, @message | sort @timestamp desc | limit 5", limit=5
61+
)
62+
assert len(df.index) == 5
63+
assert len(df.columns) == 3
64+
65+
66+
def test_describe_log_streams_and_filter_log_events(loggroup: str) -> None:
5867
cloudwatch_log_client = boto3.client("logs")
5968
log_stream_names = [
6069
"aws_sdk_pandas_log_stream_one",

0 commit comments

Comments
 (0)