veyron2,veyron/runtimes/google/rt: introduce binary metadata
This provides a way to query any vanadium binary about its build metadata (using
the --help flag), and also prints the information at the top of the log files.
The Go version comes from the runtime package, and the build metadata is filled
in with ldflags, e.g.
v23 go install -ldflags "-X v.io/core/veyron/lib/flags/buildinfo.vanadiumBuildInfo $USER`date -u +.%Y%m%d.%H%M%S`" ./veyron/services/mgmt/binary/binaryd
$> binaryd --help
{"GoVersion":"go1.4","BuildInfo":"caprita.20150220.193152"}
Usage of binaryd:
....
$> binaryd
I0220 11:32:14.855011 12933 runtime.go:72] Binary info: {"GoVersion":"go1.4","BuildInfo":"caprita.20150220.193152"}
I0220 11:32:14.881716 12933 main.go:58] Binary repository rooted at /tmp/veyron_binary_repository473207613
I0220 11:32:14.882220 12933 main.go:71] Binary repository HTTP server at: "172.22.140.20:36030"
...
The next step would be to get v23 to fill in the metadata in ldflags
automatically at build time.
Change-Id: I5de26bc697e620d11b10ae7d7a93c05e7fe3a21f
diff --git a/lib/flags/buildinfo/buildinfo.go b/lib/flags/buildinfo/buildinfo.go
new file mode 100644
index 0000000..fcc2427
--- /dev/null
+++ b/lib/flags/buildinfo/buildinfo.go
@@ -0,0 +1,29 @@
+package buildinfo
+
+import (
+ "encoding/json"
+ "runtime"
+)
+
+// vanadiumBuildInfo is filled in at link time, using:
+// -ldflags "-X v.io/core/veyron/lib/flags/buildinfo.vanadiumBuildInfo <value>"
+var vanadiumBuildInfo string
+
+// VanadiumBinaryInfo describes binary metadata.
+type VanadiumBinaryInfo struct {
+ GoVersion, BuildInfo string
+}
+
+// BinaryInfo returns the binary metadata as a JSON-encoded string, under the
+// expectation that clients may want to parse it for specific bits of metadata.
+func BinaryInfo() string {
+ info := VanadiumBinaryInfo{
+ GoVersion: runtime.Version(),
+ BuildInfo: vanadiumBuildInfo,
+ }
+ jsonInfo, err := json.Marshal(info)
+ if err != nil {
+ return ""
+ }
+ return string(jsonInfo)
+}
diff --git a/lib/flags/buildinfo/doc.go b/lib/flags/buildinfo/doc.go
new file mode 100644
index 0000000..33d0dba
--- /dev/null
+++ b/lib/flags/buildinfo/doc.go
@@ -0,0 +1,2 @@
+// Package buildinfo provides build-time metadata about the binary.
+package buildinfo
diff --git a/lib/flags/flags.go b/lib/flags/flags.go
index 241420c..8a4930c 100644
--- a/lib/flags/flags.go
+++ b/lib/flags/flags.go
@@ -7,6 +7,7 @@
"strings"
"sync"
+ "v.io/core/veyron/lib/flags/buildinfo"
"v.io/core/veyron/lib/flags/consts"
)
@@ -379,6 +380,28 @@
func (f *Flags) Parse(args []string, cfg map[string]string) error {
// TODO(cnicolaou): implement a single env var 'VANADIUM_OPTS'
// that can be used to specify any command line.
+ fs := f.FlagSet
+ if fs == flag.CommandLine {
+ // We treat the default command-line flag set specially w.r.t.
+ // printing out the build binary metadata: we want to display it
+ // as the first thing in the help message for the binary.
+ //
+ // We do this by overriding the usage function for the duration
+ // of the Parse.
+ oldUsage := fs.Usage
+ defer func() {
+ fs.Usage = oldUsage
+ }()
+ fs.Usage = func() {
+ fmt.Fprintf(os.Stderr, "Binary info: %v\n", buildinfo.BinaryInfo())
+ if oldUsage == nil {
+ flag.Usage()
+ } else {
+ oldUsage()
+ }
+ }
+ }
+
if err := f.FlagSet.Parse(args); err != nil {
return err
}
diff --git a/runtimes/google/rt/runtime.go b/runtimes/google/rt/runtime.go
index b421871..945b77e 100644
--- a/runtimes/google/rt/runtime.go
+++ b/runtimes/google/rt/runtime.go
@@ -21,6 +21,7 @@
"v.io/v23/vtrace"
"v.io/core/veyron/lib/flags"
+ "v.io/core/veyron/lib/flags/buildinfo"
"v.io/core/veyron/lib/stats"
_ "v.io/core/veyron/lib/stats/sysstats"
iipc "v.io/core/veyron/runtimes/google/ipc"
@@ -70,6 +71,8 @@
if err != nil {
return nil, nil, nil, err
}
+ // TODO(caprita): Only print this out for servers?
+ vlog.Infof("Binary info: %v", buildinfo.BinaryInfo())
// Setup the initial trace.
ctx, err = ivtrace.Init(ctx, flags.Vtrace)