| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Metrics to track with ts_mon and event_mon.""" | 5 """Metrics to track with ts_mon and event_mon.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 import gae_event_mon | 9 import gae_event_mon |
| 10 import gae_ts_mon | 10 import gae_ts_mon |
| 11 | 11 |
| 12 import instances | 12 import instance_group_managers |
| 13 | 13 |
| 14 | 14 |
| 15 # Overrides to create app-global metrics. | 15 # Overrides to create app-global metrics. |
| 16 GLOBAL_TARGET_FIELDS = { | 16 GLOBAL_TARGET_FIELDS = { |
| 17 # Name of the module reporting the metric. | 17 # Name of the module reporting the metric. |
| 18 'job_name': '', | 18 'job_name': '', |
| 19 # Version of the app reporting the metric. | 19 # Version of the app reporting the metric. |
| 20 'hostname': '', | 20 'hostname': '', |
| 21 # ID of the instance reporting the metric. | 21 # ID of the instance reporting the metric. |
| 22 'task_num': 0, | 22 'task_num': 0, |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 GLOBAL_METRICS = { | 26 GLOBAL_METRICS = { |
| 27 'instances': gae_ts_mon.GaugeMetric( | 27 'instances': gae_ts_mon.GaugeMetric( |
| 28 'machine_provider/gce_backend/instances', | 28 'machine_provider/gce_backend/instances', |
| 29 description='Current count of the number of instances.', | 29 description='Current count of the number of instances.', |
| 30 ), | 30 ), |
| 31 } | 31 } |
| 32 | 32 |
| 33 | 33 |
| 34 config_valid = gae_ts_mon.BooleanMetric( | 34 config_valid = gae_ts_mon.BooleanMetric( |
| 35 'machine_provider/gce_backend/config/valid', | 35 'machine_provider/gce_backend/config/valid', |
| 36 description='Whether or not the current config is valid.', | 36 description='Whether or not the current config is valid.', |
| 37 ) | 37 ) |
| 38 | 38 |
| 39 | 39 |
| 40 def compute_global_metrics(): | 40 def compute_global_metrics(): |
| 41 orphaned, total = instances.count_instances() | 41 for name, count in instance_group_managers.count_instances().iteritems(): |
| 42 GLOBAL_METRICS['instances'].set( | 42 logging.info('%s: %s', name, count) |
| 43 orphaned, | 43 GLOBAL_METRICS['instances'].set( |
| 44 fields={ | 44 count, |
| 45 'orphaned': True, | 45 fields={ |
| 46 }, | 46 'instance_template': name, |
| 47 target_fields=GLOBAL_TARGET_FIELDS, | 47 }, |
| 48 ) | 48 target_fields=GLOBAL_TARGET_FIELDS, |
| 49 GLOBAL_METRICS['instances'].set( | 49 ) |
| 50 total - orphaned, | |
| 51 fields={ | |
| 52 'orphaned': False, | |
| 53 }, | |
| 54 target_fields=GLOBAL_TARGET_FIELDS, | |
| 55 ) | |
| 56 | 50 |
| 57 | 51 |
| 58 def initialize(): | 52 def initialize(): |
| 59 gae_ts_mon.register_global_metrics(GLOBAL_METRICS.values()) | 53 gae_ts_mon.register_global_metrics(GLOBAL_METRICS.values()) |
| 60 gae_ts_mon.register_global_metrics_callback( | 54 gae_ts_mon.register_global_metrics_callback( |
| 61 'callback', compute_global_metrics) | 55 'callback', compute_global_metrics) |
| 62 | 56 |
| 63 | 57 |
| 64 def send_machine_event(state, hostname): | 58 def send_machine_event(state, hostname): |
| 65 """Sends an event_mon event about a GCE instance. | 59 """Sends an event_mon event about a GCE instance. |
| 66 | 60 |
| 67 Args: | 61 Args: |
| 68 state: gae_event_mon.ChromeInfraEvent.GCEBackendMachineState. | 62 state: gae_event_mon.ChromeInfraEvent.GCEBackendMachineState. |
| 69 hostname: Name of the GCE instance this event is for. | 63 hostname: Name of the GCE instance this event is for. |
| 70 """ | 64 """ |
| 71 state = gae_event_mon.MachineProviderEvent.GCEBackendMachineState.Value(state) | 65 state = gae_event_mon.MachineProviderEvent.GCEBackendMachineState.Value(state) |
| 72 event = gae_event_mon.Event('POINT') | 66 event = gae_event_mon.Event('POINT') |
| 73 event.proto.event_source.host_name = hostname | 67 event.proto.event_source.host_name = hostname |
| 74 event.proto.machine_provider_event.gce_backend_state = state | 68 event.proto.machine_provider_event.gce_backend_state = state |
| 75 logging.info('Sending event: %s', event.proto) | 69 logging.info('Sending event: %s', event.proto) |
| 76 event.send() | 70 event.send() |
| OLD | NEW |