spring cloud統合prometheusソース分析
11152 ワード
説明
本論文ではプロザス公式のspring cloud統合client mavenに対する依存性は以下の通りである。
まずmetricsから出力します。http://localhost:8080/prometheus プロジェクトに注釈@EnblePrometheusEnd pointを使用する場合、プログラムは設定を有効にして、MvcEndpointを登録することにより、アクセス経路が「prometheus」であるhttp要求サービスを暴露します。キーコードのセグメント:注
Metricsコレクション
コレクションを作成する時、コレクションはCollecter Registryに登録されます。コレクションCounterを作成して登録します。
本論文ではプロザス公式のspring cloud統合client mavenに対する依存性は以下の通りである。
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclient_spring_bootartifactId>
<version>0.0.26version>
dependency>
Metrics出力まずmetricsから出力します。http://localhost:8080/prometheus プロジェクトに注釈@EnblePrometheusEnd pointを使用する場合、プログラムは設定を有効にして、MvcEndpointを登録することにより、アクセス経路が「prometheus」であるhttp要求サービスを暴露します。キーコードのセグメント:注
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({PrometheusEndpointConfiguration.class})
public @interface EnablePrometheusEndpoint {
}
プロファイルを配置します。@Configuration
class PrometheusEndpointConfiguration {
PrometheusEndpointConfiguration() {
}
@Bean
public PrometheusEndpoint prometheusEndpoint() {
return new PrometheusEndpoint(CollectorRegistry.defaultRegistry);
}
@Bean
@ConditionalOnBean({PrometheusEndpoint.class})
@ConditionalOnEnabledEndpoint("prometheus")
public PrometheusMvcEndpoint prometheusEndpointFix(PrometheusEndpoint prometheusEndpoint) {
return new PrometheusMvcEndpoint(prometheusEndpoint);
}
}
PromtheusEndpointを作成します。@ConfigurationProperties("endpoints.prometheus")
class PrometheusEndpoint extends AbstractEndpoint<String> {
private final CollectorRegistry collectorRegistry;
PrometheusEndpoint(CollectorRegistry collectorRegistry) {
super("prometheus");
this.collectorRegistry = collectorRegistry;
}
public String invoke() {
return this.writeRegistry(Collections.emptySet());
}
public String writeRegistry(Set metricsToInclude) {
try {
StringWriter e = new StringWriter();
TextFormat.write004(e, this.collectorRegistry.filteredMetricFamilySamples(metricsToInclude));
return e.toString();
} catch (IOException var3) {
throw new RuntimeException("Writing metrics failed", var3);
}
}
}
最終的にはwriteRegistryからmetrics情報を出力します。中に入って調べたら、実はコレクション登録センターの中のすべての収集器の情報をEnterationに記述して、Text FormatからEnumerationをtext形式で出力します。Metricsコレクション
コレクションを作成する時、コレクションはCollecter Registryに登録されます。コレクションCounterを作成して登録します。
public static final Counter requestTotal = Counter.build().name("NAME").labelNames("label1", "label2").help
("total request counter of api").register();
登録Counter:public C register() {
return this.register(CollectorRegistry.defaultRegistry);
}
public C register(CollectorRegistry registry) {
SimpleCollector sc = this.create();
registry.register(sc);
return sc;
}
コレクション自体はカウント方法と説明方法を提供します。カウント方法はデータの増加を記録します。説明方法はmetricsを出力する際に記録データを記述しやすいmetrics情報に変換します。Counterソース:public class Counter extends SimpleCollector implements Describable {
Counter(Counter.Builder b) {
super(b);
}
public static Counter.Builder build(String name, String help) {
return (Counter.Builder)((Counter.Builder)(new Counter.Builder()).name(name)).help(help);
}
public static Counter.Builder build() {
return new Counter.Builder();
}
protected Counter.Child newChild() {
return new Counter.Child();
}
// 1
public void inc() {
this.inc(1.0D);
}
public void inc(double amt) {
((Counter.Child)this.noLabelsChild).inc(amt);
}
public double get() {
return ((Counter.Child)this.noLabelsChild).get();
}
// Metric
public List collect() {
ArrayList samples = new ArrayList(this.children.size());
Iterator var2 = this.children.entrySet().iterator();
while(var2.hasNext()) {
Entry c = (Entry)var2.next();
samples.add(new Sample(this.fullname, this.labelNames, (List)c.getKey(), ((Counter.Child)c.getValue()).get()));
}
return this.familySamplesList(Type.COUNTER, samples);
}
public List describe() {
return Collections.singletonList(new CounterMetricFamily(this.fullname, this.help, this.labelNames));
}
public static class Child {
private final DoubleAdder value = new DoubleAdder();
public Child() {
}
public void inc() {
this.inc(1.0D);
}
public void inc(double amt) {
if(amt < 0.0D) {
throw new IllegalArgumentException("Amount to increment must be non-negative.");
} else {
this.value.add(amt);
}
}
public double get() {
return this.value.sum();
}
}
public static class Builder extends io.prometheus.client.SimpleCollector.Builder {
public Builder() {
}
public Counter create() {
return new Counter(this);
}
}
}