-
Notifications
You must be signed in to change notification settings - Fork 16
/
scrapygraphite.py
66 lines (51 loc) · 2.67 KB
/
scrapygraphite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''Copyright 2011 Julien Duponchelle
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''
from scrapy.statscol import StatsCollector
from galena import Galena
GRAPHITE_HOST = 'localhost'
GRAPHITE_PORT = 2003
GRAPHITE_IGNOREKEYS = ["envinfo/pid"]
class GraphiteStatsCollector(StatsCollector):
def __init__(self, crawler):
super(GraphiteStatsCollector, self).__init__(crawler)
host = crawler.settings.get("GRAPHITE_HOST", GRAPHITE_HOST)
port = crawler.settings.get("GRAPHITE_PORT", GRAPHITE_PORT)
self.ignore_keys = crawler.settings.get("GRAPHITE_IGNOREKEYS",
GRAPHITE_IGNOREKEYS)
self.crawler = crawler
self._galena = Galena(host=host, port=port)
def _get_stats_key(self, spider, key):
if spider is not None:
return "scrapy/spider/%s/%s" % (spider.name, key)
return "scrapy/%s" % (key)
def set_value(self, key, value, spider=None):
super(GraphiteStatsCollector, self).set_value(key, value, spider)
self._set_value(key, value, spider)
def _set_value(self, key, value, spider):
if isinstance(value, (int, float)) and key not in self.ignore_keys:
k = self._get_stats_key(spider, key)
self._galena.send(k, value)
def inc_value(self, key, count=1, start=0, spider=None):
super(GraphiteStatsCollector, self).inc_value(key, count, start, spider)
self._galena.send(self._get_stats_key(spider, key),
self.crawler.stats.get_value(key))
def max_value(self, key, value, spider=None):
super(GraphiteStatsCollector, self).max_value(key, value, spider)
self._galena.send(self._get_stats_key(spider, key),
self.crawler.stats.get_value(key))
def min_value(self, key, value, spider=None):
super(GraphiteStatsCollector, self).min_value(key, value, spider)
self._galena.send(self._get_stats_key(spider, key),
self.crawler.stats.get_value(key))
def set_stats(self, stats, spider=None):
super(GraphiteStatsCollector, self).set_stats(stats, spider)
for key in stats:
self._set_value(key, stats[key], spider)