From 73c790f59e1afeb7658ae994c59f4e0d371a1e71 Mon Sep 17 00:00:00 2001 From: Patrice Arruda Date: Mon, 13 Jul 2020 23:01:18 +0000 Subject: [PATCH] Use the build start time from Soong main UI for metrics build timestamp. Currently, the build timestamp saved to the build_date_timestamp field in the MetricsBase metrics protobuf usually comes from a text file named build_date.txt. It may not be accurate for metrics purposes. Instead, use the build start time from Soong main UI for a better timestamp. Bug: b/140638454 Test: Ran and checked the build_date_timestamp by running the printproto command. Change-Id: I4d893fbbf9830ba21911e56ae13bc5272be47ae6 --- cmd/soong_ui/main.go | 5 +++-- ui/build/config.go | 3 --- ui/build/upload.go | 4 ++-- ui/build/upload_test.go | 4 ++-- ui/metrics/metrics.go | 12 +++--------- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go index d3802f9ae..e485c60e0 100644 --- a/cmd/soong_ui/main.go +++ b/cmd/soong_ui/main.go @@ -117,7 +117,7 @@ func inList(s string, list []string) bool { // Command is the type of soong_ui execution. Only one type of // execution is specified. The args are specific to the command. func main() { - buildStartedMilli := time.Now().UnixNano() / int64(time.Millisecond) + buildStarted := time.Now() c, args := getCommand(os.Args) if c == nil { @@ -138,6 +138,7 @@ func main() { defer trace.Close() met := metrics.New() + met.SetBuildDateTime(buildStarted) stat := &status.Status{} defer stat.Finish() @@ -171,7 +172,7 @@ func main() { buildErrorFile := filepath.Join(logsDir, c.logsPrefix+"build_error") rbeMetricsFile := filepath.Join(logsDir, c.logsPrefix+"rbe_metrics.pb") soongMetricsFile := filepath.Join(logsDir, c.logsPrefix+"soong_metrics") - defer build.UploadMetrics(buildCtx, config, c.forceDumbOutput, buildStartedMilli, buildErrorFile, rbeMetricsFile, soongMetricsFile) + defer build.UploadMetrics(buildCtx, config, c.forceDumbOutput, buildStarted, buildErrorFile, rbeMetricsFile, soongMetricsFile) os.MkdirAll(logsDir, 0777) log.SetOutput(filepath.Join(logsDir, c.logsPrefix+"soong.log")) diff --git a/ui/build/config.go b/ui/build/config.go index e567e403e..a60d70e13 100644 --- a/ui/build/config.go +++ b/ui/build/config.go @@ -256,9 +256,6 @@ func NewConfig(ctx Context, args ...string) Config { ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10) } - if ctx.Metrics != nil { - ctx.Metrics.SetBuildDateTime(ret.buildDateTime) - } ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile) return Config{ret} diff --git a/ui/build/upload.go b/ui/build/upload.go index 3a23a8013..1cc2e940c 100644 --- a/ui/build/upload.go +++ b/ui/build/upload.go @@ -44,7 +44,7 @@ var ( // environment variable. The metrics files are copied to a temporary directory // and the uploader is then executed in the background to allow the user to continue // working. -func UploadMetrics(ctx Context, config Config, forceDumbOutput bool, buildStartedMilli int64, files ...string) { +func UploadMetrics(ctx Context, config Config, forceDumbOutput bool, buildStarted time.Time, files ...string) { ctx.BeginTrace(metrics.RunSetupTool, "upload_metrics") defer ctx.EndTrace() @@ -86,7 +86,7 @@ func UploadMetrics(ctx Context, config Config, forceDumbOutput bool, buildStarte // For platform builds, the branch and target name is hardcoded to specific // values for later extraction of the metrics in the data metrics pipeline. data, err := proto.Marshal(&upload_proto.Upload{ - CreationTimestampMs: proto.Uint64(uint64(buildStartedMilli)), + CreationTimestampMs: proto.Uint64(uint64(buildStarted.UnixNano() / int64(time.Millisecond))), CompletionTimestampMs: proto.Uint64(uint64(time.Now().UnixNano() / int64(time.Millisecond))), BranchName: proto.String("developer-metrics"), TargetName: proto.String("platform-build-systems-metrics"), diff --git a/ui/build/upload_test.go b/ui/build/upload_test.go index c812730a8..dccf156a2 100644 --- a/ui/build/upload_test.go +++ b/ui/build/upload_test.go @@ -97,7 +97,7 @@ func TestUploadMetrics(t *testing.T) { buildDateTime: strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10), }} - UploadMetrics(ctx, config, false, 1591031903, metricsFiles...) + UploadMetrics(ctx, config, false, time.Now(), metricsFiles...) }) } } @@ -151,7 +151,7 @@ func TestUploadMetricsErrors(t *testing.T) { "OUT_DIR=/bad", }}} - UploadMetrics(ctx, config, true, 1591031903, metricsFile) + UploadMetrics(ctx, config, true, time.Now(), metricsFile) t.Errorf("got nil, expecting %q as a failure", tt.expectedErr) }) } diff --git a/ui/metrics/metrics.go b/ui/metrics/metrics.go index e055b769c..2b5c4c3f6 100644 --- a/ui/metrics/metrics.go +++ b/ui/metrics/metrics.go @@ -17,7 +17,7 @@ package metrics import ( "io/ioutil" "os" - "strconv" + "time" "github.com/golang/protobuf/proto" @@ -131,14 +131,8 @@ func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch { } } -func (m *Metrics) SetBuildDateTime(date_time string) { - if date_time != "" { - date_time_timestamp, err := strconv.ParseInt(date_time, 10, 64) - if err != nil { - panic(err) - } - m.metrics.BuildDateTimestamp = &date_time_timestamp - } +func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { + m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) } // exports the output to the file at outputPath