2

我有一个包含公共类的单独模块。其中两个类正在为我们的服务的指标设置通用标签。当我启动服务时,指标(计数器)被发送到 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 服务器的属性的所有其他东西,它们都被初始化得很好。

4

1 回答 1

1

我使用下面的代码为发送到 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);
    }
}
于 2019-09-12T09:15:44.070 回答