go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/appengine/monitor/main.go (about) 1 // Copyright 2022 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 "go.chromium.org/luci/server" 29 "go.chromium.org/luci/server/cron" 30 "go.chromium.org/luci/server/gaeemulation" 31 "go.chromium.org/luci/server/module" 32 "go.chromium.org/luci/server/redisconn" 33 "go.chromium.org/luci/server/secrets" 34 tsmonsrv "go.chromium.org/luci/server/tsmon" 35 36 "go.chromium.org/luci/cv/internal/aggrmetrics" 37 "go.chromium.org/luci/cv/internal/common" 38 ) 39 40 const ( 41 // aggregateMetricsCronTimeout is the amount off time the Cron has to compute 42 // and flush the aggregation metrics. 43 aggregateMetricsCronTimeout = 2 * time.Minute 44 ) 45 46 func main() { 47 48 modules := []module.Module{ 49 cron.NewModuleFromFlags(), 50 gaeemulation.NewModuleFromFlags(), 51 redisconn.NewModuleFromFlags(), 52 secrets.NewModuleFromFlags(), 53 } 54 55 server.Main(nil, modules, func(srv *server.Server) error { 56 opts := srv.Options 57 env := common.MakeEnv(opts) 58 59 // Init a new tsmon.State with the default task target, 60 // configured in luci/server. 61 target := *tsmon.GetState(srv.Context).Store().DefaultTarget().(*target.Task) 62 state := tsmon.NewState() 63 state.SetStore(store.NewInMemory(&target)) 64 state.InhibitGlobalCallbacksOnFlush() 65 66 mon, err := tsmonsrv.NewProdXMonitor(srv.Context, 1024, opts.TsMonAccount) 67 if err != nil { 68 return errors.Annotate(err, "failed to initiate monitoring client").Err() 69 } 70 71 cron.RegisterHandler("report-aggregated-metrics", func(ctx context.Context) error { 72 ctx, cancel := context.WithTimeout(ctx, aggregateMetricsCronTimeout) 73 defer cancel() 74 75 // Override the state to avoid using the default state from the server. 76 ctx = tsmon.WithState(ctx, state) 77 aggregator := aggrmetrics.New(env) 78 start := clock.Now(ctx) 79 if err := aggregator.Cron(ctx); err != nil { 80 return errors.Annotate(err, "failed to compute aggregation metrics").Err() 81 } 82 logging.Infof(ctx, "computing aggregation metrics took %s", clock.Since(ctx, start)) 83 start = clock.Now(ctx) 84 if err := state.ParallelFlush(ctx, mon, 8); err != nil { 85 return errors.Annotate(err, "failed to flush aggregation metrics").Err() 86 } 87 logging.Infof(ctx, "flushing aggregation metrics took %s", clock.Since(ctx, start)) 88 return nil 89 }) 90 91 return nil 92 }) 93 }