go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/metrics/builder.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 bbmetrics
    16  
    17  import (
    18  	"hash/fnv"
    19  	"reflect"
    20  
    21  	"go.chromium.org/luci/common/tsmon/target"
    22  	tsmonpb "go.chromium.org/luci/common/tsmon/ts_mon_proto"
    23  	"go.chromium.org/luci/common/tsmon/types"
    24  )
    25  
    26  // BuilderTarget is a metric target that represents a LUCI Builder.
    27  type BuilderTarget struct {
    28  	// Project is the LUCI project of the Builder.
    29  	Project string
    30  	// Bucket is the bucket name of the Builder.
    31  	Bucket string
    32  	// Builder is the name of the Builder.
    33  	Builder string
    34  
    35  	// ServiceName is the reporting service name.
    36  	//
    37  	// For a GAE app, typically, it will be the GAE application name.
    38  	ServiceName string
    39  	// JobName is the reporting service name.
    40  	//
    41  	// For a GAE app, typically, it will be the module name of the GAE
    42  	// application.
    43  	JobName string
    44  	// InstanceID is the ID of the reporting instance.
    45  	//
    46  	// For a GAE app, typically, it will be the GAE host name.
    47  	InstanceID string
    48  }
    49  
    50  // Clone returns a deep copy.
    51  func (b *BuilderTarget) Clone() types.Target {
    52  	clone := *b
    53  	return &clone
    54  }
    55  
    56  // Type returns the metric type identification.
    57  func (b *BuilderTarget) Type() types.TargetType {
    58  	return types.TargetType{
    59  		Name: "buildbucket.Builder",
    60  		Type: reflect.TypeOf(&BuilderTarget{}),
    61  	}
    62  }
    63  
    64  // Hash computes a hash of the Builder object.
    65  func (b *BuilderTarget) Hash() uint64 {
    66  	h := fnv.New64a()
    67  	h.Write([]byte(b.Project))
    68  	h.Write([]byte(b.Bucket))
    69  	h.Write([]byte(b.Builder))
    70  	h.Write([]byte(b.ServiceName))
    71  	h.Write([]byte(b.JobName))
    72  	h.Write([]byte(b.InstanceID))
    73  	return h.Sum64()
    74  }
    75  
    76  // PopulateProto populates root labels into the proto for the target fields.
    77  func (b *BuilderTarget) PopulateProto(d *tsmonpb.MetricsCollection) {
    78  	d.RootLabels = []*tsmonpb.MetricsCollection_RootLabels{
    79  		target.RootLabel("project", b.Project),
    80  		target.RootLabel("bucket", b.Bucket),
    81  		target.RootLabel("builder", b.Builder),
    82  
    83  		target.RootLabel("service_name", b.ServiceName),
    84  		target.RootLabel("job_name", b.JobName),
    85  		target.RootLabel("instance_id", b.InstanceID),
    86  	}
    87  }