001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.redis;
018
019import java.util.Properties;
020
021import org.springframework.boot.actuate.health.AbstractHealthIndicator;
022import org.springframework.boot.actuate.health.Health;
023import org.springframework.boot.actuate.health.HealthIndicator;
024import org.springframework.data.redis.connection.ClusterInfo;
025import org.springframework.data.redis.connection.RedisClusterConnection;
026import org.springframework.data.redis.connection.RedisConnection;
027import org.springframework.data.redis.connection.RedisConnectionFactory;
028import org.springframework.data.redis.core.RedisConnectionUtils;
029import org.springframework.util.Assert;
030
031/**
032 * Simple implementation of a {@link HealthIndicator} returning status information for
033 * Redis data stores.
034 *
035 * @author Christian Dupuis
036 * @author Richard Santana
037 * @since 2.0.0
038 */
039public class RedisHealthIndicator extends AbstractHealthIndicator {
040
041        static final String VERSION = "version";
042
043        static final String REDIS_VERSION = "redis_version";
044
045        private final RedisConnectionFactory redisConnectionFactory;
046
047        public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
048                super("Redis health check failed");
049                Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
050                this.redisConnectionFactory = connectionFactory;
051        }
052
053        @Override
054        protected void doHealthCheck(Health.Builder builder) throws Exception {
055                RedisConnection connection = RedisConnectionUtils
056                                .getConnection(this.redisConnectionFactory);
057                try {
058                        if (connection instanceof RedisClusterConnection) {
059                                ClusterInfo clusterInfo = ((RedisClusterConnection) connection)
060                                                .clusterGetClusterInfo();
061                                builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
062                                                .withDetail("slots_up", clusterInfo.getSlotsOk())
063                                                .withDetail("slots_fail", clusterInfo.getSlotsFail());
064                        }
065                        else {
066                                Properties info = connection.info();
067                                builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION));
068                        }
069                }
070                finally {
071                        RedisConnectionUtils.releaseConnection(connection,
072                                        this.redisConnectionFactory);
073                }
074        }
075
076}