go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/internal/metrics/build_events.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 metrics
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/common/data/strpair"
    21  
    22  	"go.chromium.org/luci/buildbucket/appengine/model"
    23  	pb "go.chromium.org/luci/buildbucket/proto"
    24  )
    25  
    26  // BuildCreated updates metrics for a build creation event.
    27  func BuildCreated(ctx context.Context, b *model.Build) {
    28  	bpb := b.Proto.Builder
    29  
    30  	// v1
    31  	ua := ""
    32  	for _, tag := range b.Tags {
    33  		if k, v := strpair.Parse(tag); k == "user_agent" {
    34  			ua = v
    35  			break
    36  		}
    37  	}
    38  	V1.BuildCountCreated.Add(ctx, 1, legacyBucketName(bpb.Project, bpb.Bucket), bpb.Builder, ua)
    39  
    40  	// V2
    41  	ctx = WithBuilder(ctx, bpb.Project, bpb.Bucket, bpb.Builder)
    42  	V2.BuildCountCreated.Add(ctx, 1, b.ExperimentsString())
    43  }
    44  
    45  // BuildStarted updates metrics for a build start event.
    46  func BuildStarted(ctx context.Context, b *model.Build) {
    47  	bp, bpb := b.Proto, b.Proto.Builder
    48  	var schD float64
    49  
    50  	// v1
    51  	legacyBucket := legacyBucketName(bpb.Project, bpb.Bucket)
    52  	V1.BuildCountStarted.Add(ctx, 1, legacyBucket, bpb.Builder, bp.Canary)
    53  	if bp.StartTime != nil {
    54  		schD = bp.StartTime.AsTime().Sub(b.CreateTime).Seconds()
    55  		V1.BuildDurationScheduling.Add(ctx, schD, legacyBucket, bpb.Builder, "", "", "", bp.Canary)
    56  	}
    57  
    58  	// v2
    59  	ctx = WithBuilder(ctx, bpb.Project, bpb.Bucket, bpb.Builder)
    60  	exps := b.ExperimentsString()
    61  	V2.BuildCountStarted.Add(ctx, 1, exps)
    62  	if bp.StartTime != nil {
    63  		V2.BuildDurationScheduling.Add(ctx, schD, exps)
    64  	}
    65  }
    66  
    67  // BuildCompleted updates metrics for a build completion event.
    68  func BuildCompleted(ctx context.Context, b *model.Build) {
    69  	bp, bpb := b.Proto, b.Proto.Builder
    70  	cycleD := bp.EndTime.AsTime().Sub(b.CreateTime).Seconds()
    71  	var runD float64
    72  
    73  	// v1
    74  	legacyBucket := legacyBucketName(bpb.Project, bpb.Bucket)
    75  	_, reason, failReason, cancelReason := getLegacyMetricFields(b)
    76  	V1.BuildCountCompleted.Add(
    77  		ctx, 1, legacyBucket, bpb.Builder,
    78  		reason, failReason, cancelReason, bp.Canary)
    79  	V1.BuildDurationCycle.Add(
    80  		ctx, cycleD, legacyBucket, bpb.Builder,
    81  		reason, failReason, cancelReason, bp.Canary)
    82  	if bp.StartTime != nil {
    83  		runD = bp.EndTime.AsTime().Sub(bp.StartTime.AsTime()).Seconds()
    84  		V1.BuildDurationRun.Add(
    85  			ctx, runD, legacyBucket, bpb.Builder,
    86  			reason, failReason, cancelReason, bp.Canary)
    87  	}
    88  
    89  	// v2
    90  	ctx = WithBuilder(ctx, bpb.Project, bpb.Bucket, bpb.Builder)
    91  	exps := b.ExperimentsString()
    92  	status := pb.Status_name[int32(b.Status)]
    93  	V2.BuildCountCompleted.Add(ctx, 1, status, exps)
    94  	V2.BuildDurationCycle.Add(ctx, cycleD, status, exps)
    95  	if b.Proto.StartTime != nil {
    96  		V2.BuildDurationRun.Add(ctx, runD, status, exps)
    97  	}
    98  }
    99  
   100  // ExpiredLeaseReset updates metrics for an expired lease reset.
   101  func ExpiredLeaseReset(ctx context.Context, b *model.Build) {
   102  	legacyBucket := legacyBucketName(b.Proto.Builder.Project, b.Proto.Builder.Bucket)
   103  	status, _, _, _ := getLegacyMetricFields(b)
   104  	V1.ExpiredLeaseReset.Add(ctx, 1, legacyBucket, b.Proto.Builder.Builder, status)
   105  }