Commit e40a446b authored by 王亮's avatar 王亮

fix some bug of getting ip .

parent c25256f1
......@@ -2,8 +2,6 @@ package cn.quantgroup.boot.micrometer.register.kafka;
import static java.util.stream.Collectors.joining;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpoint;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.FunctionTimer;
......@@ -38,7 +36,8 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
private final String key;
private ApplicationContext applicationContext;
public KafkaMeterRegistry(KafkaConfig config, Clock clock, ApplicationContext applicationContext) {
public KafkaMeterRegistry(KafkaConfig config, Clock clock,
ApplicationContext applicationContext) {
super(config, clock);
this.config = config;
if (ObjectUtils.isEmpty(System.getProperty("NAMESPACE"))) {
......@@ -68,19 +67,14 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
protected void publish() {
for (List<Meter> batch : MeterPartition.partition(this, config.batchSize())) {
batch.stream()
.flatMap(m -> m.match(
gauge -> writeGauge(gauge.getId(), gauge.value()),
counter -> writeCounter(counter.getId(), counter.count()),
this::writeTimer,
this::writeSummary,
this::writeLongTaskTimer,
gauge -> writeGauge(gauge.getId(), gauge.value(getBaseTimeUnit())),
counter -> writeCounter(counter.getId(), counter.count()),
this::writeFunctionTimer,
this::writeMeter)).forEach(i -> {
kafkaProducer.send(new ProducerRecord<>(config.topic(), key,i));
});
batch.stream().flatMap(m -> m.match(gauge -> writeGauge(gauge.getId(), gauge.value()),
counter -> writeCounter(counter.getId(), counter.count()), this::writeTimer,
this::writeSummary, this::writeLongTaskTimer,
gauge -> writeGauge(gauge.getId(), gauge.value(getBaseTimeUnit())),
counter -> writeCounter(counter.getId(), counter.count()), this::writeFunctionTimer,
this::writeMeter)).forEach(i -> {
kafkaProducer.send(new ProducerRecord<>(config.topic(), key, i));
});
}
}
......@@ -97,19 +91,14 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private String influxLineProtocol(Meter.Id id, String metricType, Stream<Field> fields) {
String tags = getConventionTags(id).stream()
.filter(t -> StringUtils.isNotBlank(t.getValue()))
String tags = getConventionTags(id).stream().filter(t -> StringUtils.isNotBlank(t.getValue()))
.map(t -> "," + escapeField(t.getKey()) + "=" + escapeField(t.getValue()))
.collect(joining(""));
NacosDiscoveryEndpoint nacosDiscoveryEndpoint = (NacosDiscoveryEndpoint) this.applicationContext.getBean("nacosDiscoveryEndpoint");
String ip = ((NacosDiscoveryProperties) nacosDiscoveryEndpoint.nacosDiscovery()
.get("NacosDiscoveryProperties")).getIp();
String ip = applicationContext.getEnvironment().getProperty("spring.cloud.client.ip-address");
tags = tags + (",ip=" + ip);
return getConventionName(id)
+ tags + ",metric_type=" + metricType + " "
+ fields.map(Field::toString).collect(joining(","))
+ " " + clock.wallTime();
return getConventionName(id) + tags + ",metric_type=" + metricType + " " + fields.map(
Field::toString).collect(joining(",")) + " " + clock.wallTime();
}
static class Field {
......@@ -125,6 +114,7 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return escapeField(key) + "=" + DoubleFormat.decimalOrNan(value);
......@@ -139,32 +129,24 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private Stream<String> writeTimer(Timer timer) {
final Stream<Field> fields = Stream.of(
new Field("sum", timer.totalTime(getBaseTimeUnit())),
new Field("count", timer.count()),
new Field("mean", timer.mean(getBaseTimeUnit())),
new Field("upper", timer.max(getBaseTimeUnit()))
);
final Stream<Field> fields = Stream.of(new Field("sum", timer.totalTime(getBaseTimeUnit())),
new Field("count", timer.count()), new Field("mean", timer.mean(getBaseTimeUnit())),
new Field("upper", timer.max(getBaseTimeUnit())));
return Stream.of(influxLineProtocol(timer.getId(), "histogram", fields));
}
private Stream<String> writeSummary(DistributionSummary summary) {
final Stream<Field> fields = Stream.of(
new Field("sum", summary.totalAmount()),
new Field("count", summary.count()),
new Field("mean", summary.mean()),
new Field("upper", summary.max())
);
final Stream<Field> fields = Stream.of(new Field("sum", summary.totalAmount()),
new Field("count", summary.count()), new Field("mean", summary.mean()),
new Field("upper", summary.max()));
return Stream.of(influxLineProtocol(summary.getId(), "histogram", fields));
}
private Stream<String> writeLongTaskTimer(LongTaskTimer timer) {
Stream<Field> fields = Stream.of(
new Field("active_tasks", timer.activeTasks()),
new Field("duration", timer.duration(getBaseTimeUnit()))
);
Stream<Field> fields = Stream.of(new Field("active_tasks", timer.activeTasks()),
new Field("duration", timer.duration(getBaseTimeUnit())));
return Stream.of(influxLineProtocol(timer.getId(), "long_task_timer", fields));
}
......@@ -202,8 +184,8 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private static String escapeField(String names){
return names.replace(" ","\\ ").replace(",","\\,");
private static String escapeField(String names) {
return names.replace(" ", "\\ ").replace(",", "\\,");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment