Fetching Redis INFO using Redigo

October 18, 2020

Redigo is a Go client for the Redis database.

It may be useful to be able to fetch & use Redis Server information within your Go program. The library currently doesn’t offer an explicit method to query for the information returned by the Redis INFO command.

This post outlines how to query Redis INFO when using the redigo library in a GoLang project. As an example, we’ll be fetching the uptime_in_seconds field from the INFO response.

First, ensure you have a redis connection to work with. In our example, this connection is named: conn

Send the "INFO" command, and pass it to the String helper method provided by redigo.

infoResponse, getInfoErr := redis.String(conn.Do("INFO"))

// Expected value: string blob with result of "INFO" command

Next, we’ll use a regular expression to define the field we want to extract from the INFO response. In our case, uptime_in_seconds field, which is: “Number of seconds since Redis server start”. Given that the value is expected to be an integer, our regex can explicitly search using a number.

findUptimeValueRegex := regexp.MustCompile(`uptime_in_seconds:(\d+)`)

// Expected value: uptime_in_seconds:403

And then, we’ll split the string using ":" to get the uptime value

uptimeDetails := strings.Split(findUptimeValueRegex.FindString(infoResponse), ":")

// Handle error in case the response was empty
if len(uptimeDetails) < 2{
    // return error
}

Finally, we convert the uptime value to an integer

strconv.ParseInt(uptimeDetails[1], 10, 64)

Putting it all together, we can have a function that accepts the Redis connection, and return the uptime value:

func getRedisServerUptime(conn redis.Conn) (int64, error) {
    // Send INFO command
    infoResponse, getInfoErr := redis.String(conn.Do("INFO"))
    if getInfoErr != nil {
        return 0, getInfoErr
    }

    // Define regex to extract required value
    findUptimeValueRegex := regexp.MustCompile(`uptime_in_seconds:(\d+)`)

    // Parse value
    uptimeDetails := strings.Split(findUptimeValueRegex.FindString(infoResponse), ":")

    // Handle error in case the response was empty
    if len(uptimeDetails) < 2{
        return 0, errors.New("uptime not found in redis server INFO")
    }

    // Convert value to an integer
    strconv.ParseInt(uptimeDetails[1], 10, 64)
}

This approach can be used to extract any field from the INFO result.

I’ll see if I can propose this as an addition to the library, since it could come in handy.

Happy Coding, and stay safe!