github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/cmd/logdump/main.go (about)

     1  // Copyright 2023 Google LLC
     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  // logdump binary reads reproxy log files and dumps them in a single file that is queryable by gqui.
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/bazelbuild/reclient/internal/pkg/logger"
    24  	"github.com/bazelbuild/reclient/internal/pkg/rbeflag"
    25  	"github.com/bazelbuild/reclient/pkg/version"
    26  
    27  	"google.golang.org/protobuf/proto"
    28  
    29  	lpb "github.com/bazelbuild/reclient/api/log"
    30  
    31  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/moreflag"
    32  	log "github.com/golang/glog"
    33  )
    34  
    35  var (
    36  	proxyLogDir []string
    37  	logFormat   = flag.String("log_format", "text", "Format of proxy log. Currently only text is supported.")
    38  	logPath     = flag.String("log_path", "", "DEPRECATED. Use proxy_log_dir instead. If provided, the path to a log file of all executed records. The format is e.g. text://full/file/path.")
    39  	outputDir   = flag.String("output_dir", "/tmp/", "The location to which stats should be written.")
    40  )
    41  
    42  func main() {
    43  	flag.Var((*moreflag.StringListValue)(&proxyLogDir), "proxy_log_dir", "Comma-separated list of directory paths to aggregate proxy logs from.")
    44  	rbeflag.Parse()
    45  	version.PrintAndExitOnVersionFlag(true)
    46  
    47  	if *logPath == "" && len(proxyLogDir) == 0 {
    48  		log.Fatal("Must provide proxy log path.")
    49  	}
    50  	if *outputDir == "" {
    51  		log.Fatal("Must provide an output directory.")
    52  	}
    53  	format, err := logger.ParseFormat(*logFormat)
    54  	if err != nil {
    55  		log.Fatalf("Bad log format: %v", err)
    56  	}
    57  
    58  	var recs []*lpb.LogRecord
    59  	if len(proxyLogDir) != 0 {
    60  		recs, _, err = logger.ParseFromLogDirs(format, proxyLogDir)
    61  		if err != nil {
    62  			log.Fatalf("Failed to parse log files: %v", err)
    63  		}
    64  	} else {
    65  		recs, err = logger.ParseFromFile(format, *logPath)
    66  		if err != nil {
    67  			log.Fatalf("Failed to parse log file: %v", err)
    68  		}
    69  	}
    70  	dump := &lpb.LogDump{Records: recs}
    71  	out, err := proto.Marshal(dump)
    72  	if err != nil {
    73  		log.Fatalf("Failed to encode log records: %v", err)
    74  	}
    75  	if err := os.WriteFile(filepath.Join(*outputDir, "reproxy_log.pb"), out, 0644); err != nil {
    76  		log.Fatalf("Failed to write log pb file: %v", err)
    77  	}
    78  	log.Infof("Log dumped successfully.")
    79  }