forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[monitor] feature: support redis monitor protocol (apache#142)
* [manager]feature: refactor DispatchAlarm * [alerter]bugfix nextEvalInterval npe * [monitor] 支持Redis监控类型 * [monitor] Redis监控类型参数校验 Co-authored-by: Musk.Chen <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
1 parent
58f14d9
commit 6b02af0
Showing
8 changed files
with
692 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
collector/src/main/java/com/usthe/collector/collect/redis/RedisSingleCollectImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.usthe.collector.collect.redis; | ||
|
||
import com.usthe.collector.collect.AbstractCollect; | ||
import com.usthe.collector.collect.common.cache.CommonCache; | ||
import com.usthe.common.entity.job.Metrics; | ||
import com.usthe.common.entity.job.protocol.RedisProtocol; | ||
import com.usthe.common.entity.message.CollectRep; | ||
import com.usthe.common.util.CommonConstants; | ||
import io.lettuce.core.RedisClient; | ||
import io.lettuce.core.api.StatefulRedisConnection; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Redis 单机指标收集器 | ||
* | ||
* @author <a href="mailto:[email protected]">Musk.Chen</a> | ||
* @version 1.0 | ||
* Created by Musk.Chen on 2022/5/17 | ||
*/ | ||
@Slf4j | ||
public class RedisSingleCollectImpl extends AbstractCollect { | ||
|
||
public static RedisSingleCollectImpl getInstance() { | ||
return RedisSingleCollectImpl.Singleton.INSTANCE; | ||
} | ||
|
||
@Override | ||
public void collect(CollectRep.MetricsData.Builder builder, long appId, String app, Metrics metrics) { | ||
preCheck(metrics); | ||
RedisClient redisClient = buildClient(metrics.getRedis()); | ||
|
||
StatefulRedisConnection<String, String> connection = redisClient.connect(); | ||
|
||
String info = connection.sync().info(); | ||
Map<String, String> valueMap = parseInfo(info); | ||
if (log.isDebugEnabled()) { | ||
log.debug("[RedisSingleCollectImpl] fetch redis info"); | ||
valueMap.forEach((k, v) -> log.debug("{} : {}", k, v)); | ||
} | ||
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); | ||
metrics.getAliasFields().forEach(it -> { | ||
if (valueMap.containsKey(it)) { | ||
String fieldValue = valueMap.get(it); | ||
if (fieldValue == null) { | ||
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); | ||
} else { | ||
valueRowBuilder.addColumns(fieldValue); | ||
} | ||
} else { | ||
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); | ||
} | ||
}); | ||
builder.addValues(valueRowBuilder.build()); | ||
|
||
connection.closeAsync(); | ||
} | ||
|
||
/** | ||
* pre check params | ||
*/ | ||
private void preCheck(Metrics metrics) { | ||
if (metrics == null || metrics.getRedis() == null) { | ||
throw new IllegalArgumentException("Redis collect must has redis params"); | ||
} | ||
RedisProtocol redisProtocol = metrics.getRedis(); | ||
Assert.hasText(redisProtocol.getHost(), "Redis Protocol host is required."); | ||
Assert.hasText(redisProtocol.getPort(), "Redis Protocol port is required."); | ||
} | ||
|
||
/** | ||
* build single redis client | ||
* | ||
* @param redisProtocol redis protocol config | ||
* @return redis single client | ||
*/ | ||
private RedisClient buildClient(RedisProtocol redisProtocol) { | ||
String uri = String.format("redis://%s:%d", redisProtocol.getHost(), Integer.parseInt(redisProtocol.getPort())); | ||
CommonCache commonCache = CommonCache.getInstance(); | ||
return commonCache.getCache(uri, true) | ||
.map(r -> (RedisClient) r) | ||
.orElseGet(() -> { | ||
// create new redis client | ||
RedisClient redisClient = RedisClient.create(uri); | ||
commonCache.addCache(uri, redisClient); | ||
return redisClient; | ||
}); | ||
} | ||
|
||
/** | ||
* parse redis info | ||
* | ||
* @param info redis info | ||
* @return parsed redis info | ||
*/ | ||
private Map<String, String> parseInfo(String info) { | ||
String[] lines = info.split("\n"); | ||
Map<String, String> result = new HashMap<>(); | ||
Arrays.stream(lines) | ||
.filter(it -> StringUtils.hasText(it) && !it.startsWith("#") && it.contains(":")) | ||
.map(this::removeCR) | ||
.map(r -> r.split(":")) | ||
.forEach(it -> { | ||
if (it.length > 1) { | ||
result.put(it[0], it[1]); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
private String removeCR(String value) { | ||
return value.replace("\r", ""); | ||
} | ||
|
||
private static class Singleton { | ||
private static final RedisSingleCollectImpl INSTANCE = new RedisSingleCollectImpl(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
common/src/main/java/com/usthe/common/entity/job/protocol/RedisProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.usthe.common.entity.job.protocol; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Musk.Chen</a> | ||
* @version 1.0 | ||
* Created by Musk.Chen on 2022/5/17 | ||
*/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class RedisProtocol { | ||
|
||
/** | ||
* 对端主机ip或域名 | ||
*/ | ||
private String host; | ||
|
||
/** | ||
* 端口号 | ||
*/ | ||
private String port; | ||
|
||
/** | ||
* Redis用户名(可选) | ||
*/ | ||
private String username; | ||
|
||
/** | ||
* Redis密码(可选) | ||
*/ | ||
private String password; | ||
|
||
/** | ||
* 超时时间 | ||
*/ | ||
private String timeout; | ||
|
||
} |
Oops, something went wrong.