gpt4 book ai didi

java - 使用 Micrometer 设置 InfluxDB 指标的常见标签

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:26 26 4
gpt4 key购买 nike

我有一个单独的模块,其中包含公共(public)类。其中两个类正在为我们的服务指标设置通用标签。当我启动服务时,指标(计数器)将发送到 InfluxDb,但在 MetricsConfiguration.java 中设置的通用标签不会被发送。我也查询了 InfluxDb 中的指标,并且像 serviceenv 这样的标签不存在,所有其他直接使用计数器设置的标签都存在。

我对 Spring、Spring Boot 和 Micrometer 非常熟悉。要么是我误解了常见标签的文档和配置,要么是计数器或 InfluxDb 一般不支持它们?

Micrometer版本:编译组:'io.micrometer',名称:'micrometer-registry-influx',版本:'1.0.5'

检查 /configprops 端点我可以看到:

...
"metrics-com.example.io.metrics.MetricsProperties": {
"prefix": "metrics",
"properties": {
"step": 0,
"histogramExpiry": 0,
"serviceTag": "service"
}
}
...

/env/spring.application.name给出:

...
"property": {
"source": "applicationConfig: [classpath:/bootstrap.yml]",
"value": "example-service"
},
...

即使在配置属性中设置它,似乎也没有任何效果:

environment: development

...

management:
metrics:
tags:
env: "${environment}"
...

检查端点/env/management.metrics.tags.env,它显示开发

使用 /beans 端点,我可以看到正在加载的 bean:

...
"metricsCommonTags": {
"aliases": [],
"scope": "singleton",
"type": "com.example.io.metrics.MetricsConfiguration$$Lambda$325/509806761",
"resource": "com.example.io.metrics.MetricsConfiguration",
"dependencies": []
},
"metricsConfiguration": {
"aliases": [],
"scope": "singleton",
"type": "com.example.io.metrics.MetricsConfiguration$$EnhancerBySpringCGLIB$$f1b7dc1a",
"resource": null,
"dependencies": [
"metrics-com.example.io.metrics.MetricsProperties",
"org.springframework.context.annotation.AnnotationConfigApplicationContext@2ed3b1f5"
]
},
...
"metrics-com.example.io.metrics.MetricsProperties": {
"aliases": [],
"scope": "singleton",
"type": "com.example.io.metrics.MetricsProperties",
"resource": null,
"dependencies": []
},
...

所以我的问题是我错过了什么吗?

MetricsProperties.java

package com.example.io.metrics;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "metrics")
public class MetricsProperties {
private int histogramExpiry;
private int step;
private String serviceTag;

public int getHistogramExpiry() {
return histogramExpiry;
}

public void setHistogramExpiry(int histogramExpiry) {
this.histogramExpiry = histogramExpiry;
}

public int getStep() {
return step;
}

public void setStep(int step) {
this.step = step;
}

public String getServiceTag() {
return serviceTag;
}

public void setServiceTag(String serviceTag) {
this.serviceTag = serviceTag;
}
}

MetricsConfiguration.java

package com.example.io.metrics;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MetricsProperties.class)
public class MetricsConfiguration {

private final MetricsProperties metricsProperties;

private final ApplicationContext applicationContext;

@Autowired
public MetricsConfiguration(MetricsProperties metricsProperties, ApplicationContext applicationContext) {
this.metricsProperties = metricsProperties;
this.applicationContext = applicationContext;
}

@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry
.config()
.commonTags(metricsProperties.getServiceTag(),
applicationContext.getApplicationName());
}
}

编辑:

所以我们还有 /META-INF/spring.factories 填充:

org.springframework.cloud.bootstrap.BootstrapConfiguration = com.example.io.autoconfigure.CustomConfigServiceBootstrapConfiguration\
,com.example.io.metrics.MetricsConfiguration

CustomConfigServiceBootstrapConfiguration 在本地被跳过,因为存在 @ConditionalOnProperty(name = "autoconfigure.client.resolvePrivateServerAddress", matchIfMissing = true) 条件,并且在 bootstrap.yml 中有

autoconfigure:
client:
resolvePrivateServerAddress: false

我还做了一些研究,发现在初始化上下文之前可能会调用 metricsCommonTags bean,但我对此不确定?奇怪的是,当我在 metricsCommonTags 中放置断点时,尚未从 Cloud Config 服务器设置属性。

这里还有 Application.java 的精简版本:

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableConfigurationProperties(LoginProperties.class)
@EnableJpaRepositories
@EnableMongoRepositories
@EnableSwagger2
@Configuration
@EnableWebMvc
public class Application implements WebMvcConfigurer {

private final MeterRegistry meterRegistry;

@Autowired
public Application(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

对于我们使用 Cloud Config 服务器属性的所有其他内容,它们都可以正常初始化。

最佳答案

我使用下面的代码将通用标签添加到发送到 InfluxDB 的所有指标

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import io.micrometer.core.aop.TimedAspect;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;

@Configuration
public class MetricsConfig {

@Value("${spring.application.name}")
String applicationName;

@Autowired
private Environment environment;

@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}

@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
String hostname = "";
try {
hostname = InetAddress.getLocalHost().getHostName().toString();
} catch (UnknownHostException e) {
hostname = "unknown";
}
List<Tag> tags = new LinkedList<>();
tags.add(Tag.of("hostname", hostname));
tags.add(Tag.of("applicationName", applicationName));
String activeProfiles = Arrays.stream(this.environment.getActiveProfiles()).reduce("",String::concat);
tags.add(Tag.of("environment", activeProfiles));
return registry -> registry.config().commonTags(tags);
}
}

关于java - 使用 Micrometer 设置 InfluxDB 指标的常见标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54551376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com