~landscape/txstatsd/staging

« back to all changes in this revision

Viewing changes to txstatsd/server/processor.py

  • Committer: Andreas Hasenack
  • Date: 2011-09-15 14:26:40 UTC
  • mfrom: (10.2.14 trunk)
  • Revision ID: andreas@canonical.com-20110915142640-osj3o8l3xyirqb6h
Tags: 11.08.2, 11.09.1
Merging from trunk for staging deployment of 11.08.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
SLASHES = re.compile("\/+")
13
13
NON_ALNUM = re.compile("[^a-zA-Z_\-0-9\.]")
14
14
RATE = re.compile("^@([\d\.]+)")
15
 
COUNTERS_MESSAGE = (
16
 
    "stats.%(key)s %(value)s %(timestamp)s\n"
17
 
    "stats_counts.%(key)s %(count)s %(timestamp)s\n")
18
 
TIMERS_MESSAGE = (
19
 
    "stats.timers.%(key)s.mean %(mean)s %(timestamp)s\n"
20
 
    "stats.timers.%(key)s.upper %(upper)s %(timestamp)s\n"
21
 
    "stats.timers.%(key)s.upper_%(percent)s %(threshold_upper)s"
22
 
        " %(timestamp)s\n"
23
 
    "stats.timers.%(key)s.lower %(lower)s %(timestamp)s\n"
24
 
    "stats.timers.%(key)s.count %(count)s %(timestamp)s\n")
25
 
 
26
 
GAUGE_METRIC_MESSAGE = (
27
 
    "stats.gauge.%(key)s.value %(value)s %(timestamp)s\n")
28
15
 
29
16
 
30
17
def normalize_key(key):
39
26
 
40
27
 
41
28
class MessageProcessor(object):
 
29
    """
 
30
    This C{MessageProcessor} produces StatsD-compliant messages
 
31
    for publishing to a Graphite server.
 
32
    Metrics behaviour that varies from StatsD should be placed in
 
33
    some specialised C{MessageProcessor} (see L{ConfigurableMessageProcessor
 
34
    <txstatsd.server.configurableprocessor.ConfigurableMessageProcessor>}).
 
35
    """
 
36
 
 
37
    COUNTERS_MESSAGE = (
 
38
        "stats.%(key)s %(value)s %(timestamp)s\n"
 
39
        "stats_counts.%(key)s %(count)s %(timestamp)s\n")
 
40
 
 
41
    TIMERS_MESSAGE = (
 
42
        "stats.timers.%(key)s.mean %(mean)s %(timestamp)s\n"
 
43
        "stats.timers.%(key)s.upper %(upper)s %(timestamp)s\n"
 
44
        "stats.timers.%(key)s.upper_%(percent)s %(threshold_upper)s"
 
45
            " %(timestamp)s\n"
 
46
        "stats.timers.%(key)s.lower %(lower)s %(timestamp)s\n"
 
47
        "stats.timers.%(key)s.count %(count)s %(timestamp)s\n")
 
48
 
 
49
    GAUGE_METRIC_MESSAGE = (
 
50
        "stats.gauge.%(key)s.value %(value)s %(timestamp)s\n")
42
51
 
43
52
    def __init__(self, time_function=time.time):
44
53
        self.time_function = time_function
 
54
 
 
55
        self.counters_message = MessageProcessor.COUNTERS_MESSAGE
 
56
        self.timers_message = MessageProcessor.TIMERS_MESSAGE
 
57
        self.gauge_metric_message = MessageProcessor.GAUGE_METRIC_MESSAGE
 
58
 
45
59
        self.timer_metrics = {}
46
60
        self.counter_metrics = {}
47
61
        self.gauge_metrics = deque()
100
114
            if match is None:
101
115
                return self.fail(message)
102
116
            rate = match.group(1)
 
117
 
 
118
        self.compose_counter_metric(key, value, rate)
 
119
 
 
120
    def compose_counter_metric(self, key, value, rate):
103
121
        if key not in self.counter_metrics:
104
122
            self.counter_metrics[key] = 0
105
123
        self.counter_metrics[key] += value * (1 / float(rate))
125
143
        except (TypeError, ValueError):
126
144
            self.fail(message)
127
145
 
 
146
        self.compose_meter_metric(key, value)
 
147
 
 
148
    def compose_meter_metric(self, key, value):
128
149
        if not key in self.meter_metrics:
129
 
            metric = MeterMetricReporter(key, self.time_function)
 
150
            metric = MeterMetricReporter(key, self.time_function,
 
151
                                         prefix="stats.meter")
130
152
            self.meter_metrics[key] = metric
131
153
        self.meter_metrics[key].mark(value)
132
154
 
171
193
            self.counter_metrics[key] = 0
172
194
 
173
195
            value = count / interval
174
 
            message = COUNTERS_MESSAGE % {
 
196
            message = self.counters_message % {
175
197
                "key": key,
176
198
                "value": value,
177
199
                "count": count,
205
227
                    threshold_upper = timers[-1]
206
228
                    mean = sum(timers) / index
207
229
 
208
 
                message = TIMERS_MESSAGE % {
 
230
                message = self.timers_message % {
209
231
                    "key": key,
210
232
                    "mean": mean,
211
233
                    "upper": upper,
226
248
            value = metric[0]
227
249
            key = metric[1]
228
250
 
229
 
            message = GAUGE_METRIC_MESSAGE % {
 
251
            message = self.gauge_metric_message % {
230
252
                "key": key,
231
253
                "value": value,
232
254
                "timestamp": timestamp}