Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame^] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | // This file contains the functionality to upload data from one location to |
| 18 | // another. |
| 19 | |
| 20 | import ( |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "time" |
| 25 | |
| 26 | "github.com/golang/protobuf/proto" |
| 27 | |
| 28 | upload_proto "android/soong/ui/metrics/upload_proto" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | uploadPbFilename = ".uploader.pb" |
| 33 | ) |
| 34 | |
| 35 | // UploadMetrics uploads a set of metrics files to a server for analysis. An |
| 36 | // uploader full path is required to be specified in order to upload the set |
| 37 | // of metrics files. This is accomplished by defining the ANDROID_ENABLE_METRICS_UPLOAD |
| 38 | // environment variable. |
| 39 | func UploadMetrics(ctx Context, config Config, buildStartedMilli int64, files ...string) { |
| 40 | uploader := config.MetricsUploaderApp() |
| 41 | // No metrics to upload if the path to the uploader was not specified. |
| 42 | if uploader == "" { |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | // Some files may not exist. For example, build errors protobuf file |
| 47 | // may not exist since the build was successful. |
| 48 | var metricsFiles []string |
| 49 | for _, f := range files { |
| 50 | if _, err := os.Stat(f); err == nil { |
| 51 | metricsFiles = append(metricsFiles, f) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if len(metricsFiles) == 0 { |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // For platform builds, the branch and target name is hardcoded to specific |
| 60 | // values for later extraction of the metrics in the data metrics pipeline. |
| 61 | data, err := proto.Marshal(&upload_proto.Upload{ |
| 62 | CreationTimestampMs: proto.Uint64(uint64(buildStartedMilli)), |
| 63 | CompletionTimestampMs: proto.Uint64(uint64(time.Now().UnixNano() / int64(time.Millisecond))), |
| 64 | BranchName: proto.String("developer-metrics"), |
| 65 | TargetName: proto.String("platform-build-systems-metrics"), |
| 66 | MetricsFiles: metricsFiles, |
| 67 | }) |
| 68 | if err != nil { |
| 69 | ctx.Fatalf("failed to marshal metrics upload proto buffer message: %v\n", err) |
| 70 | } |
| 71 | |
| 72 | pbFile := filepath.Join(config.OutDir(), uploadPbFilename) |
| 73 | if err := ioutil.WriteFile(pbFile, data, 0644); err != nil { |
| 74 | ctx.Fatalf("failed to write the marshaled metrics upload protobuf to %q: %v\n", pbFile, err) |
| 75 | } |
| 76 | // Remove the upload file as it's not longer needed after it has been processed by the uploader. |
| 77 | defer os.Remove(pbFile) |
| 78 | |
| 79 | Command(ctx, config, "upload metrics", uploader, "--upload-metrics", pbFile).RunAndStreamOrFatal() |
| 80 | } |