扩展GitInfoContributor以添加属性吗?

时间:2018-10-18 19:30:13

标签: spring-boot spring-boot-actuator

在我发现Spring Boot的Info Actuator几乎拥有我要发布的所有内容之前,我做了一些元终结点以确保我可以访问构建和Git信息,这些信息在尝试验证诸如以下内容时会有所帮助:

  • “是否部署了正确的版本?”
  • “谁制造的?”
  • “它是何时建造的?”
  • “这是基于哪个git提交的?”

这样做之后,我确实找到了Info执行器,它几乎为我回答了所有这些问题,但是我想从Git信息中添加一些内容-主要是提交消息和脏标志。

如果我使用以下命令打开完整的git元数据,则会查看输出:

management.info.git.mode=full

但是...这增加了更多的信息,其中大多数我都不在乎,因此超出了我真正想要的。

我想做的就是使用GitInfoContributor并将其扩展/替换,但是我不确定如何做到这一点。添加自己的贡献者很容易,但是如果我添加自己的贡献者并调用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”下的信息合并)一起添加,或者通过某种方式扩展/替换现有的GitInfoContributor?

1 个答案:

答案 0 :(得分:3)

在“ git”部分下添加新元素的最简单方法是扩展GitInfoContributor

kotlin:

sub

java:

x <- c(" Sao Paulo - Paulista - SP",
       "Minas Gerais - Mineiro - MG",
       "Rio de Janeiro - Carioca -RJ")

sub("^.*-\\s+(.*?)\\s+-.*$", "\\1", x)
[1] "Paulista" "Mineiro"  "Carioca"

此代码将在默认git信息后添加脏部分,例如^.*-\\s+ from the start, consume everything up to and including the first dash (.*?) then match and capture everything up until the second dash \\s+-.*$ consume everything after and including the second dash

如果您不想生成默认的git info部分,只需删除@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) } } 调用即可。