Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Git commit and build info metrics #4286

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
!micrometer-core/src/*/*/io/micrometer/core/instrument/binder/build
.gradle/
out/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
public interface MeterBinder {

String DEFAULT_TAG_VALUE = "unknown";

void bindTo(@NonNull MeterRegistry registry);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.micrometer.core.instrument.binder.build;

import java.time.Instant;
import java.util.Optional;

public class BuildInfo {

private final String group;

private final String artifact;

private final String name;

private final String version;

private final Instant timestamp;

private BuildInfo(String group, String artifact, String name, String version, Instant timestamp) {
this.group = group;
this.artifact = artifact;
this.name = name;
this.version = version;
this.timestamp = timestamp;
}

public static BuildInfo.Builder builder() {
return new BuildInfo.Builder();
}

public Optional<String> getGroup() {
return Optional.ofNullable(group);
}

public Optional<String> getArtifact() {
return Optional.ofNullable(artifact);
}

public Optional<String> getName() {
return Optional.ofNullable(name);
}

public Optional<String> getVersion() {
return Optional.ofNullable(version);
}

public Optional<Instant> getTimestamp() {
return Optional.ofNullable(timestamp);
}

public static class Builder {

private String group;

private String artifact;

private String name;

private String version;

private Instant timestamp;

private Builder() {
}

public BuildInfo build() {
return new BuildInfo(group, artifact, name, version, timestamp);
}

public Builder group(String group) {
this.group = group;
return this;
}

public Builder artifact(String artifact) {
this.artifact = artifact;
return this;
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder version(String version) {
this.version = version;
return this;
}

public Builder timestamp(Instant timestamp) {
this.timestamp = timestamp;
return this;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.micrometer.core.instrument.binder.build;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;

import java.time.Instant;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;

public class BuildInfoMetrics implements MeterBinder {

private final BuildInfo buildInfo;

private final Iterable<Tag> tags;

public BuildInfoMetrics(BuildInfo buildInfo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be one more constructor to add user provided tags to metrics?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added.

this(buildInfo, emptyList());
}

public BuildInfoMetrics(BuildInfo buildInfo, Iterable<Tag> tags) {
requireNonNull(buildInfo, "buildInfo");
requireNonNull(tags, "tags");
this.buildInfo = buildInfo;
this.tags = tags;
}

@Override
public void bindTo(MeterRegistry registry) {
requireNonNull(registry, "registry");
Gauge.builder("build.info", () -> 1L)
.description("Build information")
.strongReference(true)
.tag("group", buildInfo.getGroup().orElse(DEFAULT_TAG_VALUE))
.tag("artifact", buildInfo.getArtifact().orElse(DEFAULT_TAG_VALUE))
.tag("name", buildInfo.getName().orElse(DEFAULT_TAG_VALUE))
.tag("version", buildInfo.getVersion().orElse(DEFAULT_TAG_VALUE))
.tag("timestamp", buildInfo.getTimestamp().map(Instant::toString).orElse(DEFAULT_TAG_VALUE))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thoughts on the timestamp was whether it is a good candidate for metrics. But, I guess it should be fine since it will most probably be tied to versions and timestamp is more of an informative tag than anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is this really for tracking build info of dependencies? or the service itself?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be used for either the service and/or one or many dependencies. This implementation does not impose an opinion. Ultimately it was added to try and meet parity with the SpringBoot equivalent class.

One use case for a dependency would be to use this metric to assert that "all production services are using SpringBoot version 3.1.x or a version with a timestamp value from the last 3 months."

One could case for the service might be to use this metric in combination with something like "deployment.info" (which does not exist in Micrometer right now, but for the purposes of this discussion, lets say it captures at least a timestamp value of when the runtime was deployed). You could then use these two metrics together to understand how long it takes to get an application into production starting from the time the deployable artifact was created. This example is a bit more complicated and has it's flaws (such as rollbacks, rolling deployments, auto-scaling) but shared it as a potential idea.

Ultimately I agree with you, the timestamp value may end up being more informational than anything but I could imagine use cases for it to be useful and it meets SpringBoot feature parity..

.tags(tags)
.register(registry);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Meter binders for build info.
*/
package io.micrometer.core.instrument.binder.build;
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.micrometer.core.instrument.binder.git;

import java.time.Instant;
import java.util.Optional;

/**
* Information from a Git commit.
*/
public class GitCommitInfo {

private final String branch;

private final String commitId;

private final String commitIdShort;

private final Instant commitTime;

private GitCommitInfo(String branch, String commitId, String commitIdShort, Instant commitTime) {
this.branch = branch;
this.commitId = commitId;
this.commitIdShort = commitIdShort;
this.commitTime = commitTime;
}

public static GitCommitInfo.Builder builder() {
return new GitCommitInfo.Builder();
}

public Optional<String> getBranch() {
return Optional.ofNullable(branch);
}

public Optional<String> getCommitId() {
return Optional.ofNullable(commitId);
}

public Optional<String> getShortCommitId() {
return Optional.ofNullable(commitIdShort);
}

public Optional<Instant> getCommitTime() {
return Optional.ofNullable(commitTime);
}

public static class Builder {

private String branch;

private String commitId;

private String commitIdShort;

private Instant commitTime;

private Builder() {
}

public GitCommitInfo build() {
return new GitCommitInfo(branch, commitId, commitIdShort, commitTime);
}

public Builder branch(String branch) {
this.branch = branch;
return this;
}

public Builder commitId(String commitId) {
this.commitId = commitId;
return this;
}

public Builder commitIdShort(String commitIdShort) {
this.commitIdShort = commitIdShort;
return this;
}

public Builder commitTime(Instant commitTime) {
this.commitTime = commitTime;
return this;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.micrometer.core.instrument.binder.git;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;

import java.time.Instant;

import static java.util.Objects.requireNonNull;

public class GitCommitInfoMetrics implements MeterBinder {

private final GitCommitInfo gitCommitInfo;

public GitCommitInfoMetrics(GitCommitInfo gitCommitInfo) {
requireNonNull(gitCommitInfo, "gitCommitInfo");
this.gitCommitInfo = gitCommitInfo;
}

@Override
public void bindTo(MeterRegistry registry) {
requireNonNull(registry, "registry");
Gauge.builder("git.commit.info", () -> 1L)
.description("Git commit information")
.strongReference(true)
.tag("branch", gitCommitInfo.getBranch().orElse(DEFAULT_TAG_VALUE))
.tag("commit.id", gitCommitInfo.getCommitId().orElse(DEFAULT_TAG_VALUE))
.tag("commit.id.short", gitCommitInfo.getShortCommitId().orElse(DEFAULT_TAG_VALUE))
.tag("commit.timestamp", gitCommitInfo.getCommitTime().map(Instant::toString).orElse(DEFAULT_TAG_VALUE))
.register(registry);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Meter binders for git info.
*/
package io.micrometer.core.instrument.binder.git;