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

时间:2019-02-06 10:28:38

标签: java spring spring-boot influxdb spring-micrometer

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

我对Spring,Spring Boot和Micrometer非常满意。是我误解了通用标签的文档和配置,还是计数器或InfluxDb通常不支持它们?

千分尺的版本:compile group: 'io.micrometer', name: 'micrometer-registry-influx', version: '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,它显示development

有了/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服务器属性的东西,它们都可以很好地初始化。

1 个答案:

答案 0 :(得分:0)

我使用下面的代码向发送到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);
    }
}