-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
102 lines (87 loc) · 2.65 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package common
import (
"fmt"
"runtime"
"time"
"github.com/codegangsta/cli"
"github.com/prometheus/client_golang/prometheus"
)
var NAME = "gitlab-ci-multi-runner"
var VERSION = "dev"
var REVISION = "HEAD"
var BRANCH = "HEAD"
var BUILT = "now"
var AppVersion AppVersionInfo
type AppVersionInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
GOVersion string `json:"go_version"`
BuiltAt time.Time `json:"built_at"`
OS string `json:"os"`
Architecture string `json:"architecture"`
}
func (v *AppVersionInfo) Printer(c *cli.Context) {
fmt.Print(v.Extended())
}
func (v *AppVersionInfo) Line() string {
return fmt.Sprintf("%s %s (%s)", v.Name, v.Version, v.Revision)
}
func (v *AppVersionInfo) ShortLine() string {
return fmt.Sprintf("%s (%s)", v.Version, v.Revision)
}
func (v *AppVersionInfo) UserAgent() string {
return fmt.Sprintf("%s %s (%s; %s; %s/%s)", v.Name, v.Version, v.Branch, v.GOVersion, v.OS, v.Architecture)
}
func (v *AppVersionInfo) Extended() string {
version := fmt.Sprintf("Version: %s\n", v.Version)
version += fmt.Sprintf("Git revision: %s\n", v.Revision)
version += fmt.Sprintf("Git branch: %s\n", v.Branch)
version += fmt.Sprintf("GO version: %s\n", v.GOVersion)
version += fmt.Sprintf("Built: %s\n", v.BuiltAt.Format(time.RFC1123Z))
version += fmt.Sprintf("OS/Arch: %s/%s\n", v.OS, v.Architecture)
return version
}
// NewMetricsCollector returns a prometheus.Collector which represents current build information.
func (v *AppVersionInfo) NewMetricsCollector() *prometheus.GaugeVec {
labels := map[string]string{
"name": v.Name,
"version": v.Version,
"revision": v.Revision,
"branch": v.Branch,
"go_version": v.GOVersion,
"built_at": v.BuiltAt.String(),
"os": v.OS,
"architecture": v.Architecture,
}
labelNames := make([]string, 0, len(labels))
for n := range labels {
labelNames = append(labelNames, n)
}
buildInfo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ci_runner_version_info",
Help: "A metric with a constant '1' value labeled by different build stats fields.",
},
labelNames,
)
buildInfo.With(labels).Set(1)
return buildInfo
}
func init() {
builtAt := time.Now()
if BUILT != "now" {
builtAt, _ = time.Parse(time.RFC3339, BUILT)
}
AppVersion = AppVersionInfo{
Name: NAME,
Version: VERSION,
Revision: REVISION,
Branch: BRANCH,
GOVersion: runtime.Version(),
BuiltAt: builtAt,
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
}
}