go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/server/cmd/logdog_collector/flags.go (about)

     1  // Copyright 2021 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 main
    16  
    17  import (
    18  	"flag"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/errors"
    22  	"go.chromium.org/luci/common/gcloud/pubsub"
    23  )
    24  
    25  // CommandLineFlags contains collector service configuration.
    26  //
    27  // It is exposed via CLI flags.
    28  type CommandLineFlags struct {
    29  	// MaxConcurrentMessages is the maximum number of concurrent transport
    30  	// messages to process.
    31  	//
    32  	// If <= 0, a default will be chosen based on the transport.
    33  	MaxConcurrentMessages int
    34  
    35  	// StateCacheSize is the maximum number of log stream states to cache locally.
    36  	//
    37  	// If <= 0, a default will be used.
    38  	StateCacheSize int
    39  
    40  	// StateCacheExpiration is the maximum amount of time that cached stream state
    41  	// is valid.
    42  	//
    43  	// If <= 0, a default will be used.
    44  	StateCacheExpiration time.Duration
    45  
    46  	// MaxMessageWorkers is the maximum number of concurrent workers to process
    47  	// each ingested message.
    48  	//
    49  	// If <= 0, collector.DefaultMaxMessageWorkers will be used.
    50  	MaxMessageWorkers int
    51  
    52  	// PubSubProject is the Cloud Project name that hosts the PubSub subscription
    53  	// that receives incoming logs.
    54  	//
    55  	// Required.
    56  	PubSubProject string
    57  
    58  	// PubSubSubscription is the name of the PubSub subscription that receives
    59  	// incoming logs.
    60  	//
    61  	// Required.
    62  	PubSubSubscription string
    63  }
    64  
    65  // Register registers flags in the flag set.
    66  func (f *CommandLineFlags) Register(fs *flag.FlagSet) {
    67  	fs.IntVar(&f.MaxConcurrentMessages, "max-concurrent-messages", f.MaxConcurrentMessages,
    68  		"Maximum number of concurrent transport messages to process.")
    69  	fs.IntVar(&f.StateCacheSize, "state-cache-size", f.StateCacheSize,
    70  		"maximum number of log stream states to cache locally.")
    71  	fs.DurationVar(&f.StateCacheExpiration, "state-cache-expiration", f.StateCacheExpiration,
    72  		"Maximum amount of time that cached stream state is valid.")
    73  	fs.IntVar(&f.MaxMessageWorkers, "max-message-workers", f.MaxMessageWorkers,
    74  		"Maximum number of concurrent workers to process each ingested message.")
    75  	fs.StringVar(&f.PubSubProject, "pubsub-project", f.PubSubProject,
    76  		"Cloud Project that hosts the PubSub subscription.")
    77  	fs.StringVar(&f.PubSubSubscription, "pubsub-subscription", f.PubSubSubscription,
    78  		"PubSub subscription within the project.")
    79  }
    80  
    81  // Validate returns an error if some parsed flags have invalid values.
    82  func (f *CommandLineFlags) Validate() error {
    83  	if f.PubSubProject == "" {
    84  		return errors.New("-pubsub-project is required")
    85  	}
    86  	if f.PubSubSubscription == "" {
    87  		return errors.New("-pubsub-subscription is required")
    88  	}
    89  	sub := pubsub.NewSubscription(f.PubSubProject, f.PubSubSubscription)
    90  	if err := sub.Validate(); err != nil {
    91  		return errors.Annotate(err, "invalid Pub/Sub subscription %q", sub).Err()
    92  	}
    93  	return nil
    94  }