go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/monitor/main.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 is the main entry point for the app.
    16  package main
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/clock"
    23  	"go.chromium.org/luci/common/errors"
    24  	"go.chromium.org/luci/common/logging"
    25  	"go.chromium.org/luci/common/tsmon"
    26  	"go.chromium.org/luci/common/tsmon/store"
    27  	"go.chromium.org/luci/common/tsmon/target"
    28  
    29  	"go.chromium.org/luci/server"
    30  	"go.chromium.org/luci/server/cron"
    31  	"go.chromium.org/luci/server/gaeemulation"
    32  	"go.chromium.org/luci/server/module"
    33  	"go.chromium.org/luci/server/redisconn"
    34  	"go.chromium.org/luci/server/secrets"
    35  	tsmonsrv "go.chromium.org/luci/server/tsmon"
    36  
    37  	"go.chromium.org/luci/buildbucket/appengine/internal/metrics"
    38  )
    39  
    40  func main() {
    41  	mods := []module.Module{
    42  		cron.NewModuleFromFlags(),
    43  		gaeemulation.NewModuleFromFlags(),
    44  		secrets.NewModuleFromFlags(),
    45  		redisconn.NewModuleFromFlags(),
    46  	}
    47  
    48  	server.Main(nil, mods, func(srv *server.Server) error {
    49  		o := srv.Options
    50  		srv.Context = metrics.WithServiceInfo(srv.Context, o.TsMonServiceName, o.TsMonJobName, o.Hostname)
    51  
    52  		// Init a new tsmon.State with the default task target,
    53  		// configured in luci/server. V1 metrics need it.
    54  		target := *tsmon.GetState(srv.Context).Store().DefaultTarget().(*target.Task)
    55  		state := tsmon.NewState()
    56  		state.SetStore(store.NewInMemory(&target))
    57  		state.InhibitGlobalCallbacksOnFlush()
    58  		ctx := tsmon.WithState(srv.Context, state)
    59  
    60  		mon, err := tsmonsrv.NewProdXMonitor(ctx, 1024, o.TsMonAccount)
    61  		if err != nil {
    62  			srv.Fatal(errors.Annotate(err, "initiating tsmon").Err())
    63  		}
    64  
    65  		cron.RegisterHandler("report_builder_metrics", func(_ context.Context) error {
    66  			// allow at most 10 mins to run.
    67  			ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
    68  			defer cancel()
    69  
    70  			start := clock.Now(ctx)
    71  			if err := metrics.ReportBuilderMetrics(ctx); err != nil {
    72  				return errors.Annotate(err, "computing builder metrics").Err()
    73  			}
    74  			logging.Infof(ctx, "computing builder metrics took %s", clock.Since(ctx, start))
    75  			start = clock.Now(ctx)
    76  			if err := state.ParallelFlush(ctx, mon, 8); err != nil {
    77  				return errors.Annotate(err, "flushing builder metrics").Err()
    78  			}
    79  			logging.Infof(ctx, "flushing builder metrics took %s", clock.Since(ctx, start))
    80  			return nil
    81  		})
    82  
    83  		return nil
    84  	})
    85  }