Spring Boot应用中暴露字符串型静态应用信息(如版本号)应选用哪种Micrometer仪表?
Great question! Since you're dealing with a static string value like an app version, none of the Gauge/Timer/DistributionSummary components are the right fit here—those are designed for tracking numerical metrics that change over time.
Instead, you have two clean, idiomatic options depending on your needs:
Option 1: Add the version as a global common tag (recommended for cross-filtering)
If you want every metric exposed by your app to include the version as a label (super useful for filtering across versions in Grafana), you can register it as a common tag on your MeterRegistry:
import io.micrometer.core.instrument.MeterRegistry; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class MetricsConfig { public MetricsConfig(MeterRegistry registry, @Value("${spring.application.version}") String appVersion) { registry.config().commonTags("version", appVersion); } }
This will add a version="5.6.3" label to every metric (e.g., http_server_requests_seconds_count{version="5.6.3", ...}), making it trivial to slice and dice metrics by app version in Grafana.
Option 2: Create a dedicated info metric (for standalone version display)
If you just want a standalone metric that explicitly reports the app version (without attaching it to every other metric), use Micrometer's Info meter. This aligns with Prometheus best practices for static metadata, generating a metric like application_info{version="5.6.3"} 1:
import io.micrometer.core.instrument.Info; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class MetricsConfig { public MetricsConfig(MeterRegistry registry, @Value("${spring.application.version}") String appVersion) { Info.builder("application") .tag("version", appVersion) .register(registry); } }
In Grafana, you can query this metric directly and display the version label value to show your app's version.
Why not use a Gauge?
Gauges are intended for tracking numerical values that fluctuate (like memory usage or queue size). While you could hack a Gauge with a static value of 1 and the version as a tag, using common tags or the Info meter is far more semantically correct and follows Micrometer/Prometheus conventions.
内容的提问来源于stack exchange,提问作者KumByYa

