-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
build/ | ||
!micrometer-core/src/main/java/io/micrometer/core/instrument/binder/build | ||
.gradle/ | ||
out/ | ||
|
||
|
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,50 @@ | ||
/* | ||
* 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.binder.MeterBinder; | ||
|
||
import java.time.Instant; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class BuildInfoMetrics implements MeterBinder { | ||
|
||
private final BuildInfo buildInfo; | ||
|
||
public BuildInfoMetrics(BuildInfo buildInfo) { | ||
requireNonNull(buildInfo, "buildInfo"); | ||
this.buildInfo = buildInfo; | ||
} | ||
|
||
@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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial thoughts on the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 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.. |
||
.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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been added.