Add Extra Tags to Cloud Resource Reported Data
1. Background
In general, the collector extracts only some universally important attributes from the cloud vendor's resources and uses them as tags. This is not sufficient for some users. This article will introduce how to supplement additional tags to the data collected (before reporting).
2. Solution
Without modifying the official collector, the collector itself provides an after_collect
parameter. Users can assign a function to this parameter to perform secondary processing on the collected data, including adding extra tags.
Python | |
---|---|
1 2 3 4 5 6 7 |
|
The above example omits irrelevant configurations; focus on the handler
function. This function supports only one parameter point
. point
is the data that the collector is about to report. The data structure can be referenced in the relevant collector documentation under "Data Reporting Format". It can be confirmed that point
must include three fields: measurement
, tags
, and fields
. For those who want to learn more, you can refer to the relevant line protocol documentation. We mainly focus on the point.tags
field, inserting the key-value pairs to be supplemented into tags
. In the example, it is equivalent to adding a key-value pair to point.tags
with the key
being origin
and the value
being shanghai
.
3. Case Studies
Supplementing AWS Console-configured EC2 tags
to the tags
of the EC2 object data collected by the collector.
Scenario One: Directly extract the Tags
field from point.fields
and add it to point.tags
Python | |
---|---|
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 |
|
Scenario Two: Not all collectors have a Tags
field in their point.fields
(continuously supported...). If it is not supported, you need to obtain it from the cloud vendor’s open API (or possibly the customer's own API):
Python | |
---|---|
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 |
|
4. Key Points to Note
- In cloud product collectors, custom object tags are automatically added to associated Metrics tags. Therefore, if you enable both the custom object collector and the cloud monitoring collector and need to supplement tags, you only need to supplement the object collector.
- When adding tags to the reported data of the collector, pay special attention to certain fields that cannot be overwritten, such as the
name
field of custom objects. It is recommended to follow the examples provided: if the original data tags contain the same key, do not add them again to prevent unexpected situations. - The function assigned to
after_collect
only accepts one parameterpoint
. After processingpoint
, the function must return one or morepoints
. If there is no return or an error occurs during processing, the raw data will be reported without any function handling. When theafter_collect
function is defined but invalid, first check for this possibility.