-
Notifications
You must be signed in to change notification settings - Fork 10
/
trawler.py
executable file
·219 lines (198 loc) · 8.77 KB
/
trawler.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python3
""" Main trawler application """
import os
import time
import threading
import ssl
import alog
import click
from prometheus_client import start_http_server, Gauge, Counter, make_wsgi_app
import yaml
from certs_net import CertsNet
from apiconnect_net import APIConnectNet
from datapower_net import DataPowerNet
from manager_net import ManagerNet
from analytics_net import AnalyticsNet
import metrics_graphite
logger = alog.use_channel("trawler")
class Trawler(object):
""" The main trawling """
config = {
'prometheus': {'enabled': False},
'graphite': {'enabled': False}
}
# Default looping frequency
frequency = 10
# Default to True, but detected unless overridden in config
use_kubeconfig = True
# Default path for secrets in container build - override with envvar SECRETS
secrets_path = '/app/secrets'
graphite = None
gauges = {}
def __init__(self, config_file=None):
self.secrets_path = os.getenv('SECRETS', self.secrets_path)
if config_file:
self.load_config(config_file)
if 'logging' in self.config:
alog.configure(
default_level=self.config['logging'].get('level', 'debug'),
filters=self.config['logging'].get('filters', None),
formatter=self.config['logging'].get('format', 'json')
)
else:
alog.configure(default_level='info', formatter='json')
self.logger = alog.use_channel("trawler")
if self.config['prometheus']['enabled']:
port = self.config['prometheus'].get('port')
logger.info('Starting prometheus http port at http://0.0.0.0:{}'.format(port))
start_http_server(port)
if 'graphite' in self.config:
if self.config['graphite']['enabled']:
self.graphite = metrics_graphite.instance(self.config['graphite'])
use_kubeconfig = False
if 'trawler' in self.config:
use_kubeconfig = self.config['trawler'].get('use_kubeconfig')
self.frequency = self.config['trawler'].get('frequency', self.frequency)
if use_kubeconfig:
# Explicit override that we want to use kubeconfig rather than in cluster k8s comms
self.use_kubeconfig = True
else:
# Check for KUBERNETES_SERVICE_HOST to determine if running within kubernetes
if os.getenv('KUBERNETES_SERVICE_HOST'):
self.use_kubeconfig = False
def read_secret(self, key):
""" Helper function read secrets from mounted k8s secrets """
try:
with open("{}/{}".format(self.secrets_path, key), 'r', encoding='utf-8') as secret:
value = secret.read().rstrip()
return value
except FileNotFoundError as not_found_exception:
logger.exception(not_found_exception)
return None
def load_config(self, config_file):
""" Load trawler config """
try:
with open(config_file, 'r', encoding='utf-8') as config_yaml:
self.config = yaml.safe_load(config_yaml)
except FileNotFoundError as not_found_exception:
logger.exception(not_found_exception)
exit(2)
def set_gauge(self, component, target_name, value, pod_name=None, labels=None):
""" Set or create prometheus gauge """
if not labels:
labels = {}
if pod_name:
labels['pod'] = pod_name
if 'labels' in self.config['prometheus']:
labels = {**self.config['prometheus']['labels'], **labels}
logger.debug("Entering set_gauge - params: (%s, %s, %s, %s)",
component, target_name, value, pod_name)
logger.debug(labels)
if isinstance(value, (float, int)):
target_name = target_name.replace('-', '_')
if self.config['prometheus']['enabled']:
prometheus_target = "{}_{}".format(component, target_name.replace('.', '_'))
if prometheus_target not in self.gauges:
logger.info("Creating gauge %s", prometheus_target)
if labels:
self.gauges[prometheus_target] = Gauge(
prometheus_target,
prometheus_target, labels.keys())
else:
self.gauges[prometheus_target] = Gauge(
prometheus_target,
prometheus_target)
logger.debug("Setting gauge %s to %f",
self.gauges[prometheus_target]._name, value)
try:
if labels:
self.gauges[prometheus_target].labels(**labels).set(value)
else:
self.gauges[prometheus_target].set(value)
except ValueError as value_exception:
self.logger.exception(value_exception)
if 'graphite' in self.config:
if self.config['graphite']['enabled']:
if pod_name:
metric_name = "{}.{}.{}".format(component, pod_name, target_name)
else:
metric_name = "{}.{}".format(component, target_name)
self.graphite.stage(metric_name, value)
def inc_counter(self, component, target_name, value, pod_name=None, labels=None):
""" Set or increase prometheus counter """
if not labels:
labels = {}
if pod_name:
labels['pod'] = pod_name
if 'labels' in self.config['prometheus']:
labels = {**self.config['prometheus']['labels'], **labels}
logger.debug("Entering inc_counter - params: (%s, %s, %s, %s)",
component, target_name, value, pod_name)
logger.debug(labels)
if isinstance(value, (float, int)):
target_name = target_name.replace('-', '_')
if self.config['prometheus']['enabled']:
prometheus_target = "{}_{}".format(component, target_name.replace('.', '_'))
if prometheus_target not in self.gauges:
logger.info("Creating counter %s", prometheus_target)
if labels:
self.gauges[prometheus_target] = Counter(
prometheus_target,
prometheus_target, labels.keys())
else:
self.gauges[prometheus_target] = Counter(
prometheus_target,
prometheus_target)
logger.debug("Setting gauge %s to %f",
self.gauges[prometheus_target]._name, value)
if labels:
self.gauges[prometheus_target].labels(**labels).inc()
else:
self.gauges[prometheus_target].inc()
if self.config['graphite']['enabled']:
if pod_name:
metric_name = "{}.{}.{}".format(component, pod_name, target_name)
else:
metric_name = "{}.{}".format(component, target_name)
self.graphite.stage(metric_name, value)
def is_enabled(self, net_name):
""" is net in config and enabled """
if net_name in self.config['nets']:
if self.config['nets'][net_name].get('enabled', True):
return True
return False
@alog.timed_function(logger.trace)
def trawl_metrics(self):
""" Main loop to trawl for metrics """
# Initialise
logger.info("Laying nets...")
nets = []
if self.is_enabled('certs'):
nets.append(CertsNet(self.config['nets']['certs'], self))
if self.is_enabled('apiconnect'):
nets.append(APIConnectNet(self.config['nets']['apiconnect'], self))
if self.is_enabled('datapower'):
nets.append(DataPowerNet(self.config['nets']['datapower'], self))
if self.is_enabled('manager'):
nets.append(ManagerNet(self.config['nets']['manager'], self))
if self.is_enabled('analytics'):
nets.append(AnalyticsNet(self.config['nets']['analytics'], self))
while True:
logger.info("Trawling for metrics...")
for net in nets:
net.fish()
if self.graphite:
self.graphite.store()
time.sleep(self.frequency)
@click.command()
@click.version_option()
@click.option('-c', '--config', required=False, envvar='CONFIG',
help="Specifies an alternative config file",
default=None,
type=click.Path())
def cli(config=None):
""" run main trawler application """
trawler = Trawler(config)
trawler.trawl_metrics()
if __name__ == '__main__':
cli()