go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/culpritaction/revertculprit/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 revertculprit
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.chromium.org/luci/bisection/metrics"
    22  	"go.chromium.org/luci/bisection/model"
    23  	bisectionpb "go.chromium.org/luci/bisection/proto/v1"
    24  	"go.chromium.org/luci/bisection/util/datastoreutil"
    25  	"go.chromium.org/luci/common/errors"
    26  	"go.chromium.org/luci/common/logging"
    27  	"go.chromium.org/luci/common/tsmon/field"
    28  	"go.chromium.org/luci/common/tsmon/metric"
    29  )
    30  
    31  var (
    32  	culpritActionCounter = metric.NewCounter(
    33  		"bisection/culprit_action/action_taken",
    34  		"The number of actions that LUCI Bisection took against culprits",
    35  		nil,
    36  		// The LUCI Project.
    37  		field.String("project"),
    38  		// "compile" or "test"
    39  		field.String("failure_type"),
    40  		// "create_revert", "submit_revert", "comment_culprit", "comment_revert"
    41  		field.String("action_type"),
    42  	)
    43  )
    44  
    45  type ActionType string
    46  
    47  const (
    48  	// LUCI Bisection created a revert for this culprit
    49  	ActionTypeCreateRevert ActionType = "create_revert"
    50  	// LUCI Bisection submitted a revert for this culprit
    51  	ActionTypeSubmitRevert = "submit_revert"
    52  	// LUCI Bisection commented on the culprit
    53  	ActionTypeCommentCulprit = "comment_culprit"
    54  	// LUCI Bisection commented on a revert for this culprit
    55  	ActionTypeCommentRevert = "comment_revert"
    56  )
    57  
    58  func updateCulpritActionCounter(c context.Context, suspect *model.Suspect, actionType ActionType) error {
    59  	var failureType string
    60  	var project string
    61  	switch suspect.AnalysisType {
    62  	case bisectionpb.AnalysisType_COMPILE_FAILURE_ANALYSIS:
    63  		bbid, err := datastoreutil.GetAssociatedBuildID(c, suspect)
    64  		if err != nil {
    65  			return errors.Annotate(err, "GetAssociatedBuildID").Err()
    66  		}
    67  		build, err := datastoreutil.GetBuild(c, bbid)
    68  		if err != nil {
    69  			return errors.Annotate(err, "getting build %d", bbid).Err()
    70  		}
    71  		if build == nil {
    72  			return fmt.Errorf("no build %d", bbid)
    73  		}
    74  		project = build.Project
    75  		failureType = string(metrics.AnalysisTypeCompile)
    76  	case bisectionpb.AnalysisType_TEST_FAILURE_ANALYSIS:
    77  		tfa, err := datastoreutil.GetTestFailureAnalysisForSuspect(c, suspect)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		project = tfa.Project
    82  		failureType = string(metrics.AnalysisTypeTest)
    83  	default:
    84  		logging.Errorf(c, "unknown analysis type of suspect %s", suspect.AnalysisType.String())
    85  		return nil
    86  	}
    87  	culpritActionCounter.Add(c, 1, project, failureType, string(actionType))
    88  	return nil
    89  }