扩展 GitInfoContributor 以添加属性?
Extending GitInfoContributor to add properties?
在我发现 Spring Boot 的 Info Actuator 几乎包含我想发布的所有内容之前,我制作了一些元端点以确保我可以访问构建和 Git 信息,这些信息在尝试时会有所帮助验证以下内容:
- "Is the right version deployed?"
- "Who built this?"
- "When was it built?"
- "Which git commit is this based on?"
在那之后,我确实找到了 Info 执行器,它几乎为我回答了所有这些问题,但 Git 信息中有一些我想了解的内容添加——主要是提交消息和脏标志。
如果我打开完整的 git 元数据,我查看了输出:
management.info.git.mode=full
但是...这增加了很多信息,其中大部分我都不关心,所以它比我真正想要的要多。
我想做的是获取 GitInfoContributor 和 extend/replace 它,但我不太确定该怎么做。添加我自己的贡献者很容易,但是如果我添加我自己的贡献者并调用 builder.withDetails("git"),就像这样:
package ca.cpp.api.submitapi.config
import org.springframework.boot.actuate.info.Info
import org.springframework.boot.actuate.info.InfoContributor
import org.springframework.boot.info.GitProperties
import org.springframework.stereotype.Component
@Component
class CustomGitInfoContributor(private val properties: GitProperties): InfoContributor {
override fun contribute(builder: Info.Builder?) {
builder?.withDetail("git",mapOf("dirty" to properties.get("dirty"))
}
}
这取代了整套 git 属性,与此同时,我认为核心 GitInfoContributor 仍然存在,仍然提供我正在丢弃的信息。
是否有一种合理的方法来仅添加我想要的元素,或者使用我自己的贡献者可以将其信息与 "git" 下已有的信息合并,或者通过某种方式 extending/replacing 现有的 Git信息贡献者?
在 "git" 部分下添加新元素的最简单方法是扩展 GitInfoContributor
科特林:
@Component
class CustomGitInfoContributor @Autowired
constructor(properties: GitProperties) : GitInfoContributor(properties) {
override fun contribute(builder: Info.Builder) {
val map = generateContent()
map["dirty"] = properties.get("dirty")
builder.withDetail("git", map)
}
}
java:
@Component
public class CustomGitInfoContributor extends GitInfoContributor {
@Autowired
public CustomGitInfoContributor(GitProperties properties) {
super(properties);
}
@Override
public void contribute(Info.Builder builder) {
Map<String, Object> map = generateContent();
map.put("dirty", getProperties().get("dirty"));
builder.withDetail("git", map);
}
}
此代码将在默认 git 信息后添加脏部分,例如{"git":{"commit":{"time":"2018-11-03T15:22:51Z","id":"caa2ef0"},"branch":"master","dirty":"true"}}
如果您不想生成默认的 git 信息部分,请简单删除 generateContent()
调用。
在我发现 Spring Boot 的 Info Actuator 几乎包含我想发布的所有内容之前,我制作了一些元端点以确保我可以访问构建和 Git 信息,这些信息在尝试时会有所帮助验证以下内容:
- "Is the right version deployed?"
- "Who built this?"
- "When was it built?"
- "Which git commit is this based on?"
在那之后,我确实找到了 Info 执行器,它几乎为我回答了所有这些问题,但 Git 信息中有一些我想了解的内容添加——主要是提交消息和脏标志。
如果我打开完整的 git 元数据,我查看了输出:
management.info.git.mode=full
但是...这增加了很多信息,其中大部分我都不关心,所以它比我真正想要的要多。
我想做的是获取 GitInfoContributor 和 extend/replace 它,但我不太确定该怎么做。添加我自己的贡献者很容易,但是如果我添加我自己的贡献者并调用 builder.withDetails("git"),就像这样:
package ca.cpp.api.submitapi.config
import org.springframework.boot.actuate.info.Info
import org.springframework.boot.actuate.info.InfoContributor
import org.springframework.boot.info.GitProperties
import org.springframework.stereotype.Component
@Component
class CustomGitInfoContributor(private val properties: GitProperties): InfoContributor {
override fun contribute(builder: Info.Builder?) {
builder?.withDetail("git",mapOf("dirty" to properties.get("dirty"))
}
}
这取代了整套 git 属性,与此同时,我认为核心 GitInfoContributor 仍然存在,仍然提供我正在丢弃的信息。
是否有一种合理的方法来仅添加我想要的元素,或者使用我自己的贡献者可以将其信息与 "git" 下已有的信息合并,或者通过某种方式 extending/replacing 现有的 Git信息贡献者?
在 "git" 部分下添加新元素的最简单方法是扩展 GitInfoContributor
科特林:
@Component
class CustomGitInfoContributor @Autowired
constructor(properties: GitProperties) : GitInfoContributor(properties) {
override fun contribute(builder: Info.Builder) {
val map = generateContent()
map["dirty"] = properties.get("dirty")
builder.withDetail("git", map)
}
}
java:
@Component
public class CustomGitInfoContributor extends GitInfoContributor {
@Autowired
public CustomGitInfoContributor(GitProperties properties) {
super(properties);
}
@Override
public void contribute(Info.Builder builder) {
Map<String, Object> map = generateContent();
map.put("dirty", getProperties().get("dirty"));
builder.withDetail("git", map);
}
}
此代码将在默认 git 信息后添加脏部分,例如{"git":{"commit":{"time":"2018-11-03T15:22:51Z","id":"caa2ef0"},"branch":"master","dirty":"true"}}
如果您不想生成默认的 git 信息部分,请简单删除 generateContent()
调用。