Skip to content

Customizing Health Functions with Func


TrueWatch supports customizing health calculation logic through the Func platform. You can write your own functions to freely calculate health based on data such as DQL, component entities, and events, replacing the default platform algorithm.


Writing the Function Script

Create a new function script in Func. The function name can be customized, but the following input and output constraints must be satisfied.

Input Parameters:

Field Required Description
entity Yes The list of entities for which health is currently calculated. Each entity object contains basic information such as the full URN, entity name, type, and tags

Output Format:

Iterate through the input parameter entity and calculate health for each entity. The function must return an object whose keys are the full URN of each entity and whose values are objects containing the following fields.

Field Required Description
health_status Yes Health status. Fixed values: healthy (normal), warning (attention), critical (critical), unknown (unknown)
health_score Yes Health score from 0 to 100. Return null when it cannot be calculated
{
  "实体A的URN": {"health_status": "healthy", "health_score": 95},
  "实体B的URN": {"health_status": "warning", "health_score": 65}
}

Function Example:

'''
Example of a custom health function

Notes:
1. The function name can be customized; the input parameter must include entity
2. entity is the list of entities for which health is currently calculated
3. Inside the function, data such as DQL, components, and events can be freely queried and calculated
4. Iterate through entity and calculate for each entity, returning the result with the entity's full URN as the key
5. The result for each entity must include health_status and health_score
'''

@DFF.API('自定义系统健康度', category='dataPlatform.entityHealthFunc')
def myCustomHealth(entity):
    result = {}

    for item in entity:
        # Query component entities, associated alerts, and other data based on the current entity
        # Custom calculation logic...
        urn = item['urn']
        result[urn] = {
            'health_status': 'healthy',
            'health_score': 95
        }

    return result

Adding a Function Category

After writing, you must add a category tag to the function; otherwise, the function will not appear in the selection list of the health configuration.

The category tag is fixed as:

category='dataPlatform.entityHealthFunc'

Publishing the Function

After writing the code, click Publish. Unpublished functions will not be displayed in the health configuration.


Using the Function in Health Configuration

After successful publishing, go to Unified Catalog > Manage Entity Types, click "Health Configuration" in the action column, and select "Custom Function".

The dropdown list will then display all published functions categorized as dataPlatform.entityHealthFunc. Select the function you created and save to apply it.


FAQ

Why is my function not appearing in the dropdown list of the health configuration?

Check the following three points:

  1. Whether the function has been published;
  2. Whether category='dataPlatform.entityHealthFunc' has been added;
  3. Whether the function is correctly registered with the @DFF.API decorator.

What happens if the function fails to execute?

The platform validates the function output. If the output format does not meet the requirements or an execution exception occurs, the system's health will be displayed as "unknown".


What data can be used inside the function?

Inside the function, you can query metrics, logs, events, and other data via DQL, and you can also read the component entity information of the entity. For specific query methods, refer to the Func platform documentation.