Log bp2build_metrics .pb

Also share `Save(pb proto.Message, filepath string)`

Bug: 201539536
Test: bp2build_metrics.pb has expected content & path
Test: m nothing
Test: {bp2build,mixed_{libc,droid}}.sh
Test: CI
Change-Id: I7d8ad87fca6a4b0355010090a527f5ae67b27c88
This commit is contained in:
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux
2021-11-10 09:55:20 -05:00
parent 484aa25875
commit 947fdbfdee
10 changed files with 136 additions and 34 deletions

View File

@@ -32,12 +32,12 @@ package metrics
// of what an event is and how the metrics system is a stack based system.
import (
"io/ioutil"
"os"
"runtime"
"strings"
"time"
"android/soong/shared"
"google.golang.org/protobuf/proto"
soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
@@ -196,7 +196,7 @@ func (m *Metrics) Dump(out string) error {
}
m.metrics.HostOs = proto.String(runtime.GOOS)
return save(&m.metrics, out)
return shared.Save(&m.metrics, out)
}
// SetSoongBuildMetrics sets the metrics collected from the soong_build
@@ -228,25 +228,5 @@ func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
// Dump saves the collected CUJs metrics to the raw protobuf file.
func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
return save(&c.cujs, filename)
}
// save takes a protobuf message, marshals to an array of bytes
// and is then saved to a file.
func save(pb proto.Message, filename string) (err error) {
data, err := proto.Marshal(pb)
if err != nil {
return err
}
tempFilename := filename + ".tmp"
if err := ioutil.WriteFile(tempFilename, []byte(data), 0644 /* rw-r--r-- */); err != nil {
return err
}
if err := os.Rename(tempFilename, filename); err != nil {
return err
}
return nil
return shared.Save(&c.cujs, filename)
}