You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by jo...@apache.org on 2023/02/19 02:36:10 UTC
[incubator-eventmesh] branch master updated: [ISSUE #3196]Refactor Trace (#3199)
This is an automated email from the ASF dual-hosted git repository.
jonyang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git
The following commit(s) were added to refs/heads/master by this push:
new bc279e356 [ISSUE #3196]Refactor Trace (#3199)
bc279e356 is described below
commit bc279e35647fdd1fbb4d3d52bc3ecb9abfb279ba
Author: mxsm <lj...@gmail.com>
AuthorDate: Sun Feb 19 10:36:03 2023 +0800
[ISSUE #3196]Refactor Trace (#3199)
---
.../eventmesh/runtime/boot/EventMeshServer.java | 5 +--
.../org/apache/eventmesh/runtime/trace/Trace.java | 41 +++++++++++++++-------
2 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java
index 34e8785c8..99046addc 100644
--- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java
+++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshServer.java
@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
+
import lombok.extern.slf4j.Slf4j;
@Slf4j
@@ -64,7 +65,7 @@ public class EventMeshServer {
this.acl = Acl.getInstance(this.configuration.getEventMeshSecurityPluginType());
this.registry = new Registry();
- trace = new Trace(this.configuration.isEventMeshServerTraceEnable());
+ trace = Trace.getInstance(this.configuration.getEventMeshTracePluginType(), this.configuration.isEventMeshServerTraceEnable());
this.connectorResource = ConnectorResource.getInstance(this.configuration.getEventMeshConnectorPluginType());
final List<String> provideServerProtocols = configuration.getEventMeshProvideServerProtocols();
@@ -92,7 +93,7 @@ public class EventMeshServer {
registry.init(configuration.getEventMeshRegistryPluginType());
}
if (configuration.isEventMeshServerTraceEnable()) {
- trace.init(configuration.getEventMeshTracePluginType());
+ trace.init();
}
}
diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/trace/Trace.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/trace/Trace.java
index b20c9b435..cefe5da26 100644
--- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/trace/Trace.java
+++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/trace/Trace.java
@@ -20,8 +20,10 @@ package org.apache.eventmesh.runtime.trace;
import org.apache.eventmesh.trace.api.EventMeshTraceService;
import org.apache.eventmesh.trace.api.TracePluginFactory;
+import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import io.cloudevents.CloudEvent;
import io.netty.channel.ChannelHandlerContext;
@@ -36,22 +38,38 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Trace {
- private final boolean useTrace;
+ private static final Map<String, Trace> TRACE_CACHE = new HashMap<>(16);
+
+ private final AtomicBoolean inited = new AtomicBoolean(false);
+
private EventMeshTraceService eventMeshTraceService;
- public Trace(boolean useTrace) {
- this.useTrace = useTrace;
+ private boolean useTrace;
+
+ public static Trace getInstance(String tracePluginType, boolean useTrace) {
+ return TRACE_CACHE.computeIfAbsent(tracePluginType, key -> traceBuilder(tracePluginType, useTrace));
+ }
+
+ private static Trace traceBuilder(String tracePluginType, boolean useTrace) {
+ Trace trace = new Trace();
+ trace.useTrace = useTrace;
+ trace.eventMeshTraceService = TracePluginFactory.getEventMeshTraceService(tracePluginType);
+ return trace;
}
- public void init(String tracePluginType) throws Exception {
- if (useTrace) {
- eventMeshTraceService = TracePluginFactory.getEventMeshTraceService(tracePluginType);
- eventMeshTraceService.init();
+ private Trace() {
+
+ }
+
+ public void init() throws Exception {
+ if (!inited.compareAndSet(false, true)) {
+ return;
}
+ eventMeshTraceService.init();
}
public Span createSpan(String spanName, SpanKind spanKind, long startTime, TimeUnit timeUnit,
- Context context, boolean isSpanFinishInOtherThread) {
+ Context context, boolean isSpanFinishInOtherThread) {
if (!useTrace) {
return Span.getInvalid();
}
@@ -60,7 +78,7 @@ public class Trace {
}
public Span createSpan(String spanName, SpanKind spanKind, Context context,
- boolean isSpanFinishInOtherThread) {
+ boolean isSpanFinishInOtherThread) {
if (!useTrace) {
return Span.getInvalid();
}
@@ -204,8 +222,7 @@ public class Trace {
}
}
- public void finishSpan(ChannelHandlerContext ctx, StatusCode statusCode, String errMsg,
- Throwable throwable) {
+ public void finishSpan(ChannelHandlerContext ctx, StatusCode statusCode, String errMsg, Throwable throwable) {
try {
if (useTrace) {
Context context = ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).get();
@@ -229,7 +246,7 @@ public class Trace {
}
public void shutdown() throws Exception {
- if (useTrace) {
+ if (useTrace && inited.compareAndSet(true, false)) {
eventMeshTraceService.shutdown();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org