Fix TelemetryIpc when using tenant_name_discovery=False

Patches [1-2] introduced the variables `project_name` and
`user_name` for the Ceilometer samples that are collected.
The gathering of these attributes is controlled with the use
of the parameter `tenant_name_discovery` , which defaults to
False.

However, when it (the parameter) is set as False, an error will
happen at [2], as the key will not exist in the dictionary.

This patch fixes the described issue.

Without this patch, the error that happens is the follwoing:
```
ERROR ceilometer.pipeline.sample [-] Fail to process notification: KeyError: 'user_name'
ERROR ceilometer.pipeline.sample Traceback (most recent call last):
ERROR ceilometer.pipeline.sample   File "/usr/lib/python3/dist-packages/ceilometer/pipeline/sample.py", line 44, in process_notifications
ERROR ceilometer.pipeline.sample     p(list(self.build_sample(message)))
ERROR ceilometer.pipeline.sample   File "/usr/lib/python3/dist-packages/ceilometer/telemetry/notifications.py", line 34, in build_sample
ERROR ceilometer.pipeline.sample     user_name=sample_dict['user_name'],
ERROR ceilometer.pipeline.sample KeyError: 'user_name'
ERROR ceilometer.pipeline.sample

```

[1] https://review.opendev.org/c/openstack/ceilometer/+/877647
[2] 2a5f63da95
Change-Id: I54a0fd43ac2da1b9a8bcdeae7ad7d160bc07f35b
This commit is contained in:
Rafael Weingärtner 2023-11-21 13:48:19 -03:00
parent 10ee6a9690
commit de91333fed

View File

@ -31,11 +31,16 @@ class TelemetryIpc(endpoint.SampleEndpoint):
unit=sample_dict['counter_unit'],
volume=sample_dict['counter_volume'],
user_id=sample_dict['user_id'],
user_name=sample_dict['user_name'],
project_id=sample_dict['project_id'],
project_name=sample_dict['project_name'],
resource_id=sample_dict['resource_id'],
timestamp=sample_dict['timestamp'],
resource_metadata=sample_dict['resource_metadata'],
source=sample_dict['source'],
id=sample_dict['message_id'])
id=sample_dict['message_id'],
# Project name and username might not be set, depending on the
# configuration `tenant_name_discovery`. Therefore, we cannot
# assume that they exist in the sample dictionary.
user_name=sample_dict.get('user_name'),
project_name=sample_dict.get('project_name')
)