go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/bqlog/module.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 bqlog 16 17 import ( 18 "context" 19 "flag" 20 "time" 21 22 storage "cloud.google.com/go/bigquery/storage/apiv1" 23 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" 24 "google.golang.org/api/option" 25 "google.golang.org/grpc" 26 "google.golang.org/grpc/keepalive" 27 28 "go.chromium.org/luci/common/errors" 29 "go.chromium.org/luci/grpc/grpcmon" 30 31 "go.chromium.org/luci/server/auth" 32 "go.chromium.org/luci/server/module" 33 ) 34 35 // ModuleName can be used to refer to this module when declaring dependencies. 36 var ModuleName = module.RegisterName("go.chromium.org/luci/server/bqlog") 37 38 // ModuleOptions contain configuration of the bqlog server module. 39 // 40 // It will be used to initialize Default bundler. 41 type ModuleOptions struct { 42 // Bundler is a bundler to use. 43 // 44 // Default is the global Default instance. 45 Bundler *Bundler 46 47 // CloudProject is a project with the dataset to send logs to. 48 // 49 // Default is the project the server is running in. 50 CloudProject string 51 52 // Dataset is a BQ dataset to send logs to. 53 // 54 // Can be omitted in the development mode. In that case BQ writes will be 55 // disabled and the module will be running in the dry run mode. Required in 56 // the production. 57 Dataset string 58 } 59 60 // Register registers the command line flags. 61 func (o *ModuleOptions) Register(f *flag.FlagSet) { 62 f.StringVar(&o.CloudProject, "bqlog-cloud-project", o.CloudProject, 63 `Cloud Project with the BQ dataset with tables to writes logs into, default is the same as -cloud-project.`) 64 f.StringVar(&o.Dataset, "bqlog-dataset", o.Dataset, 65 `BigQuery dataset name within the project with tables to writes logs into. Required.`) 66 } 67 68 // NewModule returns a server module that sets up a cron dispatcher. 69 func NewModule(opts *ModuleOptions) module.Module { 70 if opts == nil { 71 opts = &ModuleOptions{} 72 } 73 return &bqlogModule{opts: opts} 74 } 75 76 // NewModuleFromFlags is a variant of NewModule that initializes options through 77 // command line flags. 78 // 79 // Calling this function registers flags in flag.CommandLine. They are usually 80 // parsed in server.Main(...). 81 func NewModuleFromFlags() module.Module { 82 opts := &ModuleOptions{} 83 opts.Register(flag.CommandLine) 84 return NewModule(opts) 85 } 86 87 // bqlogModule implements module.Module. 88 type bqlogModule struct { 89 opts *ModuleOptions 90 } 91 92 // Name is part of module.Module interface. 93 func (*bqlogModule) Name() module.Name { 94 return ModuleName 95 } 96 97 // Dependencies is part of module.Module interface. 98 func (*bqlogModule) Dependencies() []module.Dependency { 99 return nil 100 } 101 102 // Initialize is part of module.Module interface. 103 func (m *bqlogModule) Initialize(ctx context.Context, host module.Host, opts module.HostOptions) (context.Context, error) { 104 bundler := m.opts.Bundler 105 if bundler == nil { 106 bundler = &Default 107 } 108 109 if m.opts.CloudProject != "" { 110 bundler.CloudProject = m.opts.CloudProject 111 } else { 112 bundler.CloudProject = opts.CloudProject 113 } 114 if opts.Prod && m.opts.Dataset == "" { 115 return nil, errors.Reason("flag -bqlog-dataset is required").Err() 116 } 117 bundler.Dataset = m.opts.Dataset 118 119 var writer BigQueryWriter 120 if m.opts.Dataset != "" { 121 // Create the production writer that writes to the BQ for real. 122 creds, err := auth.GetPerRPCCredentials(ctx, auth.AsSelf, auth.WithScopes(auth.CloudOAuthScopes...)) 123 if err != nil { 124 return nil, errors.Annotate(err, "failed to initialize credentials").Err() 125 } 126 writer, err = storage.NewBigQueryWriteClient(ctx, 127 option.WithGRPCDialOption(grpc.WithStatsHandler(&grpcmon.ClientRPCStatsMonitor{})), 128 option.WithGRPCDialOption(grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor())), 129 option.WithGRPCDialOption(grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor())), 130 option.WithGRPCDialOption(grpc.WithPerRPCCredentials(creds)), 131 option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{ 132 Time: time.Minute, 133 })), 134 ) 135 if err != nil { 136 return nil, errors.Annotate(err, "failed to create BQ write client").Err() 137 } 138 } else { 139 // Use some phony names in the dev mode, we won't be writing to BQ. 140 bundler.CloudProject = "bqlog-project" 141 bundler.Dataset = "bqlog-dataset" 142 // Use a writer that just silently discards everything. 143 writer = &FakeBigQueryWriter{} 144 } 145 146 // Launch the bundler background activities after the server is fully 147 // initialized (i.e. when it launches its background activities). But use 148 // the root context, since we want the bundler to keep running right until its 149 // Shutdown logic is called below. If we use `ctx` from RunInBackground, 150 // the bundler will completely shutdown before it has a chance to drain itself 151 // properly. 152 rootCtx := ctx 153 host.RunInBackground("luci.bqlog", func(ctx context.Context) { 154 bundler.Start(rootCtx, writer) 155 }) 156 157 // Try to flush everything gracefully when closing down. 158 host.RegisterCleanup(func(ctx context.Context) { 159 bundler.Shutdown(ctx) 160 writer.Close() 161 }) 162 163 return ctx, nil 164 }