go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/flags.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 tsmon
    16  
    17  import (
    18  	"flag"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/tsmon/target"
    22  )
    23  
    24  // Flags defines command line flags related to tsmon.  Use NewFlags()
    25  // to get a Flags struct with sensible default values.
    26  type Flags struct {
    27  	ConfigFile    string
    28  	Endpoint      string
    29  	Credentials   string
    30  	ActAs         string
    31  	Flush         FlushType
    32  	FlushInterval time.Duration
    33  
    34  	Target target.Flags
    35  }
    36  
    37  // NewFlags returns a Flags struct with sensible default values.
    38  func NewFlags() Flags {
    39  	return Flags{
    40  		ConfigFile:    defaultConfigFilePath(),
    41  		Endpoint:      "",
    42  		Credentials:   "",
    43  		ActAs:         "",
    44  		Flush:         FlushAuto,
    45  		FlushInterval: time.Minute,
    46  
    47  		Target: target.NewFlags(),
    48  	}
    49  }
    50  
    51  // Register adds tsmon related flags to a FlagSet.
    52  func (fl *Flags) Register(f *flag.FlagSet) {
    53  	f.StringVar(&fl.ConfigFile, "ts-mon-config-file", fl.ConfigFile,
    54  		"path to a JSON config file that contains suitable values for "+
    55  			"\"endpoint\" and \"credentials\" for this machine. This config file is "+
    56  			"intended to be shared by all processes on the machine, as the values "+
    57  			"depend on the machine's position in the network, IP allowlisting and "+
    58  			"deployment of credentials.")
    59  	f.StringVar(&fl.Endpoint, "ts-mon-endpoint", fl.Endpoint,
    60  		"url (including file://, https://, pubsub://project/topic) to post "+
    61  			"monitoring metrics to. If set, overrides the value in "+
    62  			"--ts-mon-config-file")
    63  	f.StringVar(&fl.Credentials, "ts-mon-credentials", fl.Credentials,
    64  		"path to a pkcs8 json credential file. If set, overrides the value in "+
    65  			"--ts-mon-config-file")
    66  	f.StringVar(&fl.ActAs, "ts-mon-act-as", fl.ActAs,
    67  		"(advanced) a service account email to impersonate when authenticating to "+
    68  			"tsmon backends. Uses 'iam' scope and serviceAccountTokenCreator role. "+
    69  			"If set, overrides the value in --ts-mon-config-file")
    70  	f.Var(&fl.Flush, "ts-mon-flush",
    71  		"metric push behavior: manual (only send when Flush() is called), or auto "+
    72  			"(send automatically every --ts-mon-flush-interval)")
    73  	f.DurationVar(&fl.FlushInterval, "ts-mon-flush-interval", fl.FlushInterval,
    74  		"automatically push metrics on this interval if --ts-mon-flush=auto")
    75  
    76  	fl.Target.Register(f)
    77  }