blob: 3a23a8013049b2fbae335b48ee9eb44cb9dabfec [file] [log] [blame]
Patrice Arruda219eef32020-06-01 17:29:30 +00001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17// This file contains the functionality to upload data from one location to
18// another.
19
20import (
21 "io/ioutil"
22 "os"
23 "path/filepath"
24 "time"
25
Patrice Arruda7cc20742020-06-10 18:48:01 +000026 "android/soong/ui/metrics"
Patrice Arruda219eef32020-06-01 17:29:30 +000027 "github.com/golang/protobuf/proto"
28
29 upload_proto "android/soong/ui/metrics/upload_proto"
30)
31
32const (
33 uploadPbFilename = ".uploader.pb"
34)
35
Patrice Arruda7cc20742020-06-10 18:48:01 +000036var (
37 // For testing purpose
38 getTmpDir = ioutil.TempDir
39)
40
Patrice Arruda219eef32020-06-01 17:29:30 +000041// UploadMetrics uploads a set of metrics files to a server for analysis. An
42// uploader full path is required to be specified in order to upload the set
43// of metrics files. This is accomplished by defining the ANDROID_ENABLE_METRICS_UPLOAD
Patrice Arruda7cc20742020-06-10 18:48:01 +000044// environment variable. The metrics files are copied to a temporary directory
45// and the uploader is then executed in the background to allow the user to continue
46// working.
47func UploadMetrics(ctx Context, config Config, forceDumbOutput bool, buildStartedMilli int64, files ...string) {
48 ctx.BeginTrace(metrics.RunSetupTool, "upload_metrics")
49 defer ctx.EndTrace()
50
Patrice Arruda219eef32020-06-01 17:29:30 +000051 uploader := config.MetricsUploaderApp()
52 // No metrics to upload if the path to the uploader was not specified.
53 if uploader == "" {
54 return
55 }
56
57 // Some files may not exist. For example, build errors protobuf file
58 // may not exist since the build was successful.
59 var metricsFiles []string
60 for _, f := range files {
61 if _, err := os.Stat(f); err == nil {
62 metricsFiles = append(metricsFiles, f)
63 }
64 }
65
66 if len(metricsFiles) == 0 {
67 return
68 }
69
Patrice Arruda7cc20742020-06-10 18:48:01 +000070 // The temporary directory cannot be deleted as the metrics uploader is started
71 // in the background and requires to exist until the operation is done. The
72 // uploader can delete the directory as it is specified in the upload proto.
73 tmpDir, err := getTmpDir("", "upload_metrics")
74 if err != nil {
75 ctx.Fatalf("failed to create a temporary directory to store the list of metrics files: %v\n", err)
76 }
77
78 for i, src := range metricsFiles {
79 dst := filepath.Join(tmpDir, filepath.Base(src))
80 if _, err := copyFile(src, dst); err != nil {
81 ctx.Fatalf("failed to copy %q to %q: %v\n", src, dst, err)
82 }
83 metricsFiles[i] = dst
84 }
85
Patrice Arruda219eef32020-06-01 17:29:30 +000086 // For platform builds, the branch and target name is hardcoded to specific
87 // values for later extraction of the metrics in the data metrics pipeline.
88 data, err := proto.Marshal(&upload_proto.Upload{
89 CreationTimestampMs: proto.Uint64(uint64(buildStartedMilli)),
90 CompletionTimestampMs: proto.Uint64(uint64(time.Now().UnixNano() / int64(time.Millisecond))),
91 BranchName: proto.String("developer-metrics"),
92 TargetName: proto.String("platform-build-systems-metrics"),
93 MetricsFiles: metricsFiles,
Patrice Arruda7cc20742020-06-10 18:48:01 +000094 DirectoriesToDelete: []string{tmpDir},
Patrice Arruda219eef32020-06-01 17:29:30 +000095 })
96 if err != nil {
97 ctx.Fatalf("failed to marshal metrics upload proto buffer message: %v\n", err)
98 }
99
Patrice Arruda7cc20742020-06-10 18:48:01 +0000100 pbFile := filepath.Join(tmpDir, uploadPbFilename)
Patrice Arruda219eef32020-06-01 17:29:30 +0000101 if err := ioutil.WriteFile(pbFile, data, 0644); err != nil {
102 ctx.Fatalf("failed to write the marshaled metrics upload protobuf to %q: %v\n", pbFile, err)
103 }
Patrice Arruda219eef32020-06-01 17:29:30 +0000104
Patrice Arruda7cc20742020-06-10 18:48:01 +0000105 // Start the uploader in the background as it takes several milliseconds to start the uploader
106 // and prepare the metrics for upload. This affects small commands like "lunch".
107 cmd := Command(ctx, config, "upload metrics", uploader, "--upload-metrics", pbFile)
108 if forceDumbOutput {
109 cmd.RunOrFatal()
110 } else {
111 cmd.RunAndStreamOrFatal()
112 }
Patrice Arruda219eef32020-06-01 17:29:30 +0000113}