Esempio n. 1
0
def test_create_key():
    with pytest.raises(TypeError):
        create_key(['un', 'hashable'], 'this should raise')

    assert create_key('get schwifty',
                      'ztm') == 'tscached:ztm:6fed3992d23c711d8c21d354f6dc46e9'
    assert create_key(json.dumps(
        {}), 'lulz') == 'tscached:lulz:99914b932bd37a50b983c5e7c90ae93b'
Esempio n. 2
0
def metadata_caching(config, name, endpoint, post_data=None):
    """ Encapsulate stupid-simple cache logic for Kairos "metadata" endpoints.
        config: nested dict loaded from the 'tscached' section of a yaml file.
        name: string, used as a part of redis keying.
        endpoint: string, the corresponding kairosdb endpoint.
        post_data: None or string. overrides default GET proxy behavior. implies custom keying.
        returns: 2-tuple: (content, HTTP code)
    """
    if post_data:
        redis_key = create_key(post_data, name)
    else:
        redis_key = 'tscached:' + name

    redis_client = redis.StrictRedis(host=config['redis']['host'], port=config['redis']['port'])
    try:
        get_result = redis_client.get(redis_key)
    except redis.exceptions.RedisError as e:
        logging.error('RedisError: ' + e.message)
        get_result = False  # proxy through to kairos even if redis is broken

    if get_result:  # hit. no need to process the JSON blob, so don't!
        logging.info('Meta Endpoint HIT: %s' % redis_key)
        return get_result, 200
    else:
        logging.info('Meta Endpoint MISS: %s' % redis_key)
        url = 'http://%s:%s%s' % (config['kairosdb']['host'], config['kairosdb']['port'], endpoint)

        try:
            if post_data:
                kairos_result = requests.post(url, data=post_data)
            else:
                kairos_result = requests.get(url)

        except requests.exceptions.RequestException as e:
            logging.error('BackendQueryFailure: %s' % e.message)
            return json.dumps({'error': 'Could not connect to KairosDB: %s' % e.message}), 500

        if kairos_result.status_code / 100 != 2:
            # propagate the kairos message to the user along with its error code.
            value = json.loads(kairos_result.text)
            value_message = ', '.join(value.get('errors', ['No message given']))
            message = 'Meta Endpoint: %s: KairosDB responded %d: %s' % (redis_key,
                                                                        kairos_result.status_code,
                                                                        value_message)
            return json.dumps({'error': message}), 500
        else:
            # kairos response seems to be okay
            expiry = config['expiry'].get(name, 300)  # 5 minute default

            try:
                set_result = redis_client.set(redis_key, kairos_result.text, ex=expiry)
                if not set_result:
                    logging.error('Meta Endpoint: %s: Cache SET failed: %s' % (redis_key, set_result))
            except redis.exceptions.RedisError as e:
                # Eat the Redis exception - turns these endpoints into straight proxies.
                logging.error('RedisError: ' + e.message)

        return kairos_result.text, kairos_result.status_code, {'Content-Type': 'application/json'}
Esempio n. 3
0
def test_create_key():
    with pytest.raises(TypeError):
        create_key(['un', 'hashable'], 'this should raise')

    assert create_key('get schwifty', 'ztm') == 'tscached:ztm:6fed3992d23c711d8c21d354f6dc46e9'
    assert create_key(json.dumps({}), 'lulz') == 'tscached:lulz:99914b932bd37a50b983c5e7c90ae93b'