Thomas Sundberg
Thomas Sundberg

Reputation: 4343

Metric for version number

Is it possible to add some metrics to a Java system that will return the version number as a string for the application that is monitored?

I am aiming for a dashboard where each pod, running a Java application inside a Docker container, in a Kubernetes cluster is monitored and the current version of each Java application is viewed.

If it isn't possible, do you have an idea on how to get that information from the Java application and make it available in a Grafana dashboard?

Upvotes: 2

Views: 2448

Answers (2)

Nexonus
Nexonus

Reputation: 814

Expanding on the idea of @Oliver I'm adding a sample java class which exposes the currently used application version, git branch and git commit id to the Prometheus metrics endpoint after the application is started/ready.

This assumes that you have a spring boot app with prometheus metrics enabled as an actuator endpoint.

@Component
@RequiredArgsConstructor
public class PrometheusCustomMetricsService implements ApplicationListener<ApplicationReadyEvent> {

    private static final int FIXED_VALUE = 1;
    @Value("${info.project.version}")
    private String applicationVersion;

    private final MeterRegistry meterRegistry;
    private final GitProperties gitProperties;

    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        registerApplicationInfoGauge();
    }

    private void registerApplicationInfoGauge() {
        Tag versionTag = new ImmutableTag("application_version", applicationVersion);
        Tag branchTag = new ImmutableTag("branch", gitProperties.getBranch());
        Tag commitIdTag = new ImmutableTag("commit_id", gitProperties.getShortCommitId());

        meterRegistry.gauge("application_info",
                            List.of(versionTag, branchTag, commitIdTag),
                            new AtomicInteger(FIXED_VALUE));
    }
}

Your custom metric should show up something like this in the prometheus endpoint:

application_info{application_version="1.2.3",branch="SOME-BRANCH-123",commit_id="123456"} NaN

I wouldn't worry about the value of the application_info gauge being NaN as we don't need the value and only use it as a way to send over the tags.

Upvotes: 2

Oliver
Oliver

Reputation: 13031

In your application, you can make available a Gauge metric that uses labels to export e.g. a version number or commit/build hash and then set the value of the gauge to 1.

For example, this is how the redis_exporter exports information about a redis instance:

# HELP redis_instance_info Information about the Redis instance
# TYPE redis_instance_info gauge
redis_instance_info{addr="redis://localhost:6379",os="Linux 4.4.0-62-generic x86_64",redis_build_id="687a2a319020fa42",redis_mode="standalone",redis_version="3.0.6",role="master"} 1

You can see the version and a couple of other attributes exported as labels of the metric redis_instance_info.

Upvotes: 2

Related Questions