github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/build/build.go (about) 1 // Package build contains build-related variables set at compile time. 2 package build 3 4 import ( 5 "encoding/json" 6 "fmt" 7 "runtime" 8 "strconv" 9 "strings" 10 11 "github.com/prometheus/client_golang/prometheus" 12 "github.com/pyroscope-io/pyroscope/pkg/agent/spy" 13 "github.com/pyroscope-io/pyroscope/webapp" 14 ) 15 16 var ( 17 Version = "N/A" 18 ID = "N/A" 19 Time = "N/A" 20 21 GitSHA = "N/A" 22 GitDirtyStr = "-1" 23 GitDirty int 24 25 PhpspyGitSHA = "N/A" 26 ) 27 28 func init() { 29 GitDirty, _ = strconv.Atoi(GitDirtyStr) 30 } 31 32 const tmplt = ` 33 GENERAL 34 GOARCH: %s 35 GOOS: %s 36 Go Version: %s 37 Version: %s 38 Build ID: %s 39 Build Time: %s 40 Git SHA: %s 41 Git Dirty Files: %d 42 Embedded Assets: %t 43 44 AGENT 45 Supported Spies: %q 46 phpspy Git SHA: %q 47 ` 48 49 func Summary() string { 50 return fmt.Sprintf(strings.TrimSpace(tmplt), 51 runtime.GOARCH, 52 runtime.GOOS, 53 runtime.Version(), 54 Version, 55 ID, 56 Time, 57 GitSHA, 58 GitDirty, 59 webapp.AssetsEmbedded, 60 spy.SupportedSpies, 61 PhpspyGitSHA, 62 ) 63 } 64 65 type buildInfoJSON struct { 66 GOOS string `json:"goos"` 67 GOARCH string `json:"goarch"` 68 GoVersion string `json:"goVersion"` 69 Version string `json:"version"` 70 ID string `json:"id"` 71 Time string `json:"time"` 72 GitSHA string `json:"gitSHA"` 73 GitDirty int `json:"gitDirty"` 74 UseEmbeddedAssets bool `json:"useEmbeddedAssets"` 75 PhpspyGitSHA string `json:"phpspyGitSHA"` 76 } 77 78 func generateBuildInfoJSON() buildInfoJSON { 79 return buildInfoJSON{ 80 GOOS: runtime.GOOS, 81 GOARCH: runtime.GOARCH, 82 GoVersion: runtime.Version(), 83 Version: Version, 84 ID: ID, 85 Time: Time, 86 GitSHA: GitSHA, 87 GitDirty: GitDirty, 88 UseEmbeddedAssets: webapp.AssetsEmbedded, 89 PhpspyGitSHA: PhpspyGitSHA, 90 } 91 } 92 93 func JSON() string { 94 b, _ := json.Marshal(generateBuildInfoJSON()) 95 return string(b) 96 } 97 98 func PrettyJSON() string { 99 b, _ := json.MarshalIndent(generateBuildInfoJSON(), "", " ") 100 return string(b) 101 } 102 103 // PrometheusBuildLabels returns a map of the labels 104 // that will be exposed in the build_info metric 105 func PrometheusBuildLabels() prometheus.Labels { 106 return prometheus.Labels{ 107 "GOOS": runtime.GOOS, 108 "GOARCH": runtime.GOARCH, 109 "version": Version, 110 "time": Time, 111 "revision": GitSHA, 112 "use_embedded_assets": strconv.FormatBool(webapp.AssetsEmbedded), 113 } 114 }