Skip to content
Flecs v4.1
alerts.h
Go to the documentation of this file.
1/**
2 * @file addons/alerts.h
3 * @brief Alerts module.
4 *
5 * The alerts module enables applications to register alerts for when certain
6 * conditions are met. Alerts are registered as queries, and automatically
7 * become active when entities match the alert query.
8 */
9
10#ifdef FLECS_ALERTS
11
12/**
13 * @defgroup c_addons_alerts Alerts
14 * @ingroup c_addons
15 * Create alerts from monitoring queries.
16 *
17 * @{
18 */
19
20#ifndef FLECS_ALERTS_H
21#define FLECS_ALERTS_H
22
23#ifndef FLECS_PIPELINE
24#define FLECS_PIPELINE
25#endif
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/** Maximum number of severity filters per alert. */
32#define ECS_ALERT_MAX_SEVERITY_FILTERS (4)
33
34/** Module ID. */
35FLECS_API extern ECS_COMPONENT_DECLARE(FlecsAlerts);
36
37/** Module components. */
38
39FLECS_API extern ECS_COMPONENT_DECLARE(EcsAlert); /**< Component added to alert, and used as the first element of the alert severity pair. */
40FLECS_API extern ECS_COMPONENT_DECLARE(EcsAlertInstance); /**< Component added to alert instance. */
41FLECS_API extern ECS_COMPONENT_DECLARE(EcsAlertsActive); /**< Component added to alert source, which tracks how many active alerts there are. */
42FLECS_API extern ECS_COMPONENT_DECLARE(EcsAlertTimeout); /**< Component added to alert, which tracks how long an alert has been inactive. */
43
44/** Alert severity tags. */
45FLECS_API extern ECS_TAG_DECLARE(EcsAlertInfo); /**< Info alert severity. */
46FLECS_API extern ECS_TAG_DECLARE(EcsAlertWarning); /**< Warning alert severity. */
47FLECS_API extern ECS_TAG_DECLARE(EcsAlertError); /**< Error alert severity. */
48FLECS_API extern ECS_TAG_DECLARE(EcsAlertCritical); /**< Critical alert severity. */
49
50/** Component added to alert instance. */
51typedef struct EcsAlertInstance {
52 char *message; /**< Generated alert message. */
54
55/** Map with active alerts for entity. */
56typedef struct EcsAlertsActive {
57 int32_t info_count; /**< Number of alerts for source with info severity. */
58 int32_t warning_count; /**< Number of alerts for source with warning severity. */
59 int32_t error_count; /**< Number of alerts for source with error severity. */
60 ecs_map_t alerts; /**< Map of active alerts for entity. */
62
63/** Alert severity filter.
64 * A severity filter can adjust the severity of an alert based on whether an
65 * entity in the alert query has a specific component. For example, a filter
66 * could check if an entity has the "Production" tag, and increase the default
67 * severity of an alert from Warning to Error.
68 */
70 ecs_entity_t severity; /**< Severity kind. */
71 ecs_id_t with; /**< Component to match. */
72 const char *var; /**< Variable to match component on. Do not include the
73 * '$' character. Leave as NULL for $this. */
74 int32_t _var_index; /**< Index of variable in query (do not set). */
76
77/** Alert descriptor, used with ecs_alert_init(). */
78typedef struct ecs_alert_desc_t {
79 int32_t _canary; /**< Used for validity testing. Do not set. */
80
81 /** Entity associated with alert. */
83
84 /** Alert query. An alert will be created for each entity that matches the
85 * specified query. The query must have at least one term that uses the
86 * $this variable (default). */
88
89 /** Template for alert message. This string is used to generate the alert
90 * message and may refer to variables in the query result. The format for
91 * the template expressions is as specified by ecs_script_string_interpolate().
92 *
93 * Examples:
94 *
95 * "$this has Position but not Velocity"
96 * "$this has a parent entity $parent without Position"
97 */
98 const char *message;
99
100 /** User-friendly name. Will only be set if FLECS_DOC addon is enabled. */
101 const char *doc_name;
102
103 /** Description of alert. Will only be set if FLECS_DOC addon is enabled. */
104 const char *brief;
105
106 /** Alert severity. Must be EcsAlertInfo, EcsAlertWarning, EcsAlertError, or
107 * EcsAlertCritical. Defaults to EcsAlertError. */
109
110 /** Severity filters can be used to assign different severities to the same
111 * alert. This prevents having to create multiple alerts, and allows
112 * entities to transition between severities without resetting the
113 * alert duration (optional). */
115
116 /** The retain period specifies how long an alert must be inactive before it
117 * is cleared. This makes it easier to track noisy alerts. While an alert is
118 * inactive, its duration won't increase.
119 * When the retain period is 0, the alert will clear immediately after it no
120 * longer matches the alert query. */
122
123 /** Alert when member value is out of range. Uses the warning and error ranges
124 * assigned to the member in the MemberRanges component (optional). */
126
127 /** (Component) ID of member to monitor. If left to 0, this will be set to
128 * the parent entity of the member (optional). */
130
131 /** Variable from which to fetch the member (optional). When left to NULL,
132 * 'id' will be obtained from $this. */
133 const char *var;
135
136/** Create a new alert.
137 * An alert is a query that is evaluated periodically and creates alert
138 * instances for each entity that matches the query. Alerts can be used to
139 * automate detection of errors in an application.
140 *
141 * Alerts are automatically cleared when a query is no longer true for an alert
142 * instance. At most one alert instance will be created per matched entity.
143 *
144 * Alert instances have three components:
145 * - AlertInstance: contains the alert message for the instance
146 * - MetricSource: contains the entity that triggered the alert
147 * - MetricValue: contains how long the alert has been active
148 *
149 * Alerts reuse components from the metrics addon so that alert instances can be
150 * tracked and discovered as metrics. Just like metrics, alert instances are
151 * created as children of the alert.
152 *
153 * When an entity has active alerts, it will have the EcsAlertsActive component
154 * which contains a map with active alerts for the entity. This component
155 * will be automatically removed once all alerts are cleared for the entity.
156 *
157 * @param world The world.
158 * @param desc Alert description.
159 * @return The alert entity.
160 */
161FLECS_API
163 ecs_world_t *world,
164 const ecs_alert_desc_t *desc);
165
166/** Create a new alert.
167 * @see ecs_alert_init()
168 */
169#define ecs_alert(world, ...)\
170 ecs_alert_init(world, &(ecs_alert_desc_t)__VA_ARGS__)
171
172/** Return number of active alerts for entity.
173 * When a valid alert entity is specified for the alert parameter, the operation
174 * will return whether the specified alert is active for the entity. When no
175 * alert is specified, the operation will return the total number of active
176 * alerts for the entity.
177 *
178 * @param world The world.
179 * @param entity The entity.
180 * @param alert The alert to test for (optional).
181 * @return The number of active alerts for the entity.
182 */
183FLECS_API
185 const ecs_world_t *world,
186 ecs_entity_t entity,
187 ecs_entity_t alert);
188
189/** Return alert instance for specified alert.
190 * This operation returns the alert instance for the specified alert. If the
191 * alert is not active for the entity, the operation will return 0.
192 *
193 * @param world The world.
194 * @param entity The entity.
195 * @param alert The alert to test for.
196 * @return The alert instance for the specified alert.
197 */
198FLECS_API
200 const ecs_world_t *world,
201 ecs_entity_t entity,
202 ecs_entity_t alert);
203
204/** Alert module import function.
205 * Usage:
206 * @code
207 * ECS_IMPORT(world, FlecsAlerts)
208 * @endcode
209 *
210 * @param world The world.
211 */
212FLECS_API
214 ecs_world_t *world);
215
216#ifdef __cplusplus
217}
218#endif
219
220#endif
221
222/** @} */
223
224#endif
FLECS_API ecs_entity_t ecs_get_alert(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t alert)
Return alert instance for specified alert.
#define ECS_ALERT_MAX_SEVERITY_FILTERS
Maximum number of severity filters per alert.
Definition alerts.h:32
FLECS_API void FlecsAlertsImport(ecs_world_t *world)
Alert module import function.
FLECS_API ecs_entity_t ecs_alert_init(ecs_world_t *world, const ecs_alert_desc_t *desc)
Create a new alert.
FLECS_API int32_t ecs_get_alert_count(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t alert)
Return number of active alerts for entity.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:388
#define ECS_TAG_DECLARE
Forward declare a tag.
Definition flecs_c.h:29
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:65
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
Component added to alert instance.
Definition alerts.h:51
char * message
Generated alert message.
Definition alerts.h:52
Map with active alerts for entity.
Definition alerts.h:56
int32_t error_count
Number of alerts for source with error severity.
Definition alerts.h:59
int32_t info_count
Number of alerts for source with info severity.
Definition alerts.h:57
ecs_map_t alerts
Map of active alerts for entity.
Definition alerts.h:60
int32_t warning_count
Number of alerts for source with warning severity.
Definition alerts.h:58
Alert descriptor, used with ecs_alert_init().
Definition alerts.h:78
ecs_alert_severity_filter_t severity_filters[(4)]
Severity filters can be used to assign different severities to the same alert.
Definition alerts.h:114
const char * brief
Description of alert.
Definition alerts.h:104
ecs_ftime_t retain_period
The retain period specifies how long an alert must be inactive before it is cleared.
Definition alerts.h:121
ecs_entity_t member
Alert when member value is out of range.
Definition alerts.h:125
ecs_id_t id
(Component) ID of member to monitor.
Definition alerts.h:129
ecs_entity_t entity
Entity associated with alert.
Definition alerts.h:82
ecs_entity_t severity
Alert severity.
Definition alerts.h:108
ecs_query_desc_t query
Alert query.
Definition alerts.h:87
const char * doc_name
User-friendly name.
Definition alerts.h:101
int32_t _canary
Used for validity testing.
Definition alerts.h:79
const char * message
Template for alert message.
Definition alerts.h:98
const char * var
Variable from which to fetch the member (optional).
Definition alerts.h:133
Alert severity filter.
Definition alerts.h:69
int32_t _var_index
Index of variable in query (do not set).
Definition alerts.h:74
const char * var
Variable to match component on.
Definition alerts.h:72
ecs_entity_t severity
Severity kind.
Definition alerts.h:70
ecs_id_t with
Component to match.
Definition alerts.h:71
Used with ecs_query_init().
Definition flecs.h:1325