我是千分尺和普罗米修斯的新手,我正在尝试构建我的第一个hello-world
应用程序,使用千分尺和普罗米修斯作为监控后端进行监控。但我看不到我的应用程序(Counter
s 和Timer
s)出现在 prometheus 端点上的指标。
我正在关注本教程的普罗米修斯。我也跟着这个视频开始使用千分尺。
我从这个链接下载了 prometheus ,解压了它,然后运行 prometheus 以使用以下命令进行抓取:./prometheus --config.file=prometheus.yml
. 我在此配置文件中将目标设置为targets: ['localhost:9090']
然后我运行了我的 Main 类,如下所示:
import cern.jet.random.Normal;
import cern.jet.random.engine.MersenneTwister64;
import cern.jet.random.engine.RandomEngine;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
import io.micrometer.jmx.JmxMeterRegistry;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws InterruptedException {
CompositeMeterRegistry compositeMeterRegistry = new CompositeMeterRegistry();
LoggingMeterRegistry loggingMeterRegistry = SampleMeterRegistries.loggingMeterRegistry();
JmxMeterRegistry jmxMeterRegistry = SampleMeterRegistries.jmxMeterRegistry();
// AtlasMeterRegistry atlasMeterRegistry = SampleMeterRegistries.atlasMeterRegistry();
PrometheusMeterRegistry prometheusMeterRegistry = SampleMeterRegistries.prometheus();
compositeMeterRegistry.add(loggingMeterRegistry);
compositeMeterRegistry.add(jmxMeterRegistry);
// compositeMeterRegistry.add(atlasMeterRegistry);
compositeMeterRegistry.add(prometheusMeterRegistry);
AtomicInteger latencyForThisSecond = new AtomicInteger(0);
Gauge gauge = Gauge.builder("my.guage", latencyForThisSecond, n -> n.get())
.register(compositeMeterRegistry);
Counter counter = Counter
.builder("my.counter")
.description("some description")
.tags("dev", "performance")
.register(compositeMeterRegistry);
Timer timer = Timer.builder("timer")
.publishPercentileHistogram()
.sla(Duration.ofMillis(270))
.register(compositeMeterRegistry);
// colt/colt/1.2.0 is to be added for this.
RandomEngine randomEngine = new MersenneTwister64(0);
Normal incomingRequests = new Normal(0, 1, randomEngine);
Normal duration = new Normal(250, 50, randomEngine);
latencyForThisSecond.set(duration.nextInt());
// For Flux you require io.projectreactor/reactor-core/3.2.3.RELEASE
Flux.interval(Duration.ofSeconds(1))
.doOnEach(d -> {
if (incomingRequests.nextDouble() + 0.4 > 0) {
timer.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
}
}).blockLast();
}
}
当我运行时./prometheus --config.file=prometheus.yml
,我可以访问端点http://localhost:9090/metrics
,也可以访问http://localhost:9090/graph
。但是当我尝试对其执行查询http://localhost:9090/graph
sum(timer_duration_seconds_sum) / sum(timer_duration_seconds_count)
时说no datapoints found
。
在我看来,我缺少一些明显的东西(因为我是这两个主题的初学者)。
有人可以指出我缺少什么吗?
我找不到(在我的 Main 类中的哪个位置)我必须配置 URI 才能为 prometheus 发布。即使我要发布到http://localhost:9090
(可能默认隐藏在千分尺的某个地方),我也找不到它。