| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Package target contains information about the thing that is sending metrics - | 5 // Package target contains information about the thing that is sending metrics - |
| 6 // either a NetworkDevice (a machine) or a Task (a service). | 6 // either a NetworkDevice (a machine) or a Task (a service). |
| 7 // There is a default target that is usually configured with commandline flags | 7 // There is a default target that is usually configured with commandline flags |
| 8 // (flags.go), but a target can also be passed through the Context (context.go) | 8 // (flags.go), but a target can also be passed through the Context (context.go) |
| 9 // if you need to set metric values for a different target. | 9 // if you need to set metric values for a different target. |
| 10 package target | 10 package target |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 // AsProto returns this object as a tsmon.proto.Task message. | 26 // AsProto returns this object as a tsmon.proto.Task message. |
| 27 func (t *Task) AsProto() *pb.Task { return (*pb.Task)(t) } | 27 func (t *Task) AsProto() *pb.Task { return (*pb.Task)(t) } |
| 28 | 28 |
| 29 // PopulateProto implements Target. | 29 // PopulateProto implements Target. |
| 30 func (t *Task) PopulateProto(d *pb.MetricsData) { d.Task = t.AsProto() } | 30 func (t *Task) PopulateProto(d *pb.MetricsData) { d.Task = t.AsProto() } |
| 31 | 31 |
| 32 // IsPopulatedIn returns true if the MetricsData message is for this target. | 32 // IsPopulatedIn returns true if the MetricsData message is for this target. |
| 33 func (t *Task) IsPopulatedIn(d *pb.MetricsData) bool { | 33 func (t *Task) IsPopulatedIn(d *pb.MetricsData) bool { |
| 34 return d.Task != nil && | 34 return d.Task != nil && |
| 35 » » d.Task.ServiceName == t.AsProto().ServiceName && | 35 » » d.Task.GetServiceName() == t.AsProto().GetServiceName() && |
| 36 » » d.Task.JobName == t.AsProto().JobName && | 36 » » d.Task.GetJobName() == t.AsProto().GetJobName() && |
| 37 » » d.Task.DataCenter == t.AsProto().DataCenter && | 37 » » d.Task.GetDataCenter() == t.AsProto().GetDataCenter() && |
| 38 » » d.Task.HostName == t.AsProto().HostName && | 38 » » d.Task.GetHostName() == t.AsProto().GetHostName() && |
| 39 » » d.Task.TaskNum == t.AsProto().TaskNum | 39 » » d.Task.GetTaskNum() == t.AsProto().GetTaskNum() |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Hash returns a uint64 hash of this target. | 42 // Hash returns a uint64 hash of this target. |
| 43 func (t *Task) Hash() uint64 { | 43 func (t *Task) Hash() uint64 { |
| 44 h := fnv.New64a() | 44 h := fnv.New64a() |
| 45 fmt.Fprintf(h, "%s\n%s\n%s\n%s\n%d", | 45 fmt.Fprintf(h, "%s\n%s\n%s\n%s\n%d", |
| 46 » » t.AsProto().ServiceName, | 46 » » t.AsProto().GetServiceName(), |
| 47 » » t.AsProto().JobName, | 47 » » t.AsProto().GetJobName(), |
| 48 » » t.AsProto().DataCenter, | 48 » » t.AsProto().GetDataCenter(), |
| 49 » » t.AsProto().HostName, | 49 » » t.AsProto().GetHostName(), |
| 50 » » t.AsProto().TaskNum) | 50 » » t.AsProto().GetTaskNum()) |
| 51 return h.Sum64() | 51 return h.Sum64() |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Clone returns a copy of this object. | 54 // Clone returns a copy of this object. |
| 55 func (t *Task) Clone() types.Target { | 55 func (t *Task) Clone() types.Target { |
| 56 return (*Task)(proto.Clone(t.AsProto()).(*pb.Task)) | 56 return (*Task)(proto.Clone(t.AsProto()).(*pb.Task)) |
| 57 } | 57 } |
| 58 | 58 |
| 59 // A NetworkDevice is a machine that has a hostname. | 59 // A NetworkDevice is a machine that has a hostname. |
| 60 type NetworkDevice pb.NetworkDevice | 60 type NetworkDevice pb.NetworkDevice |
| 61 | 61 |
| 62 // AsProto returns this object as a tsmon.proto.NetworkDevice message. | 62 // AsProto returns this object as a tsmon.proto.NetworkDevice message. |
| 63 func (t *NetworkDevice) AsProto() *pb.NetworkDevice { return (*pb.NetworkDevice)
(t) } | 63 func (t *NetworkDevice) AsProto() *pb.NetworkDevice { return (*pb.NetworkDevice)
(t) } |
| 64 | 64 |
| 65 // PopulateProto implements Target. | 65 // PopulateProto implements Target. |
| 66 func (t *NetworkDevice) PopulateProto(d *pb.MetricsData) { d.NetworkDevice = t.A
sProto() } | 66 func (t *NetworkDevice) PopulateProto(d *pb.MetricsData) { d.NetworkDevice = t.A
sProto() } |
| 67 | 67 |
| 68 // IsPopulatedIn returns true if the MetricsData message is for this target. | 68 // IsPopulatedIn returns true if the MetricsData message is for this target. |
| 69 func (t *NetworkDevice) IsPopulatedIn(d *pb.MetricsData) bool { | 69 func (t *NetworkDevice) IsPopulatedIn(d *pb.MetricsData) bool { |
| 70 return d.NetworkDevice != nil && | 70 return d.NetworkDevice != nil && |
| 71 » » d.NetworkDevice.Alertable == t.AsProto().Alertable && | 71 » » d.NetworkDevice.GetAlertable() == t.AsProto().GetAlertable() && |
| 72 » » d.NetworkDevice.Realm == t.AsProto().Realm && | 72 » » d.NetworkDevice.GetRealm() == t.AsProto().GetRealm() && |
| 73 » » d.NetworkDevice.Metro == t.AsProto().Metro && | 73 » » d.NetworkDevice.GetMetro() == t.AsProto().GetMetro() && |
| 74 » » d.NetworkDevice.Role == t.AsProto().Role && | 74 » » d.NetworkDevice.GetRole() == t.AsProto().GetRole() && |
| 75 » » d.NetworkDevice.Hostname == t.AsProto().Hostname && | 75 » » d.NetworkDevice.GetHostname() == t.AsProto().GetHostname() && |
| 76 » » d.NetworkDevice.Hostgroup == t.AsProto().Hostgroup | 76 » » d.NetworkDevice.GetHostgroup() == t.AsProto().GetHostgroup() |
| 77 } | 77 } |
| 78 | 78 |
| 79 // Hash returns a uint64 hash of this target. | 79 // Hash returns a uint64 hash of this target. |
| 80 func (t *NetworkDevice) Hash() uint64 { | 80 func (t *NetworkDevice) Hash() uint64 { |
| 81 h := fnv.New64a() | 81 h := fnv.New64a() |
| 82 fmt.Fprintf(h, "%t%s\n%s\n%s\n%s\n%s", | 82 fmt.Fprintf(h, "%t%s\n%s\n%s\n%s\n%s", |
| 83 » » t.AsProto().Alertable, | 83 » » t.AsProto().GetAlertable(), |
| 84 » » t.AsProto().Realm, | 84 » » t.AsProto().GetRealm(), |
| 85 » » t.AsProto().Metro, | 85 » » t.AsProto().GetMetro(), |
| 86 » » t.AsProto().Role, | 86 » » t.AsProto().GetRole(), |
| 87 » » t.AsProto().Hostname, | 87 » » t.AsProto().GetHostname(), |
| 88 » » t.AsProto().Hostgroup) | 88 » » t.AsProto().GetHostgroup()) |
| 89 return h.Sum64() | 89 return h.Sum64() |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Clone returns a copy of this object. | 92 // Clone returns a copy of this object. |
| 93 func (t *NetworkDevice) Clone() types.Target { | 93 func (t *NetworkDevice) Clone() types.Target { |
| 94 return (*NetworkDevice)(proto.Clone(t.AsProto()).(*pb.NetworkDevice)) | 94 return (*NetworkDevice)(proto.Clone(t.AsProto()).(*pb.NetworkDevice)) |
| 95 } | 95 } |
| 96 | 96 |
| 97 // NewFromFlags returns a Target configured from commandline flags. | 97 // NewFromFlags returns a Target configured from commandline flags. |
| 98 func NewFromFlags(fl *Flags) (types.Target, error) { | 98 func NewFromFlags(fl *Flags) (types.Target, error) { |
| 99 if fl.TargetType == "task" { | 99 if fl.TargetType == "task" { |
| 100 if fl.TaskServiceName == "" { | 100 if fl.TaskServiceName == "" { |
| 101 return nil, errors.New( | 101 return nil, errors.New( |
| 102 "--ts-mon-task-service-name must be provided whe
n using --ts-mon-target-type=task") | 102 "--ts-mon-task-service-name must be provided whe
n using --ts-mon-target-type=task") |
| 103 } | 103 } |
| 104 if fl.TaskJobName == "" { | 104 if fl.TaskJobName == "" { |
| 105 return nil, errors.New( | 105 return nil, errors.New( |
| 106 "--ts-mon-task-job-name must be provided when us
ing --ts-mon-target-type=task") | 106 "--ts-mon-task-job-name must be provided when us
ing --ts-mon-target-type=task") |
| 107 } | 107 } |
| 108 | 108 |
| 109 return (*Task)(&pb.Task{ | 109 return (*Task)(&pb.Task{ |
| 110 » » » ServiceName: fl.TaskServiceName, | 110 » » » ServiceName: proto.String(fl.TaskServiceName), |
| 111 » » » JobName: fl.TaskJobName, | 111 » » » JobName: proto.String(fl.TaskJobName), |
| 112 » » » DataCenter: fl.TaskRegion, | 112 » » » DataCenter: proto.String(fl.TaskRegion), |
| 113 » » » HostName: fl.TaskHostname, | 113 » » » HostName: proto.String(fl.TaskHostname), |
| 114 » » » TaskNum: int32(fl.TaskNumber), | 114 » » » TaskNum: proto.Int32(int32(fl.TaskNumber)), |
| 115 }), nil | 115 }), nil |
| 116 } else if fl.TargetType == "device" { | 116 } else if fl.TargetType == "device" { |
| 117 return (*NetworkDevice)(&pb.NetworkDevice{ | 117 return (*NetworkDevice)(&pb.NetworkDevice{ |
| 118 » » » Alertable: true, | 118 » » » Alertable: proto.Bool(true), |
| 119 » » » Realm: "ACQ_CHROME", | 119 » » » Realm: proto.String("ACQ_CHROME"), |
| 120 » » » Metro: fl.DeviceRegion, | 120 » » » Metro: proto.String(fl.DeviceRegion), |
| 121 » » » Role: fl.DeviceRole, | 121 » » » Role: proto.String(fl.DeviceRole), |
| 122 » » » Hostname: fl.DeviceHostname, | 122 » » » Hostname: proto.String(fl.DeviceHostname), |
| 123 » » » Hostgroup: fl.DeviceNetwork, | 123 » » » Hostgroup: proto.String(fl.DeviceNetwork), |
| 124 }), nil | 124 }), nil |
| 125 } else { | 125 } else { |
| 126 return nil, fmt.Errorf("unknown --ts-mon-target-type '%s'", fl.T
argetType) | 126 return nil, fmt.Errorf("unknown --ts-mon-target-type '%s'", fl.T
argetType) |
| 127 } | 127 } |
| 128 } | 128 } |
| OLD | NEW |