@@ -55,6 +55,8 @@ public class RealTimeAlertCalculator {
55
55
private static final int CALCULATE_THREADS = 3 ;
56
56
57
57
private static final String KEY_INSTANCE = "__instance__" ;
58
+ private static final String KEY_INSTANCE_NAME = "__instancename__" ;
59
+ private static final String KEY_INSTANCE_HOST = "__instancehost__" ;
58
60
private static final String KEY_APP = "__app__" ;
59
61
private static final String KEY_METRICS = "__metrics__" ;
60
62
private static final String KEY_PRIORITY = "__priority__" ;
@@ -125,10 +127,14 @@ private void startCalculate() {
125
127
private void calculate (CollectRep .MetricsData metricsData ) {
126
128
long currentTimeMilli = System .currentTimeMillis ();
127
129
String instance = String .valueOf (metricsData .getId ());
130
+ String instanceName = metricsData .getInstanceName ();
131
+ String instanceHost = metricsData .getInstanceHost ();
128
132
String app = metricsData .getApp ();
129
133
String metrics = metricsData .getMetrics ();
130
134
int priority = metricsData .getPriority ();
131
135
int code = metricsData .getCode ().getNumber ();
136
+ Map <String , String > labels = metricsData .getLabels ();
137
+ Map <String , String > annotations = metricsData .getAnnotations ();
132
138
List <AlertDefine > thresholds = this .alertDefineService .getRealTimeAlertDefines ();
133
139
// Filter thresholds by app, metrics and instance
134
140
thresholds = filterThresholdsByAppAndMetrics (thresholds , app , metrics , instance , priority );
@@ -137,13 +143,18 @@ private void calculate(CollectRep.MetricsData metricsData) {
137
143
}
138
144
Map <String , Object > commonContext = new HashMap <>(8 );
139
145
commonContext .put (KEY_INSTANCE , instance );
146
+ commonContext .put (KEY_INSTANCE_NAME , instanceName );
147
+ commonContext .put (KEY_INSTANCE_HOST , instanceHost );
140
148
commonContext .put (KEY_APP , app );
141
149
commonContext .put (KEY_PRIORITY , priority );
142
150
commonContext .put (KEY_CODE , code );
143
151
commonContext .put (KEY_METRICS , metrics );
144
152
if (priority == 0 ) {
145
153
commonContext .put (KEY_AVAILABLE , metricsData .getCode () == CollectRep .Code .SUCCESS ? UP : DOWN );
146
154
}
155
+ if (labels != null ) {
156
+ commonContext .putAll (labels );
157
+ }
147
158
List <CollectRep .Field > fields = metricsData .getFields ();
148
159
Map <String , Object > fieldValueMap = new HashMap <>(8 );
149
160
int valueRowCount = metricsData .getValuesCount ();
@@ -160,21 +171,26 @@ private void calculate(CollectRep.MetricsData metricsData) {
160
171
if (StringUtils .isBlank (expr )) {
161
172
continue ;
162
173
}
174
+ Map <String , String > commonFingerPrints = new HashMap <>(8 );
175
+ commonFingerPrints .put (CommonConstants .LABEL_INSTANCE , instance );
176
+ // here use the alert name as finger, not care the alert name may be changed
177
+ commonFingerPrints .put (CommonConstants .LABEL_ALERT_NAME , define .getName ());
178
+ commonFingerPrints .put (CommonConstants .LABEL_INSTANCE_NAME , instanceName );
179
+ commonFingerPrints .put (CommonConstants .LABEL_INSTANCE_HOST , instanceHost );
180
+ commonFingerPrints .putAll (define .getLabels ());
181
+ if (labels != null ) {
182
+ commonFingerPrints .putAll (labels );
183
+ }
163
184
{
164
185
// trigger the expr before the metrics data, due the available up down or others
165
186
try {
166
187
boolean match = execAlertExpression (fieldValueMap , expr , true );
167
188
try {
168
- Map <String , String > fingerPrints = new HashMap <>(8 );
169
- fingerPrints .put (CommonConstants .LABEL_INSTANCE , instance );
170
- // here use the alert name as finger, not care the alert name may be changed
171
- fingerPrints .put (CommonConstants .LABEL_ALERT_NAME , define .getName ());
172
- fingerPrints .putAll (define .getLabels ());
173
189
if (match ) {
174
190
// If the threshold rule matches, the number of times the threshold has been triggered is determined and an alarm is triggered
175
- afterThresholdRuleMatch (currentTimeMilli , fingerPrints , fieldValueMap , define );
191
+ afterThresholdRuleMatch (currentTimeMilli , commonFingerPrints , fieldValueMap , define , annotations );
176
192
} else {
177
- handleRecoveredAlert (fingerPrints );
193
+ handleRecoveredAlert (commonFingerPrints );
178
194
}
179
195
// if this threshold pre compile success, ignore blew
180
196
continue ;
@@ -192,9 +208,7 @@ private void calculate(CollectRep.MetricsData metricsData) {
192
208
fieldValueMap .put (KEY_ROW , valueRowCount );
193
209
fieldValueMap .putAll (commonContext );
194
210
fingerPrints .clear ();
195
- fingerPrints .put (CommonConstants .LABEL_INSTANCE , instance );
196
- fingerPrints .put (CommonConstants .LABEL_ALERT_NAME , define .getName ());
197
- fingerPrints .putAll (define .getLabels ());
211
+ fingerPrints .putAll (commonFingerPrints );
198
212
for (int index = 0 ; index < valueRow .getColumnsList ().size (); index ++) {
199
213
String valueStr = valueRow .getColumns (index );
200
214
if (CommonConstants .NULL_VALUE .equals (valueStr )) {
@@ -228,7 +242,7 @@ private void calculate(CollectRep.MetricsData metricsData) {
228
242
boolean match = execAlertExpression (fieldValueMap , expr , false );
229
243
try {
230
244
if (match ) {
231
- afterThresholdRuleMatch (currentTimeMilli , fingerPrints , fieldValueMap , define );
245
+ afterThresholdRuleMatch (currentTimeMilli , fingerPrints , fieldValueMap , define , annotations );
232
246
} else {
233
247
handleRecoveredAlert (fingerPrints );
234
248
}
@@ -311,20 +325,31 @@ private void handleRecoveredAlert(Map<String, String> fingerprints) {
311
325
pendingAlertMap .remove (fingerprint );
312
326
}
313
327
314
- private void afterThresholdRuleMatch (long currentTimeMilli , Map <String , String > fingerPrints ,
315
- Map <String , Object > fieldValueMap , AlertDefine define ) {
328
+ private void afterThresholdRuleMatch (long currentTimeMilli , Map <String , String > fingerPrints ,
329
+ Map <String , Object > fieldValueMap , AlertDefine define , Map < String , String > annotations ) {
316
330
String fingerprint = calculateFingerprint (fingerPrints );
317
331
SingleAlert existingAlert = pendingAlertMap .get (fingerprint );
318
- Map <String , String > labels = new HashMap <>(8 );
319
332
fieldValueMap .putAll (define .getLabels ());
320
- labels .putAll (fingerPrints );
321
333
int requiredTimes = define .getTimes () == null ? 1 : define .getTimes ();
322
334
if (existingAlert == null ) {
323
335
// First time triggering alert, create new alert and set to pending status
336
+ Map <String , String > alertLabels = new HashMap <>(8 );
337
+ alertLabels .putAll (fingerPrints );
338
+ Map <String , String > alertAnnotations = new HashMap <>(8 );
339
+ if (annotations != null ) {
340
+ alertAnnotations .putAll (annotations );
341
+ }
342
+ if (define .getAnnotations () != null ) {
343
+ alertAnnotations .putAll (define .getAnnotations ());
344
+ }
345
+ // render var content in annotations
346
+ for (Map .Entry <String , String > entry : alertAnnotations .entrySet ()) {
347
+ entry .setValue (AlertTemplateUtil .render (entry .getValue (), fieldValueMap ));
348
+ }
324
349
SingleAlert newAlert = SingleAlert .builder ()
325
- .labels (labels )
326
- // todo render var content in annotations
327
- . annotations ( define . getAnnotations ())
350
+ .labels (alertLabels )
351
+ . annotations ( alertAnnotations )
352
+ // render var content in content
328
353
.content (AlertTemplateUtil .render (define .getTemplate (), fieldValueMap ))
329
354
.status (CommonConstants .ALERT_STATUS_PENDING )
330
355
.triggerTimes (1 )
0 commit comments