go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/target/populate.go (about)

     1  // Copyright 2016 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 target
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	pb "go.chromium.org/luci/common/tsmon/ts_mon_proto"
    23  )
    24  
    25  // PopulateProto implements Target.
    26  func (t *Task) PopulateProto(d *pb.MetricsCollection) {
    27  	d.RootLabels = []*pb.MetricsCollection_RootLabels{
    28  		// ProdX automatically adds and sets the following fields
    29  		// with own values, even if tsmon doesn't add them in RootLabels,
    30  		// except that proxy_zone may be required under certain conditions.
    31  		//
    32  		// This adds them to avoid unexpected data loss,
    33  		// just in case the above assumptions change.
    34  		RootLabel("proxy_environment", "pa"),
    35  		RootLabel("acquisition_name", "mon-chrome-infra"),
    36  		RootLabel("proxy_zone", "atl"),
    37  
    38  		RootLabel("service_name", t.ServiceName),
    39  		RootLabel("job_name", t.JobName),
    40  		RootLabel("data_center", t.DataCenter),
    41  		RootLabel("host_name", t.HostName),
    42  		RootLabel("task_num", int64(t.TaskNum)),
    43  	}
    44  }
    45  
    46  // PopulateProto implements Target.
    47  func (t *NetworkDevice) PopulateProto(d *pb.MetricsCollection) {
    48  	d.RootLabels = []*pb.MetricsCollection_RootLabels{
    49  		// ProdX automatically adds and sets the following fields
    50  		// with own values. Even if tsmon sets them with own values,
    51  		// the custom values are ignored.
    52  		//
    53  		// This adds them to avoid unexpected data loss,
    54  		// just in case the above assumptions change.
    55  		RootLabel("proxy_environment", "pa"),
    56  		RootLabel("acquisition_name", "mon-chrome-infra"),
    57  		RootLabel("proxy_zone", "atl"),
    58  
    59  		RootLabel("pop", ""),
    60  		RootLabel("alertable", true),
    61  		RootLabel("realm", "ACQ_CHROME"),
    62  		RootLabel("asn", int64(0)),
    63  		RootLabel("metro", t.Metro),
    64  		RootLabel("role", t.Role),
    65  		RootLabel("hostname", t.Hostname),
    66  		RootLabel("vendor", ""),
    67  		RootLabel("hostgroup", t.Hostgroup),
    68  	}
    69  }
    70  
    71  // RootLabel returns a root label with a given value.
    72  func RootLabel(key string, value any) *pb.MetricsCollection_RootLabels {
    73  	label := &pb.MetricsCollection_RootLabels{Key: proto.String(key)}
    74  
    75  	switch v := reflect.ValueOf(value); v.Kind() {
    76  	case reflect.String:
    77  		label.Value = &pb.MetricsCollection_RootLabels_StringValue{
    78  			StringValue: value.(string),
    79  		}
    80  	case reflect.Int64:
    81  		label.Value = &pb.MetricsCollection_RootLabels_Int64Value{
    82  			Int64Value: value.(int64),
    83  		}
    84  	case reflect.Bool:
    85  		label.Value = &pb.MetricsCollection_RootLabels_BoolValue{
    86  			BoolValue: value.(bool),
    87  		}
    88  	default:
    89  		panic("unsupported type; all target fields must be one of string, int64, or bool.")
    90  	}
    91  	return label
    92  }