go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/tryjob/metrics.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 tryjob
    16  
    17  import (
    18  	"context"
    19  
    20  	bbmetrics "go.chromium.org/luci/buildbucket/metrics"
    21  	"go.chromium.org/luci/common/errors"
    22  	"go.chromium.org/luci/common/tsmon/target"
    23  
    24  	"go.chromium.org/luci/cv/internal/common"
    25  )
    26  
    27  // RunWithBuilderMetricsTarget executes `cb` under the context of a Builder
    28  // metrics target.
    29  //
    30  // It is normally used to report metrics using Builder Target.
    31  func RunWithBuilderMetricsTarget(ctx context.Context, env *common.Env, def *Definition, cb func(context.Context)) {
    32  	if def.GetBuildbucket() == nil {
    33  		panic(errors.Reason("only Buildbucket backend is supported. Got %T", def.GetBackend()))
    34  	}
    35  	builder := def.GetBuildbucket().GetBuilder()
    36  	cctx := target.Set(ctx, &bbmetrics.BuilderTarget{
    37  		Project: builder.GetProject(),
    38  		Bucket:  builder.GetBucket(),
    39  		Builder: builder.GetBuilder(),
    40  
    41  		ServiceName: env.GAEInfo.CloudProject,
    42  		JobName:     env.GAEInfo.ServiceName,
    43  		InstanceID:  env.GAEInfo.InstanceID,
    44  	})
    45  	cb(cctx)
    46  }