summaryrefslogtreecommitdiffstats
path: root/clover/collector/db/redisops.py
blob: e80c41780423f80493957a69dc762f08c0609354 (plain)
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
# Copyright (c) Authors of Clover
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

import redis
import logging

REDIS_HOST = 'redis'
# REDIS_HOST = '10.244.0.85'


class RedisOps:

    def __init__(self, host=REDIS_HOST):
        logging.basicConfig(filename='redisops.log',
                            level=logging.DEBUG)
        try:
            self.r = redis.StrictRedis(host=host, port=6379, db=0)
        except Exception as e:
            logging.debug(e)

    def init_services(self, skey='visibility_services'):
        service_names = ['http_lb', 'proxy_access_control']
        for s in service_names:
            self.r.sadd(skey, s)

    def init_metrics(self, pkey='metric_prefixes', skey='metric_suffixes'):
        metric_prefixes = ['envoy_cluster_out_', 'envoy_cluster_in_']
        metric_suffixes = [
            '_default_svc_cluster_local_http_internal_upstream_rq_2xx',
            '_default_svc_cluster_local_http_upstream_cx_active']
        for p in metric_prefixes:
            self.r.sadd(pkey, p)
        for s in metric_suffixes:
            self.r.sadd(skey, s)

    def get_services(self, skey='visibility_services'):
        services = self.r.smembers(skey)
        return services

    def get_metrics(self, pkey='metric_prefixes', skey='metric_suffixes'):
        prefixes = self.r.smembers(pkey)
        suffixes = self.r.smembers(skey)
        return prefixes, suffixes


def main():
    r = RedisOps()
    r.init_services()
    r.init_metrics()
    r.get_services()
    r.get_metrics()


if __name__ == '__main__':
    main()