|
|
|
@ -1,7 +1,5 @@
|
|
|
|
|
package com.fr.design.carton.latency; |
|
|
|
|
|
|
|
|
|
import com.fanruan.third.v2.org.ehcache.impl.internal.concurrent.ConcurrentHashMap; |
|
|
|
|
import com.fr.concurrent.FineExecutors; |
|
|
|
|
import com.fr.concurrent.NamedThreadFactory; |
|
|
|
|
import com.fr.config.MarketConfig; |
|
|
|
|
import com.fr.design.DesignerEnvManager; |
|
|
|
@ -10,15 +8,19 @@ import com.fr.design.mainframe.SiteCenterToken;
|
|
|
|
|
import com.fr.event.Event; |
|
|
|
|
import com.fr.event.EventDispatcher; |
|
|
|
|
import com.fr.event.Listener; |
|
|
|
|
import com.fr.general.CloudCenter; |
|
|
|
|
import com.fr.general.http.HttpToolbox; |
|
|
|
|
import com.fr.json.JSONObject; |
|
|
|
|
import com.fr.log.FineLoggerFactory; |
|
|
|
|
import com.fr.stable.ProductConstants; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
import com.fr.workspace.WorkContext; |
|
|
|
|
import com.fr.workspace.Workspace; |
|
|
|
|
import com.fr.workspace.WorkspaceEvent; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
@ -44,11 +46,13 @@ import static com.fr.design.carton.CartonConstants.USERID;
|
|
|
|
|
*/ |
|
|
|
|
public class DesignerLatencyMetric { |
|
|
|
|
|
|
|
|
|
private String latencyUrl; |
|
|
|
|
private ExecutorService executorService; |
|
|
|
|
private ScheduledExecutorService scheduler; |
|
|
|
|
private static final Map<LatencyLevel, AtomicInteger> LATENCY_CONTAINER = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
|
private static final String LATENCY_INFO_URL = "https://cloud.fanruan.com/api/monitor/record_of_deisgner_latency/single"; |
|
|
|
|
private static final String DEFAULT_MONITOR_URL = "https://cloud.fanruan.com/api/monitor/"; |
|
|
|
|
private static final String LATENCY_TABLE_SUFFIX = "record_of_designer_latency/single"; |
|
|
|
|
|
|
|
|
|
private final static class InstanceHolder { |
|
|
|
|
static final DesignerLatencyMetric INSTANCE = new DesignerLatencyMetric(); |
|
|
|
@ -72,11 +76,10 @@ public class DesignerLatencyMetric {
|
|
|
|
|
// 初始化容器
|
|
|
|
|
initializeContainer(); |
|
|
|
|
// 启动异步性能记录线程池
|
|
|
|
|
executorService = FineExecutors.newCachedThreadPool( |
|
|
|
|
new NamedThreadFactory(DesignerLatencyMetric.class)); |
|
|
|
|
executorService = Executors.newFixedThreadPool(8); |
|
|
|
|
// 启动定时埋点
|
|
|
|
|
this.scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("LatencyMetricWorker")); |
|
|
|
|
this.scheduler.scheduleWithFixedDelay(this::collectAndSubmit, 60, 60, TimeUnit.SECONDS); |
|
|
|
|
this.scheduler.scheduleWithFixedDelay(this::collectAndSubmit, 60, 60, TimeUnit.MINUTES); |
|
|
|
|
// 注册设计器工作目录切换事件监听
|
|
|
|
|
EventDispatcher.listen(WorkspaceEvent.AfterSwitch, new Listener<Workspace>() { |
|
|
|
|
@Override |
|
|
|
@ -84,6 +87,7 @@ public class DesignerLatencyMetric {
|
|
|
|
|
collectAndSubmit(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
FineLoggerFactory.getLogger().info("[Latency] designer latency metric started."); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -99,9 +103,19 @@ public class DesignerLatencyMetric {
|
|
|
|
|
this.scheduler.shutdown(); |
|
|
|
|
} |
|
|
|
|
collectAndSubmit(); |
|
|
|
|
FineLoggerFactory.getLogger().info("[Latency] designer latency metric stopped."); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String getLatencyUrl() { |
|
|
|
|
if (StringUtils.isEmpty(latencyUrl)) { |
|
|
|
|
String monitorEntry = CloudCenter.getInstance().acquireUrlByKind("cloud.monitor.api.entrypoint"); |
|
|
|
|
latencyUrl = (StringUtils.isNotEmpty(monitorEntry) ? monitorEntry : DEFAULT_MONITOR_URL) |
|
|
|
|
+ LATENCY_TABLE_SUFFIX; |
|
|
|
|
} |
|
|
|
|
return latencyUrl; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void initializeContainer() { |
|
|
|
|
for (LatencyLevel level : LatencyLevel.values()) { |
|
|
|
|
LATENCY_CONTAINER.put(level, new AtomicInteger()); |
|
|
|
@ -134,9 +148,10 @@ public class DesignerLatencyMetric {
|
|
|
|
|
para.put("token", SiteCenterToken.generateToken()); |
|
|
|
|
para.put("content", collect()); |
|
|
|
|
try { |
|
|
|
|
HttpToolbox.post(LATENCY_INFO_URL, para); |
|
|
|
|
} catch (Throwable ignore) { |
|
|
|
|
// doNothing
|
|
|
|
|
HttpToolbox.post(getLatencyUrl(), para); |
|
|
|
|
FineLoggerFactory.getLogger().debug("[Latency] submit latency log to cloud."); |
|
|
|
|
} catch (Throwable t) { |
|
|
|
|
FineLoggerFactory.getLogger().debug(t,"[Latency] failed to submit latency log to cloud."); |
|
|
|
|
} |
|
|
|
|
resetContainer(); |
|
|
|
|
} |
|
|
|
@ -151,8 +166,10 @@ public class DesignerLatencyMetric {
|
|
|
|
|
info.put(DESIGN_METHOD, WorkContext.getCurrent().isLocal() ? LOCAL : REMOTE); |
|
|
|
|
info.put(OPERANDS_NUM, LATENCY_CONTAINER.values().stream().mapToInt(AtomicInteger::get).sum()); |
|
|
|
|
for (Map.Entry<LatencyLevel, AtomicInteger> entry : LATENCY_CONTAINER.entrySet()) { |
|
|
|
|
if (!LatencyLevel.UNDEFINE.equals(entry.getKey())) { |
|
|
|
|
info.put(entry.getKey().getMark(), entry.getValue().get()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return info; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|