How do I validate that a Cloudwatch Log Group and Log Stream exists?
Asked Answered
S

3

7

Is there any method in java to check whether a given Log Group and Log Stream exists, before getting log events from the Log Group?

Saunders answered 15/11, 2016 at 3:36 Comment(0)
A
3

Pseudocode: Validate that a log group's log stream exists

  1. Build describeLogStreamsRequest:
    • Pass in your given log group name on the constructor, or on the request's withLogGroupName setter.
    • Pass in log stream name in on the request's withLogStreamNamePrefix setter.
  2. Call describeLogStreams.
  3. Inspect the resulting log streams on the DescribeLogStreamsResult object. If the list isn't empty, you're safe to further operate on that stream.

Java: Validate that a log group's log stream exists (note: untested)

AWSLogsClient logs = new AWSLogsClient();

DescribeLogStreamsRequest req = new DescribeLogStreamsRequest("myLogGroupName")
    .withLogStreamNamePrefix("myLogStreamName");

DescribeLogStreamsResult res = logs.describeLogStreams(req);

if(res != null && res.getLogStreams() != null && !res.getLogStreams().isEmpty())
{
  // Log Stream exists, do work here
}
Axilla answered 23/11, 2016 at 17:12 Comment(1)
Just a heads up, a call to AWSLogsClient#describeLogStreams with a logGroupName that does not exist will generate a ResourceNotFoundException. If you are unsure about the existence of the log group, you should handle the exception.Webfooted
S
1

I know this question asks for a Java solution, but this was the top-ranked question on Google when I had the same question for Python. The boto3 documentation doesn't seem to directly support a "does this log group exist?" function as of today, so here is what I wrote instead. I use the paginator because by default the boto3 API only lets you pull 50 log groups at a time. The paginator will keep fetching log group names until it goes through all of your log groups.

import boto3


def log_group_exists(log_group_name):
    client = boto3.client('logs')
    paginator = client.get_paginator('describe_log_groups')
    for page in paginator.paginate():
        for group in page['logGroups']:
            if group['logGroupName'].lower() == log_group_name.lower():
                return True
    return False
Stovepipe answered 7/2, 2023 at 20:24 Comment(0)
W
0

In reality, a call to AWSLogsClient#describeLogStreams with a logGroupName that does not exist will generate a ResourceNotFoundException. For that reason, you should check for:

  1. Absence of ResourceNotFoundException.
  2. Existence of a single entry in DescribeLogStreamsResult#getLogStreams matching the logStreamName provided.

Code snippet of a method that will do that:

private boolean doesLogStreamExist() {
    DescribeLogStreamsRequest request = new DescribeLogStreamsRequest()
        .withLogGroupName(logGroupName)
        .withLogStreamNamePrefix(logStreamName);
    try {
        return client.describeLogStreams(request).getLogStreams()
            .stream()
            .anyMatch(it -> it.getLogStreamName().equals(logStreamName));
    } catch (ResourceNotFoundException e) {
        // log group does not exist
        return false;
    }
}
Webfooted answered 28/12, 2017 at 18:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.