OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 """Classes representing the monitoring interface for tasks or devices. | |
6 | |
7 Usage: | |
8 # symlink appengine/modules/gae_ts_mon into the top level directory | |
9 # of your appengine app | |
10 | |
11 import gae_ts_mon | |
12 | |
13 # Sets up default target | |
14 gae_ts_mon.initialize(job_name='job', instance='12d2e1', | |
Sergey Berezin
2015/08/18 00:40:38
nit: is the instance value supposed to be a consta
jshu
2015/08/18 01:15:55
should be a real instance id, which is an int. fix
Sergey Berezin
2015/08/18 17:31:38
Two comments then:
1. In README, let's use <insta
jshu
2015/08/20 00:23:34
fixed
| |
15 service_name='service', endpoint='endpoint') | |
16 | |
17 # Will use the default Target set up with initialize | |
18 count_metric = gae_ts_mon.CounterMetric('my/metric/name', fields={}) | |
19 count_metric.set(0) | |
20 for x in range(100): | |
21 count_metric.increment() | |
22 | |
23 # Use a custom Target: | |
24 t = ts_mon.TaskTarget('service', 'job', 'region', 'host') | |
25 g_metric = ts_mon.GaugeMetric('/my/metric/name2', fields={'asdf': 'qwer'}, | |
26 target=t) | |
Sergey Berezin
2015/08/18 00:40:37
nit: indentation.
| |
27 g_metric.set(5) | |
28 | |
29 # Flush (send metrics to monarch) | |
30 gae_ts_mon.flush() | |
Sergey Berezin
2015/08/18 00:40:38
nit: I believe this should be called automatically
jshu
2015/08/18 01:15:55
called from a cron job. leaving that in to give th
Sergey Berezin
2015/08/18 17:31:38
I'd still mention what you said in the comment; ot
| |
31 | |
32 """ | |
33 | |
34 import logging | |
35 import os | |
36 import random | |
37 import threading | |
38 import time | |
39 | |
40 from proto import metrics_pb2 | |
41 | |
42 from common import errors | |
43 | |
44 # The maximum number of MetricsData messages to include in each HTTP request. | |
45 # MetricsCollections larger than this will be split into multiple requests. | |
46 METRICS_DATA_LENGTH_LIMIT = 5000 | |
Sergey Berezin
2015/08/18 00:40:38
nit: we are reducing this to 1000 - see https://co
| |
47 | |
48 | |
49 class State(object): | |
50 """Package-level state is stored here so that it is easily accessible. | |
51 | |
52 Configuration is kept in this one object at the global level so that all | |
53 libraries in use by the same tool or service can all take advantage of the | |
54 same configuration. | |
55 """ | |
56 | |
57 def __init__(self): | |
58 # The Monitor object that will be used to send all metrics. | |
59 self.global_monitor = None | |
60 # The Target object that will be paired with all metrics that don't supply | |
61 # their own. | |
62 self.default_target = None | |
63 # The flush mode being used to control when metrics are pushed. | |
64 self.flush_mode = None | |
65 # All metrics created by this application. | |
66 self.metrics = set() | |
67 | |
68 state = State() | |
69 | |
70 | |
71 def send(metric): | |
72 """Send a single metric to the monitoring api. | |
73 | |
74 This is called automatically by Metric.set - you don't need to call it | |
75 manually. | |
76 """ | |
77 if state.flush_mode != 'all': | |
78 return | |
79 | |
80 if not state.global_monitor: | |
81 raise errors.MonitoringNoConfiguredMonitorError(metric._name) | |
82 | |
83 proto = metrics_pb2.MetricsCollection() | |
84 metric.serialize_to(proto, default_target=state.default_target) | |
85 state.global_monitor.send(proto) | |
86 | |
87 | |
88 def flush(): | |
89 """Send all metrics that are registered in the application.""" | |
90 if not state.global_monitor: | |
91 raise errors.MonitoringNoConfiguredMonitorError(None) | |
92 | |
93 proto = metrics_pb2.MetricsCollection() | |
94 | |
95 def loop_action(proto): | |
96 if len(proto.data) >= METRICS_DATA_LENGTH_LIMIT: | |
97 state.global_monitor.send(proto) | |
98 del proto.data[:] | |
99 | |
100 for metric in state.metrics: | |
101 metric.serialize_to(proto, default_target=state.default_target, | |
102 loop_action=loop_action) | |
103 | |
104 state.global_monitor.send(proto) | |
105 | |
106 | |
107 def register(metric): | |
108 """Adds the metric to the list of metrics sent by flush(). | |
109 | |
110 This is called automatically by Metric's constructor. | |
111 """ | |
112 # If someone is registering the same metric object twice, that's okay, but | |
113 # registering two different metric objects with the same metric name is not. | |
114 if metric in state.metrics: | |
115 return | |
116 if any([metric._name == m._name for m in state.metrics]): | |
117 raise errors.MonitoringDuplicateRegistrationError(metric._name) | |
118 | |
119 state.metrics.add(metric) | |
120 | |
121 | |
122 def unregister(metric): | |
123 """Removes the metric from the list of metrics sent by flush().""" | |
124 state.metrics.remove(metric) | |
OLD | NEW |