简体   繁体   English

使用Micrometer为InfluxDB指标设置通用标签

[英]Setting common tags with Micrometer for InfluxDB metrics

I have a separate module which contains common classes. 我有一个单独的模块,其中包含通用类。 Two of the classes are setting up common tags for metrics for our services. 其中两个类别正在为我们的服务的指标设置通用标签。 When I start the service the metrics (counters) are sent to the InfluxDb, but the common tags, that are set in MetricsConfiguration.java are not being sent. 当我启动该服务时,指标(计数器)将发送到InfluxDb,但不会发送在MetricsConfiguration.java中设置的通用标签。 I have queried the metrics in the InfluxDb as well and tags like service and env are not there, all others that are set directly with counters are. 我也查询了InfluxDb中的指标,并且不存在诸如serviceenv类的标记,所有其他直接由计数器设置的标记都存在。

I am pretty fresh with Spring, Spring Boot and Micrometer. 我对Spring,Spring Boot和Micrometer非常满意。 Either I misunderstood the documentation and configuration of common tags or they are not supported within counters or InfluxDb in general? 我是否误解了通用标签的文档和配置,或者计数器或InfluxDb通常不支持它们?

Version of Micrometer: compile group: 'io.micrometer', name: 'micrometer-registry-influx', version: '1.0.5' 千分尺的版本: compile group: 'io.micrometer', name: 'micrometer-registry-influx', version: '1.0.5'

Checking the /configprops endpoint I can see: 检查/configprops端点,我可以看到:

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

and /env/spring.application.name gives: /env/spring.application.name给出:

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

Even setting this in config properties, does not seem to have any effect: 即使在配置属性中设置此选项,也似乎没有任何效果:

environment: development

...

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

Checking endpoint /env/management.metrics.tags.env it shows development . 检查端点/env/management.metrics.tags.env它显示development

With /beans endpoint, I can see beans being loaded: 使用/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": []
},
...

So my question is am I missing something? 所以我的问题是我缺少什么吗?

MetricsProperties.java 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 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());
    }
}

EDIT: 编辑:

So we also have /META-INF/spring.factories filled with: 因此,我们也将/META-INF/spring.factories填充为:

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

The CustomConfigServiceBootstrapConfiguration is getting skiped locally because there is @ConditionalOnProperty(name = "autoconfigure.client.resolvePrivateServerAddress", matchIfMissing = true) condition and in bootstrap.yml there is 由于存在@ConditionalOnProperty(name = "autoconfigure.client.resolvePrivateServerAddress", matchIfMissing = true)条件,并且在bootstrap.ymlCustomConfigServiceBootstrapConfiguration正在本地@ConditionalOnProperty(name = "autoconfigure.client.resolvePrivateServerAddress", matchIfMissing = true)

autoconfigure:
  client:
    resolvePrivateServerAddress: false

I did also some research and what I found out that metricsCommonTags bean might of been called before the context is initialised, but I am not sure about that? 我也做了一些研究,发现在上下文初始化之前可能会调用metricsCommonTags bean,但是我不确定吗? What is weird is that, when I put a breakpoint in the metricsCommonTags , the properties are not yet set from Cloud Config server. 奇怪的是,当我在metricsCommonTags设置断点时,尚未从Cloud Config服务器设置属性。

Here is also trimmed down version of Application.java : 这也是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);
    }
}

For all other stuff where we use properties from Cloud Config server they are being initialised just fine. 对于我们使用Cloud Config服务器的属性的所有其他内容,将它们初始化就很好了。

I used below code to add common tags to all metrics being sent to InfluxDB 我使用下面的代码向发送到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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM