| 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 """Classes representing the monitoring interface for tasks or devices. | 5 """Classes representing the monitoring interface for tasks or devices. |
| 6 | 6 |
| 7 Usage: | 7 Usage: |
| 8 import argparse | 8 import argparse |
| 9 from infra_libs import ts_mon | 9 from infra_libs import ts_mon |
| 10 | 10 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 """Generate MetricsPayload for global_monitor.send().""" | 116 """Generate MetricsPayload for global_monitor.send().""" |
| 117 proto = new_metrics_pb2.MetricsPayload() | 117 proto = new_metrics_pb2.MetricsPayload() |
| 118 | 118 |
| 119 # Key: Target, value: MetricsCollection. | 119 # Key: Target, value: MetricsCollection. |
| 120 collections = {} | 120 collections = {} |
| 121 | 121 |
| 122 # Key: (Target, metric name) tuple, value: MetricsDataSet. | 122 # Key: (Target, metric name) tuple, value: MetricsDataSet. |
| 123 data_sets = {} | 123 data_sets = {} |
| 124 | 124 |
| 125 count = 0 | 125 count = 0 |
| 126 error_count = 0 | |
| 127 for (target, metric, start_time, end_time, fields_values | 126 for (target, metric, start_time, end_time, fields_values |
| 128 ) in state.store.get_all(): | 127 ) in state.store.get_all(): |
| 129 for fields, value in fields_values.iteritems(): | 128 for fields, value in fields_values.iteritems(): |
| 130 if count >= METRICS_DATA_LENGTH_LIMIT: | 129 if count >= METRICS_DATA_LENGTH_LIMIT: |
| 131 yield proto | 130 yield proto |
| 132 proto = new_metrics_pb2.MetricsPayload() | 131 proto = new_metrics_pb2.MetricsPayload() |
| 133 collections.clear() | 132 collections.clear() |
| 134 data_sets.clear() | 133 data_sets.clear() |
| 135 count = 0 | 134 count = 0 |
| 136 | 135 |
| 137 if target not in collections: | 136 if target not in collections: |
| 138 collections[target] = proto.metrics_collection.add() | 137 collections[target] = proto.metrics_collection.add() |
| 139 target._populate_target_pb_new(collections[target]) | 138 target._populate_target_pb_new(collections[target]) |
| 140 collection = collections[target] | 139 collection = collections[target] |
| 141 | 140 |
| 142 key = (target, metric.name) | 141 key = (target, metric.name) |
| 143 new_data_set = None | 142 new_data_set = None |
| 144 try: | 143 if key not in data_sets: |
| 145 if key not in data_sets: | 144 new_data_set = new_metrics_pb2.MetricsDataSet() |
| 146 new_data_set = new_metrics_pb2.MetricsDataSet() | 145 metric._populate_data_set(new_data_set) |
| 147 metric._populate_data_set(new_data_set, fields) | |
| 148 | 146 |
| 149 data = new_metrics_pb2.MetricsData() | 147 data = new_metrics_pb2.MetricsData() |
| 150 metric._populate_data(data, start_time, end_time, fields, value) | 148 metric._populate_data(data, start_time, end_time, fields, value) |
| 151 except errors.MonitoringError: | |
| 152 logging.exception('Failed to serialize a metric.') | |
| 153 error_count += 1 | |
| 154 continue | |
| 155 | 149 |
| 156 # All required data protos have been successfully populated. Now we can | 150 # All required data protos have been successfully populated. Now we can |
| 157 # insert them in serialized proto and bookeeping data structures. | 151 # insert them in serialized proto and bookeeping data structures. |
| 158 if new_data_set is not None: | 152 if new_data_set is not None: |
| 159 collection.metrics_data_set.add().CopyFrom(new_data_set) | 153 collection.metrics_data_set.add().CopyFrom(new_data_set) |
| 160 data_sets[key] = collection.metrics_data_set[-1] | 154 data_sets[key] = collection.metrics_data_set[-1] |
| 161 data_sets[key].data.add().CopyFrom(data) | 155 data_sets[key].data.add().CopyFrom(data) |
| 162 count += 1 | 156 count += 1 |
| 163 | 157 |
| 164 if count > 0: | 158 if count > 0: |
| 165 yield proto | 159 yield proto |
| 166 | 160 |
| 167 if error_count: | |
| 168 raise errors.MonitoringFailedToFlushAllMetricsError(error_count) | |
| 169 | |
| 170 | 161 |
| 171 def _generate_proto(): | 162 def _generate_proto(): |
| 172 """Generate MetricsCollection for global_monitor.send().""" | 163 """Generate MetricsCollection for global_monitor.send().""" |
| 173 proto = metrics_pb2.MetricsCollection() | 164 proto = metrics_pb2.MetricsCollection() |
| 174 | 165 |
| 175 error_count = 0 | |
| 176 for target, metric, start_time, _, fields_values in state.store.get_all(): | 166 for target, metric, start_time, _, fields_values in state.store.get_all(): |
| 177 for fields, value in fields_values.iteritems(): | 167 for fields, value in fields_values.iteritems(): |
| 178 if len(proto.data) >= METRICS_DATA_LENGTH_LIMIT: | 168 if len(proto.data) >= METRICS_DATA_LENGTH_LIMIT: |
| 179 yield proto | 169 yield proto |
| 180 proto = metrics_pb2.MetricsCollection() | 170 proto = metrics_pb2.MetricsCollection() |
| 181 | 171 |
| 182 try: | 172 metrics_pb = metrics_pb2.MetricsData() |
| 183 metrics_pb = metrics_pb2.MetricsData() | 173 metric.serialize_to(metrics_pb, start_time, fields, value, target) |
| 184 metric.serialize_to(metrics_pb, start_time, fields, value, target) | |
| 185 except errors.MonitoringError: | |
| 186 error_count += 1 | |
| 187 logging.exception('Failed to serialize a metric.') | |
| 188 continue | |
| 189 | 174 |
| 190 proto.data.add().CopyFrom(metrics_pb) | 175 proto.data.add().CopyFrom(metrics_pb) |
| 191 | 176 |
| 192 if len(proto.data) > 0: | 177 if len(proto.data) > 0: |
| 193 yield proto | 178 yield proto |
| 194 | 179 |
| 195 if error_count: | |
| 196 raise errors.MonitoringFailedToFlushAllMetricsError(error_count) | |
| 197 | |
| 198 | 180 |
| 199 def register(metric): | 181 def register(metric): |
| 200 """Adds the metric to the list of metrics sent by flush(). | 182 """Adds the metric to the list of metrics sent by flush(). |
| 201 | 183 |
| 202 This is called automatically by Metric's constructor. | 184 This is called automatically by Metric's constructor. |
| 203 """ | 185 """ |
| 204 # If someone is registering the same metric object twice, that's okay, but | 186 # If someone is registering the same metric object twice, that's okay, but |
| 205 # registering two different metric objects with the same metric name is not. | 187 # registering two different metric objects with the same metric name is not. |
| 206 for m in state.metrics.values(): | 188 for m in state.metrics.values(): |
| 207 if metric == m: | 189 if metric == m: |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 'Last monitoring flush took %f seconds (longer than ' | 252 'Last monitoring flush took %f seconds (longer than ' |
| 271 '--ts-mon-flush-interval-secs = %f seconds)', | 253 '--ts-mon-flush-interval-secs = %f seconds)', |
| 272 flush_duration, self.interval_secs) | 254 flush_duration, self.interval_secs) |
| 273 next_timeout = 0 | 255 next_timeout = 0 |
| 274 | 256 |
| 275 def stop(self): | 257 def stop(self): |
| 276 """Stops the background thread and performs a final flush.""" | 258 """Stops the background thread and performs a final flush.""" |
| 277 | 259 |
| 278 self.stop_event.set() | 260 self.stop_event.set() |
| 279 self.join() | 261 self.join() |
| OLD | NEW |