Skip to content

Commit

Permalink
refactor : tracing analyzer optimization (#108)
Browse files Browse the repository at this point in the history
* Aggregate trace metrics using analyzer metrics

* Use jackson replace gson

* Optimize service calculation

* Optimize service calculation
  • Loading branch information
liuhaoyang authored Dec 29, 2021
1 parent ed0dd61 commit 1fd43cb
Show file tree
Hide file tree
Showing 59 changed files with 521 additions and 1,691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

import cloud.erda.analyzer.alert.models.RenderedAlertEvent;
import cloud.erda.analyzer.alert.models.eventbox.*;
import cloud.erda.analyzer.alert.models.eventbox.*;
import cloud.erda.analyzer.common.constant.AlertConstants;
import cloud.erda.analyzer.common.constant.Constants;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import org.apache.flink.api.common.functions.MapFunction;

import java.util.Map;
Expand Down Expand Up @@ -62,7 +61,7 @@ public EventBoxRequest map(RenderedAlertEvent value) throws Exception {
eventBoxChannel.setName(groupType);
eventBoxChannel.setTemplate(value.getContent());
// todo 在 3.19 兼容tag使用,eventbox Channel 需要重构,tag 改为map比较好
eventBoxChannel.setTag(GsonUtil.toJson(value.getMetricEvent().getTags()));
eventBoxChannel.setTag(JsonMapperUtils.toStrings(value.getMetricEvent().getTags()));
// todo 只在3.11兼容邮件模板用
if (groupType.equals("email")) {
eventBoxChannel.setType("markdown");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import cloud.erda.analyzer.alert.templates.TemplateRenderer;
import cloud.erda.analyzer.alert.templates.formatters.TemplateFormatter;
import cloud.erda.analyzer.alert.templates.formatters.TemplateFormatterFactory;
import cloud.erda.analyzer.alert.models.*;
import cloud.erda.analyzer.common.constant.AlertConstants;
import cloud.erda.analyzer.common.utils.DateUtils;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import org.apache.flink.api.common.functions.MapFunction;

import java.util.HashMap;
Expand All @@ -48,7 +47,7 @@ public RenderedNotifyEvent map(NotifyEvent notifyEvent) throws Exception {
result.setTemplateTarget(notifyEvent.getNotifyTemplate().getTemplate().getTarget());
//将fields转换为map
Map<String, Object> templateContext = TemplateContext.fromMetric(result.getMetricEvent());
Map<String, Object> attribute = GsonUtil.toMap(notifyEvent.getNotify().getAttribute(), String.class, Object.class);
Map<String, Object> attribute = JsonMapperUtils.toObjectValueMap(notifyEvent.getNotify().getAttribute());
if (attribute != null) {
templateContext.putAll(attribute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

import cloud.erda.analyzer.alert.models.RenderedNotifyEvent;
import cloud.erda.analyzer.alert.models.eventbox.*;
import cloud.erda.analyzer.alert.models.eventbox.*;
import cloud.erda.analyzer.common.constant.AlertConstants;
import cloud.erda.analyzer.common.constant.Constants;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import org.apache.flink.api.common.functions.MapFunction;

import java.util.Map;
Expand All @@ -29,12 +28,12 @@ public class NotifyTargetToEventBoxMapFunction implements MapFunction<RenderedNo
public EventBoxRequest map(RenderedNotifyEvent renderedNotifyEvent) throws Exception {
EventBoxRequest request = new EventBoxRequest();
request.setSender(Constants.EVENTBOX_SENDER);
if(renderedNotifyEvent.getNotifyTarget().equals(AlertConstants.ALERT_NOTIFY_TYPE_DINGDING)) {
if (renderedNotifyEvent.getNotifyTarget().equals(AlertConstants.ALERT_NOTIFY_TYPE_DINGDING)) {
request.setContent(renderedNotifyEvent.getContent());
EventBoxDingDingLabel eventBoxDingDingLabel = EventBoxDingDingLabel.label(renderedNotifyEvent.getTitle(),renderedNotifyEvent.getNotifyTarget().getDingdingUrl());
EventBoxDingDingLabel eventBoxDingDingLabel = EventBoxDingDingLabel.label(renderedNotifyEvent.getTitle(), renderedNotifyEvent.getNotifyTarget().getDingdingUrl());
request.setLabels(eventBoxDingDingLabel);
} else {
Map<String,String> tags = renderedNotifyEvent.getMetricEvent().getTags();
Map<String, String> tags = renderedNotifyEvent.getMetricEvent().getTags();
EventBoxContent content = new EventBoxContent();
// content.setSourceType(renderedNotifyEvent.getScopeType());
// content.setSourceId(renderedNotifyEvent.getScopeId());
Expand All @@ -44,7 +43,7 @@ public EventBoxRequest map(RenderedNotifyEvent renderedNotifyEvent) throws Excep
EventBoxChannel eventBoxChannel = new EventBoxChannel();
eventBoxChannel.setName(groupType);
eventBoxChannel.setTemplate(renderedNotifyEvent.getContent());
eventBoxChannel.setTag(GsonUtil.toJson(renderedNotifyEvent.getMetricEvent().getTags()));
eventBoxChannel.setTag(JsonMapperUtils.toStrings(renderedNotifyEvent.getMetricEvent().getTags()));
if (groupType.equals("email")) {
eventBoxChannel.setType("markdown");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package cloud.erda.analyzer.alert.models;

import com.google.gson.annotations.SerializedName;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;

/**
Expand All @@ -36,13 +36,13 @@ public class AlertNotifyTarget {
*/
private String type;

@SerializedName(value = "group_id")
@JsonSetter("group_id")
private String groupId;

@SerializedName(value = "group_type")
@JsonSetter("group_type")
private String groupType;

@SerializedName(value = "dingding_url")
@JsonSetter("dingding_url")
private String dingdingUrl;

private String level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

package cloud.erda.analyzer.alert.models;

import com.google.gson.annotations.SerializedName;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;

@Data
public class NotifyTarget {
@SerializedName(value = "group_id")
@JsonSetter("group_id")
private String groupId;
@SerializedName(value = "channels")

@JsonSetter("channels")
private String[] channels;

private String dingdingUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@

package cloud.erda.analyzer.alert.models;

import com.google.gson.annotations.SerializedName;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class NotifyTemplate {
@SerializedName("id")

@JsonSetter("id")
private String notifyId; //模版id

private Metadata metadata;

private Behavior behavior;

private Template[] templates;

private long processingTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package cloud.erda.analyzer.alert.models.eventbox;

import com.google.gson.annotations.SerializedName;
import com.fasterxml.jackson.annotation.JsonGetter;
import lombok.Data;

/**
Expand All @@ -24,12 +24,21 @@
@Data
public class EventBoxDingDingLabel {

@SerializedName(value = "DINGDING")
private String[] dingding;

@SerializedName(value = "MARKDOWN")
private EventBoxDingDingLabelMarkdown markdown;

private String[] dingding;

@JsonGetter("MARKDOWN")
public EventBoxDingDingLabelMarkdown getMarkdown() {
return markdown;
}

@JsonGetter("DINGDING")
public String[] getDingding() {
return dingding;
}

public static EventBoxDingDingLabel label(String title, String... dingding) {
EventBoxDingDingLabel eventBoxDingDingLabel = new EventBoxDingDingLabel();
eventBoxDingDingLabel.setDingding(dingding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

package cloud.erda.analyzer.alert.models.eventbox;

import com.google.gson.annotations.SerializedName;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;

/**
Expand All @@ -24,6 +25,11 @@
@Data
public class EventBoxNotifyGroupLabel {

@SerializedName(value = "GROUP")
@JsonSetter("GROUP")
private long group;

@JsonGetter("GROUP")
public long getGroup() {
return group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import cloud.erda.analyzer.alert.models.eventbox.EventBoxRequest;
import cloud.erda.analyzer.common.constant.Constants;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import cloud.erda.analyzer.common.utils.http.ContentTypes;
import cloud.erda.analyzer.common.utils.http.HttpMethods;
import cloud.erda.analyzer.common.utils.http.HttpRequestDTO;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void invoke(EventBoxRequest value, Context context) throws Exception {
if (value == null) {
return;
}
String body = GsonUtil.toJson(value);
String body = JsonMapperUtils.toStrings(value);
HttpRequestDTO httpRequestDTO = new HttpRequestDTO();
httpRequestDTO.setBody(body);
httpRequestDTO.setContentType(ContentTypes.APPLICATION_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import cloud.erda.analyzer.alert.models.AlertTrigger;
import cloud.erda.analyzer.common.constant.AlertConstants;
import cloud.erda.analyzer.common.constant.Constants;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import cloud.erda.analyzer.common.utils.http.ContentTypes;
import cloud.erda.analyzer.common.utils.http.HttpAsyncRequestService;
import cloud.erda.analyzer.common.utils.http.HttpMethods;
Expand Down Expand Up @@ -58,7 +58,7 @@ public void open(Configuration parameters) throws Exception {

@Override
public void invoke(Ticket value, Context context) throws Exception {
String body = GsonUtil.toJson(value);
String body = JsonMapperUtils.toStrings(value);
HttpRequestDTO httpRequestDTO = new HttpRequestDTO();
String trigger = value.getLabel().getOrDefault(AlertConstants.TRIGGER, AlertTrigger.alert.name());
if (AlertTrigger.alert.equals(AlertTrigger.valueOf(trigger))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cloud.erda.analyzer.alert.sources;

import cloud.erda.analyzer.alert.models.NotifyTemplate;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
Expand All @@ -31,7 +31,7 @@
import java.util.Map;

@Slf4j
public class AllNotifyTemplates implements SourceFunction<NotifyTemplate>{
public class AllNotifyTemplates implements SourceFunction<NotifyTemplate> {
private String monitorAddr;
private CloseableHttpClient httpclient;
private long httpInterval = 60000;
Expand All @@ -50,10 +50,10 @@ public ArrayList<NotifyTemplate> GetSysTemplateList() throws IOException {
CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpGet);
try {
if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
String str = EntityUtils.toString(closeableHttpResponse.getEntity());
Map<String, Object> templateMap = GsonUtil.toMap(str, String.class, Object.class);
byte[] data = EntityUtils.toByteArray(closeableHttpResponse.getEntity());
Map<String, Object> templateMap = JsonMapperUtils.toObjectValueMap(data);
Object templateInfo = templateMap.get("data");
templateArr = GsonUtil.toArrayList(GsonUtil.toJson(templateInfo),NotifyTemplate.class);
templateArr = JsonMapperUtils.toArrayList(JsonMapperUtils.toBytes(templateInfo), NotifyTemplate.class);
}
} finally {
closeableHttpResponse.close();
Expand All @@ -78,7 +78,7 @@ public void run(SourceContext<NotifyTemplate> sourceContext) throws Exception {

@Override
public void cancel() {
if(this.httpclient != null) {
if (this.httpclient != null) {
try {
this.httpclient.close();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import cloud.erda.analyzer.alert.models.AlertNotify;
import cloud.erda.analyzer.alert.models.AlertNotifyTarget;
import cloud.erda.analyzer.common.constant.AlertConstants;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import cloud.erda.analyzer.common.utils.StringUtil;
import cloud.erda.analyzer.runtime.sources.DataRowReader;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -40,7 +40,7 @@ public AlertNotify read(ResultSet resultSet) throws Exception {
notify.setAlertId(resultSet.getString("alert_id"));
notify.setEnable(resultSet.getBoolean("enable"));
String notifyTargetData = resultSet.getString("notify_target");
AlertNotifyTarget notifyTarget = GsonUtil.toObject(notifyTargetData, AlertNotifyTarget.class);
AlertNotifyTarget notifyTarget = JsonMapperUtils.toObject(notifyTargetData, AlertNotifyTarget.class);
if (AlertConstants.ALERT_NOTIFY_TYPE_NOTIFY_GROUP.equals(notifyTarget.getType())) {
notifyTarget.setGroupTypes(notifyTarget.getGroupType().split(","));
}
Expand All @@ -58,7 +58,9 @@ public AlertNotify read(ResultSet resultSet) throws Exception {
notify.setSilence(resultSet.getLong("silence"));
notify.setSilencePolicy(resultSet.getString("silence_policy"));
notify.setProcessingTime(System.currentTimeMillis());
log.info("Read alert notify {} data: {}", notify.getId(), GsonUtil.toJson(notify));
if (log.isInfoEnabled()) {
log.info("Read alert notify {} data: {}", notify.getId(), JsonMapperUtils.toStrings(notify));
}
return notify;
} catch (Exception ex) {
log.warn("Read or deserialize Notify Metadata error.", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import cloud.erda.analyzer.alert.models.AlertNotifyTemplate;
import cloud.erda.analyzer.alert.models.AlertTrigger;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import cloud.erda.analyzer.runtime.sources.DataRowReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -58,14 +58,16 @@ public AlertNotifyTemplate read(ResultSet resultSet) throws Exception {
if (StringUtils.isEmpty(formatString)) {
formats = new HashMap<>();
} else {
formats = GsonUtil.toMap(formatString, String.class, String.class);
formats = JsonMapperUtils.toStringValueMap(formatString);
}
notifyTemplate.setFormats(formats);
checkNotNull(notifyTemplate.getTitle(), "Title cannot be null");
checkNotNull(notifyTemplate.getTemplate(), "Template cannot be null");
notifyTemplate.setProcessingTime(System.currentTimeMillis());
notifyTemplate.setVariable(templateVariable);
log.info("Read alert notify template {} data: {}",notifyTemplate.getId(), GsonUtil.toJson(notifyTemplate));
if (log.isInfoEnabled()) {
log.info("Read alert notify template {} data: {}", notifyTemplate.getId(), JsonMapperUtils.toStrings(notifyTemplate));
}
return notifyTemplate;
} catch (Exception ex) {
log.warn("Read or deserialize id {} Custom AlertNotifyTemplate error.", resultSet.getLong("id"), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import cloud.erda.analyzer.alert.models.NotifyTarget;
import cloud.erda.analyzer.alert.models.Notify;
import cloud.erda.analyzer.common.utils.GsonUtil;
import cloud.erda.analyzer.common.utils.JsonMapperUtils;
import cloud.erda.analyzer.runtime.sources.DataRowReader;

import java.sql.ResultSet;
Expand All @@ -29,9 +29,9 @@ public Notify read(ResultSet resultSet) throws Exception {
Notify notify = new Notify();
Long id = resultSet.getLong("id");
String target = resultSet.getString("target");
NotifyTarget notifyTarget = GsonUtil.toObject(target, NotifyTarget.class);
NotifyTarget notifyTarget = JsonMapperUtils.toObject(target, NotifyTarget.class);
String templateIds = resultSet.getString("notify_id");
ArrayList<String> templateIDArr = GsonUtil.toArrayList(templateIds,String.class);
ArrayList<String> templateIDArr = JsonMapperUtils.toArrayList(templateIds, String.class);
String scopeType = resultSet.getString("scope");
String scopeId = resultSet.getString("scope_id");
String attribute = resultSet.getString("attributes");
Expand Down
Loading

0 comments on commit 1fd43cb

Please sign in to comment.