go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/tsmon/monitor.go (about) 1 // Copyright 2023 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 "context" 19 20 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" 21 "google.golang.org/grpc" 22 "google.golang.org/grpc/credentials" 23 24 "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/common/tsmon/monitor" 26 "go.chromium.org/luci/grpc/grpcmon" 27 28 "go.chromium.org/luci/server/auth" 29 ) 30 31 // NewProdXMonitor creates a monitor that flushes metrics to the ProdX endpoint. 32 // 33 // It splits them into chunks of given size and impersonates the given service 34 // account to authenticate calls. 35 func NewProdXMonitor(ctx context.Context, chunkSize int, account string) (monitor.Monitor, error) { 36 cred, err := auth.GetPerRPCCredentials( 37 ctx, auth.AsActor, 38 auth.WithServiceAccount(account), 39 auth.WithScopes(monitor.ProdxmonScopes...), 40 ) 41 if err != nil { 42 return nil, errors.Annotate(err, "failed to get per RPC credentials").Err() 43 } 44 conn, err := grpc.Dial( 45 prodXEndpoint, 46 grpc.WithTransportCredentials(credentials.NewTLS(nil)), 47 grpc.WithPerRPCCredentials(cred), 48 grpc.WithStatsHandler(&grpcmon.ClientRPCStatsMonitor{}), 49 grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), 50 grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()), 51 ) 52 if err != nil { 53 return nil, errors.Annotate(err, "failed to dial ProdX service %s", prodXEndpoint).Err() 54 } 55 return monitor.NewGRPCMonitor(ctx, chunkSize, conn), nil 56 }