HealthCheck: show enterprise commit (#75242)

This commit is contained in:
Ezequiel Victorero 2023-09-22 08:17:10 -03:00 committed by GitHub
parent 3529b7413d
commit 4cfc834c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 102 additions and 30 deletions

View File

@ -36,6 +36,29 @@ func TestHealthAPI_Version(t *testing.T) {
require.JSONEq(t, expectedBody, rec.Body.String()) require.JSONEq(t, expectedBody, rec.Body.String())
} }
func TestHealthAPI_VersionEnterprise(t *testing.T) {
m, _ := setupHealthAPITestEnvironment(t, func(cfg *setting.Cfg) {
cfg.BuildVersion = "7.4.0"
cfg.EnterpriseBuildCommit = "22206ab1be"
cfg.BuildCommit = "59906ab1bf"
})
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
rec := httptest.NewRecorder()
m.ServeHTTP(rec, req)
require.Equal(t, 200, rec.Code)
expectedBody := `
{
"database": "ok",
"enterpriseCommit": "22206ab1be",
"version": "7.4.0",
"commit": "59906ab1bf"
}
`
require.JSONEq(t, expectedBody, rec.Body.String())
}
func TestHealthAPI_AnonymousHideVersion(t *testing.T) { func TestHealthAPI_AnonymousHideVersion(t *testing.T) {
m, hs := setupHealthAPITestEnvironment(t) m, hs := setupHealthAPITestEnvironment(t)
hs.Cfg.AnonymousHideVersion = true hs.Cfg.AnonymousHideVersion = true

View File

@ -679,6 +679,9 @@ func (hs *HTTPServer) apiHealthHandler(ctx *web.Context) {
if !hs.Cfg.AnonymousHideVersion { if !hs.Cfg.AnonymousHideVersion {
data.Set("version", hs.Cfg.BuildVersion) data.Set("version", hs.Cfg.BuildVersion)
data.Set("commit", hs.Cfg.BuildCommit) data.Set("commit", hs.Cfg.BuildCommit)
if hs.Cfg.EnterpriseBuildCommit != "NA" && hs.Cfg.EnterpriseBuildCommit != "" {
data.Set("enterpriseCommit", hs.Cfg.EnterpriseBuildCommit)
}
} }
if !hs.databaseHealthy(ctx.Req.Context()) { if !hs.databaseHealthy(ctx.Req.Context()) {

View File

@ -222,15 +222,25 @@ func ldflags(opts BuildOpts) (string, error) {
commitSha = v commitSha = v
} }
var enterpriseCommitSha string
if opts.enterprise {
enterpriseCommitSha = getGitEnterpriseSha()
if v := os.Getenv("ENTERPRISE_COMMIT_SHA"); v != "" {
enterpriseCommitSha = v
}
}
buildBranch := getGitBranch() buildBranch := getGitBranch()
if v := os.Getenv("BUILD_BRANCH"); v != "" { if v := os.Getenv("BUILD_BRANCH"); v != "" {
buildBranch = v buildBranch = v
} }
var b bytes.Buffer var b bytes.Buffer
b.WriteString("-w") b.WriteString("-w")
b.WriteString(fmt.Sprintf(" -X main.version=%s", opts.version)) b.WriteString(fmt.Sprintf(" -X main.version=%s", opts.version))
b.WriteString(fmt.Sprintf(" -X main.commit=%s", commitSha)) b.WriteString(fmt.Sprintf(" -X main.commit=%s", commitSha))
if enterpriseCommitSha != "" {
b.WriteString(fmt.Sprintf(" -X main.enterpriseCommit=%s", enterpriseCommitSha))
}
b.WriteString(fmt.Sprintf(" -X main.buildstamp=%d", buildStamp)) b.WriteString(fmt.Sprintf(" -X main.buildstamp=%d", buildStamp))
b.WriteString(fmt.Sprintf(" -X main.buildBranch=%s", buildBranch)) b.WriteString(fmt.Sprintf(" -X main.buildBranch=%s", buildBranch))
if v := os.Getenv("LDFLAGS"); v != "" { if v := os.Getenv("LDFLAGS"); v != "" {

View File

@ -12,6 +12,7 @@ import (
type Revision struct { type Revision struct {
Timestamp int64 Timestamp int64
SHA256 string SHA256 string
EnterpriseCommit string
Branch string Branch string
} }
@ -42,6 +43,14 @@ func GrafanaRevision(ctx context.Context, grafanaDir string) (Revision, error) {
return Revision{}, err return Revision{}, err
} }
enterpriseCommit, err := executil.OutputAt(ctx, grafanaDir, "git", "-C", "../grafana-enterprise", "rev-parse", "--short", "HEAD")
if err != nil {
enterpriseCommit, err = executil.OutputAt(ctx, grafanaDir, "git", "-C", "..", "rev-parse", "--short", "HEAD")
if err != nil {
return Revision{}, err
}
}
branch, err := executil.OutputAt(ctx, grafanaDir, "git", "rev-parse", "--abbrev-ref", "HEAD") branch, err := executil.OutputAt(ctx, grafanaDir, "git", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil { if err != nil {
return Revision{}, err return Revision{}, err
@ -49,6 +58,7 @@ func GrafanaRevision(ctx context.Context, grafanaDir string) (Revision, error) {
return Revision{ return Revision{
SHA256: sha, SHA256: sha,
EnterpriseCommit: enterpriseCommit,
Branch: branch, Branch: branch,
Timestamp: stamp, Timestamp: stamp,
}, nil }, nil

View File

@ -15,3 +15,16 @@ func getGitSha() string {
} }
return string(v) return string(v)
} }
func getGitEnterpriseSha() string {
// supporting the old way of dev setup
v, err := runError("git", "-C", "../grafana-enterprise", "rev-parse", "--short", "HEAD")
if err != nil {
// supporting the new way of dev setup
v, err = runError("git", "-C", "..", "rev-parse", "--short", "HEAD")
if err != nil {
return ""
}
}
return string(v)
}

View File

@ -23,13 +23,19 @@ const (
) )
func GrafanaLDFlags(version string, r config.Revision) []string { func GrafanaLDFlags(version string, r config.Revision) []string {
return []string{ cmd := []string{
"-w", "-w",
fmt.Sprintf("-X main.version=%s", version), fmt.Sprintf("-X main.version=%s", version),
fmt.Sprintf("-X main.commit=%s", r.SHA256), fmt.Sprintf("-X main.commit=%s", r.SHA256),
fmt.Sprintf("-X main.buildstamp=%d", r.Timestamp), fmt.Sprintf("-X main.buildstamp=%d", r.Timestamp),
fmt.Sprintf("-X main.buildBranch=%s", r.Branch), fmt.Sprintf("-X main.buildBranch=%s", r.Branch),
} }
if r.EnterpriseCommit != "" {
cmd = append(cmd, fmt.Sprintf("-X main.enterpriseCommit=%s", r.EnterpriseCommit))
}
return cmd
} }
// BinaryFolder returns the path to where the Grafana binary is build given the provided arguments. // BinaryFolder returns the path to where the Grafana binary is build given the provided arguments.

View File

@ -16,6 +16,7 @@ func setBuildInfo(opts ServerOptions) {
} }
setting.BuildVersion = opts.Version setting.BuildVersion = opts.Version
setting.BuildCommit = opts.Commit setting.BuildCommit = opts.Commit
setting.EnterpriseBuildCommit = opts.EnterpriseCommit
setting.BuildStamp = buildstampInt64 setting.BuildStamp = buildstampInt64
setting.BuildBranch = opts.BuildBranch setting.BuildBranch = opts.BuildBranch
setting.IsEnterprise = extensions.IsEnterprise setting.IsEnterprise = extensions.IsEnterprise

View File

@ -25,12 +25,13 @@ import (
type ServerOptions struct { type ServerOptions struct {
Version string Version string
Commit string Commit string
EnterpriseCommit string
BuildBranch string BuildBranch string
BuildStamp string BuildStamp string
Context *cli.Context Context *cli.Context
} }
func ServerCommand(version, commit, buildBranch, buildstamp string) *cli.Command { func ServerCommand(version, commit, enterpriseCommit, buildBranch, buildstamp string) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "server", Name: "server",
Usage: "run the grafana server", Usage: "run the grafana server",
@ -39,6 +40,7 @@ func ServerCommand(version, commit, buildBranch, buildstamp string) *cli.Command
return RunServer(ServerOptions{ return RunServer(ServerOptions{
Version: version, Version: version,
Commit: commit, Commit: commit,
EnterpriseCommit: enterpriseCommit,
BuildBranch: buildBranch, BuildBranch: buildBranch,
BuildStamp: buildstamp, BuildStamp: buildstamp,
Context: context, Context: context,

View File

@ -14,6 +14,7 @@ import (
// The following variables cannot be constants, since they can be overridden through the -X link flag // The following variables cannot be constants, since they can be overridden through the -X link flag
var version = "9.2.0" var version = "9.2.0"
var commit = "NA" var commit = "NA"
var enterpriseCommit = "NA"
var buildBranch = "main" var buildBranch = "main"
var buildstamp string var buildstamp string
@ -30,7 +31,7 @@ func main() {
Version: version, Version: version,
Commands: []*cli.Command{ Commands: []*cli.Command{
gcli.CLICommand(version), gcli.CLICommand(version),
gsrv.ServerCommand(version, commit, buildBranch, buildstamp), gsrv.ServerCommand(version, commit, enterpriseCommit, buildBranch, buildstamp),
}, },
CommandNotFound: cmdNotFound, CommandNotFound: cmdNotFound,
EnableBashCompletion: true, EnableBashCompletion: true,

View File

@ -65,6 +65,7 @@ var (
// build // build
BuildVersion string BuildVersion string
BuildCommit string BuildCommit string
EnterpriseBuildCommit string
BuildBranch string BuildBranch string
BuildStamp int64 BuildStamp int64
IsEnterprise bool IsEnterprise bool
@ -178,6 +179,7 @@ type Cfg struct {
// build // build
BuildVersion string BuildVersion string
BuildCommit string BuildCommit string
EnterpriseBuildCommit string
BuildBranch string BuildBranch string
BuildStamp int64 BuildStamp int64
IsEnterprise bool IsEnterprise bool
@ -1024,6 +1026,7 @@ func (cfg *Cfg) Load(args CommandLineArgs) error {
cfg.BuildVersion = BuildVersion cfg.BuildVersion = BuildVersion
cfg.BuildCommit = BuildCommit cfg.BuildCommit = BuildCommit
cfg.EnterpriseBuildCommit = EnterpriseBuildCommit
cfg.BuildStamp = BuildStamp cfg.BuildStamp = BuildStamp
cfg.BuildBranch = BuildBranch cfg.BuildBranch = BuildBranch
cfg.IsEnterprise = IsEnterprise cfg.IsEnterprise = IsEnterprise