11import collectd
22
33from .. import protocol
4- from .. import types
54from . import aggregator
5+ from ..client import internal_types
66
77import logging
88log = logging .getLogger (__name__ )
99
1010
11+ summarizers = {}
12+
13+
14+ def summarizes (protocol_type ):
15+ def decorate (fn ):
16+ summarizers [protocol_type ] = fn
17+ return fn
18+
19+ return decorate
20+
21+
1122class Receiver (object ):
1223 def __init__ (self , plugin = "sqlalchemy" ):
1324 self .plugin = plugin
14- self .types = types_ = [
15- types .pool ,
16- types .checkouts ,
17- types .commits ,
18- types .rollbacks ,
19- types .invalidated ,
20- types .transactions
25+ self .internal_types = [
26+ internal_types .pool ,
27+ internal_types .totals
2128 ]
22- self .message_receiver = protocol .MessageReceiver (* types_ )
29+ self .message_receiver = protocol .MessageReceiver (* self . internal_types )
2330
2431 self .aggregator = aggregator .Aggregator (
25- [type_ .name for type_ in types_ ]
32+ [type_ .name for type_ in self . internal_types ]
2633 )
2734
2835 def receive (self , connection ):
@@ -39,42 +46,72 @@ def receive(self, connection):
3946 )
4047
4148 def summarize (self , timestamp ):
42- for type_ in self .types :
43- self ._summarize_for_type (type_ , timestamp )
44-
45- def _summarize_for_type (self , type_ , timestamp ):
46- values = collectd .Values (
47- type = type_ .name ,
48- plugin = self .plugin ,
49- time = timestamp ,
50- interval = self .aggregator .interval
51- )
52- for hostname , progname , stats in \
53- self .aggregator .get_stats_by_progname (
54- type_ .name , timestamp , sum ):
49+ for type_ in self .internal_types :
50+ summarizer = summarizers .get (type_ , None )
51+ if summarizer :
52+ summarizer (self , type_ , timestamp )
53+
54+
55+ @summarizes (internal_types .pool )
56+ def _summarize_pool_stats (receiver , type_ , timestamp ):
57+ values = collectd .Values (
58+ type = "count" ,
59+ plugin = receiver .plugin ,
60+ time = timestamp ,
61+ interval = receiver .aggregator .interval
62+ )
63+ for hostname , progname , numrecs , stats in \
64+ receiver .aggregator .get_stats_by_progname (
65+ type_ .name , timestamp , sum ):
66+ for name , value in zip (type_ .names , stats ):
5567 values .dispatch (
56- type_instance = "sum" , host = hostname , plugin_instance = progname ,
57- values = stats
68+ host = hostname , plugin_instance = progname ,
69+ type_instance = name ,
70+ values = [value ]
5871 )
5972
60- for hostname , stats in self .aggregator .get_stats_by_hostname (
61- type_ .name , timestamp , sum ):
73+ values .dispatch (
74+ host = hostname , plugin_instance = progname ,
75+ type_instance = "numprocs" , values = [numrecs ])
76+
77+ for hostname , numrecs , stats in receiver .aggregator .get_stats_by_hostname (
78+ type_ .name , timestamp , sum ):
79+ for name , value in zip (type_ .names , stats ):
6280 values .dispatch (
63- type_instance = "sum" , host = hostname , plugin_instance = "all" ,
64- values = stats
81+ host = hostname , plugin_instance = "host" ,
82+ type_instance = name ,
83+ values = [value ]
6584 )
85+ values .dispatch (
86+ host = hostname , plugin_instance = "host" ,
87+ type_instance = "numprocs" , values = [numrecs ])
6688
67- for hostname , progname , stats in self .aggregator .get_stats_by_progname (
68- type_ .name , timestamp , aggregator .avg ):
89+
90+ @summarizes (internal_types .totals )
91+ def _summarize_totals (receiver , type_ , timestamp ):
92+ values = collectd .Values (
93+ type = "derive" ,
94+ plugin = receiver .plugin ,
95+ time = timestamp ,
96+ interval = receiver .aggregator .interval
97+ )
98+
99+ for hostname , progname , numrecs , stats in \
100+ receiver .aggregator .get_stats_by_progname (
101+ type_ .name , timestamp , sum ):
102+ for name , value in zip (type_ .names , stats ):
69103 values .dispatch (
70- type_instance = "avg" , host = hostname , plugin_instance = progname ,
71- values = stats
104+ host = hostname , plugin_instance = progname ,
105+ type_instance = name ,
106+ values = [value ]
72107 )
73108
74- for hostname , stats in self .aggregator .get_stats_by_hostname (
75- type_ .name , timestamp , aggregator .avg ):
109+ for hostname , numrecs , stats in receiver .aggregator .get_stats_by_hostname (
110+ type_ .name , timestamp , sum ):
111+ for name , value in zip (type_ .names , stats ):
76112 values .dispatch (
77- type_instance = "avg" , host = hostname , plugin_instance = "all" ,
78- values = stats
113+ host = hostname , plugin_instance = "host" ,
114+ type_instance = name ,
115+ values = [value ]
79116 )
80117
0 commit comments