diff options
author | Yadnesh Kulkarni <ykulkarn@redhat.com> | 2022-08-01 10:42:02 +0000 |
---|---|---|
committer | Yadnesh Kulkarni <ykulkarn@redhat.com> | 2022-09-13 11:15:51 +0000 |
commit | 79454d6b22787627ae6239aa7b2707101ba30212 (patch) | |
tree | cba04acbc58b7ee581eb0001837f6216940b2a33 /ceilometer/cache_utils.py | |
parent | 6f88ee6be9a3ae5c78587f1a2bc8cd2e9a5a2fa7 (diff) | |
download | ceilometer-79454d6b22787627ae6239aa7b2707101ba30212.tar.gz |
Add user/project names to polled samples
Project and user names would be first fetched from cache, if not found,
they will be requested from keystone and then cached. Using cache will
significanlty reduce the number of calls made to keystone.
If ceilometer is configured with no caching backend, the results
will always be fetched by querying request to keystone.
A new config option, `tenant_name_discovery` is introduced
to operate this feature. This feature is optional and is disabled by default.
No attempts to identify names will be made if uuids are found to be `None`.
Signed-off-by: Yadnesh Kulkarni <ykulkarn@redhat.com>
Change-Id: Iee5dbf09a1fd3ac571746fc66d2683eb8e6a1b27
Diffstat (limited to 'ceilometer/cache_utils.py')
-rw-r--r-- | ceilometer/cache_utils.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/ceilometer/cache_utils.py b/ceilometer/cache_utils.py new file mode 100644 index 00000000..55a9e263 --- /dev/null +++ b/ceilometer/cache_utils.py @@ -0,0 +1,53 @@ +# +# Copyright 2022 Red Hat, Inc +# +# 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. + +"""Simple wrapper for oslo_cache.""" + + +from oslo_cache import core as cache + + +class CacheClient(object): + def __init__(self, region): + self.region = region + + def get(self, key): + value = self.region.get(key) + if value == cache.NO_VALUE: + return None + return value + + def set(self, key, value): + return self.region.set(key, value) + + def delete(self, key): + return self.region.delete(key) + + +def get_client(conf, expiration_time=0): + cache.configure(conf) + if conf.cache.enabled: + return CacheClient(_get_default_cache_region( + conf, + expiration_time=expiration_time + )) + + +def _get_default_cache_region(conf, expiration_time): + region = cache.create_region() + if expiration_time != 0: + conf.cache.expiration_time = expiration_time + cache.configure_cache_region(conf, region) + return region |