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

     1  // Copyright 2015 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 contains information about the thing that is sending metrics -
    16  // either a NetworkDevice (a machine) or a Task (a service).
    17  // There is a default target that is usually configured with commandline flags
    18  // (flags.go), but a target can also be passed through the Context (context.go)
    19  // if you need to set metric values for a different target.
    20  package target
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"hash/fnv"
    26  	"reflect"
    27  
    28  	"go.chromium.org/luci/common/tsmon/types"
    29  )
    30  
    31  // A Task is a process or a service running on one or more machine.
    32  type Task struct {
    33  	ServiceName string
    34  	JobName     string
    35  	DataCenter  string
    36  	HostName    string
    37  	TaskNum     int32
    38  }
    39  
    40  // Hash returns a uint64 hash of this target.
    41  func (t *Task) Hash() uint64 {
    42  	h := fnv.New64a()
    43  	fmt.Fprintf(h, "%s\n%s\n%s\n%s\n%d",
    44  		t.ServiceName,
    45  		t.JobName,
    46  		t.DataCenter,
    47  		t.HostName,
    48  		t.TaskNum)
    49  	return h.Sum64()
    50  }
    51  
    52  // Clone returns a copy of this object.
    53  func (t *Task) Clone() types.Target {
    54  	clone := *t
    55  	return &clone
    56  }
    57  
    58  // Type returns the TargetType of Task.
    59  func (t *Task) Type() types.TargetType {
    60  	return types.TargetType{Name: "task", Type: reflect.TypeOf(t)}
    61  }
    62  
    63  // A NetworkDevice is a machine that has a hostname.
    64  type NetworkDevice struct {
    65  	Metro     string
    66  	Role      string
    67  	Hostname  string
    68  	Hostgroup string
    69  }
    70  
    71  // Hash returns a uint64 hash of this target.
    72  func (t *NetworkDevice) Hash() uint64 {
    73  	h := fnv.New64a()
    74  	fmt.Fprintf(h, "%s\n%s\n%s\n%s",
    75  		t.Metro,
    76  		t.Role,
    77  		t.Hostname,
    78  		t.Hostgroup)
    79  	return h.Sum64()
    80  }
    81  
    82  // Clone returns a copy of this object.
    83  func (t *NetworkDevice) Clone() types.Target {
    84  	clone := *t
    85  	return &clone
    86  }
    87  
    88  // Type returns the TargetType of NetworkDevice.
    89  func (t *NetworkDevice) Type() types.TargetType {
    90  	return types.TargetType{Name: "device", Type: reflect.TypeOf(t)}
    91  }
    92  
    93  // NewFromFlags returns a Target configured from commandline flags.
    94  func NewFromFlags(fl *Flags) (types.Target, error) {
    95  	if fl.TargetType == TaskType {
    96  		if fl.TaskServiceName == "" {
    97  			return nil, errors.New(
    98  				"--ts-mon-task-service-name must be provided when using --ts-mon-target-type=task")
    99  		}
   100  		if fl.TaskJobName == "" {
   101  			return nil, errors.New(
   102  				"--ts-mon-task-job-name must be provided when using --ts-mon-target-type=task")
   103  		}
   104  
   105  		return &Task{
   106  			ServiceName: fl.TaskServiceName,
   107  			JobName:     fl.TaskJobName,
   108  			DataCenter:  fl.TaskRegion,
   109  			HostName:    fl.TaskHostname,
   110  			TaskNum:     int32(fl.TaskNumber),
   111  		}, nil
   112  	} else if fl.TargetType == DeviceType {
   113  		return &NetworkDevice{
   114  			Metro:     fl.DeviceRegion,
   115  			Role:      fl.DeviceRole,
   116  			Hostname:  fl.DeviceHostname,
   117  			Hostgroup: fl.DeviceNetwork,
   118  		}, nil
   119  	} else {
   120  		return nil, fmt.Errorf("unknown --ts-mon-target-type '%s'", fl.TargetType)
   121  	}
   122  }