From 978f43af89aa0834dfb49fbf606ce60def391c6e Mon Sep 17 00:00:00 2001 From: lrhkobe Date: Wed, 22 Jun 2022 17:41:27 +0800 Subject: [PATCH 1/4] optimize trace module in eventmesh --- .../runtime/boot/EventMeshHTTPServer.java | 13 ---- .../eventmesh-trace-api/build.gradle | 1 + .../eventmesh/trace/api/EventMeshSpan.java | 35 +++++++++ ...ervice.java => EventMeshTraceContext.java} | 41 ++-------- .../trace/api/EventMeshTraceService.java | 48 ++++++++++++ .../trace/api/TracePluginFactory.java | 6 +- .../api/common/EventMeshTraceConstants.java | 33 ++++++++ .../trace/api/common/ProtocolType.java | 23 ++++++ .../trace/api/exception/TraceException.java | 29 +++++++ .../propagation/EventMeshContextCarrier.java | 26 +++++++ .../trace/api/TracePluginFactoryTest.java | 6 +- .../trace/zipkin/ZipkinTraceService.java | 75 ++++++++++++++++--- 12 files changed, 272 insertions(+), 64 deletions(-) create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java rename eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/{TraceService.java => EventMeshTraceContext.java} (52%) create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/EventMeshTraceConstants.java create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/ProtocolType.java create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/exception/TraceException.java create mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java index 328c76e6a6..4971efb410 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshHTTPServer.java @@ -53,8 +53,6 @@ import org.apache.eventmesh.runtime.core.protocol.http.retry.HttpRetryer; import org.apache.eventmesh.runtime.metrics.http.HTTPMetricsServer; import org.apache.eventmesh.runtime.registry.Registry; -import org.apache.eventmesh.trace.api.TracePluginFactory; -import org.apache.eventmesh.trace.api.TraceService; import org.apache.commons.lang3.StringUtils; @@ -78,8 +76,6 @@ public class EventMeshHTTPServer extends AbstractHTTPServer { private EventMeshHTTPConfiguration eventMeshHttpConfiguration; - private TraceService traceService; - private Registry registry; public final ConcurrentHashMap localConsumerGroupMapping = @@ -253,11 +249,6 @@ public void init() throws Exception { //get the trace-plugin if (StringUtils.isNotEmpty(eventMeshHttpConfiguration.eventMeshTracePluginType) && eventMeshHttpConfiguration.eventMeshServerTraceEnable) { - traceService = - TracePluginFactory.getTraceService(eventMeshHttpConfiguration.eventMeshTracePluginType); - traceService.init(); - super.tracer = traceService.getTracer(super.getClass().toString()); - super.textMapPropagator = traceService.getTextMapPropagator(); super.useTrace = true; } @@ -284,10 +275,6 @@ public void shutdown() throws Exception { metrics.shutdown(); - if (traceService != null) { - traceService.shutdown(); - } - consumerManager.shutdown(); shutdownThreadPool(); diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/build.gradle b/eventmesh-trace-plugin/eventmesh-trace-api/build.gradle index 0804111b7c..f8b885f881 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/build.gradle +++ b/eventmesh-trace-plugin/eventmesh-trace-api/build.gradle @@ -18,6 +18,7 @@ dependencies { api project(":eventmesh-spi") implementation project(":eventmesh-common") + api 'io.cloudevents:cloudevents-core' implementation 'io.opentelemetry:opentelemetry-api' implementation 'io.opentelemetry:opentelemetry-sdk' diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java new file mode 100644 index 0000000000..5b27c9f80f --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api; + +import org.apache.eventmesh.trace.api.common.ProtocolType; + +public interface EventMeshSpan { + void start(); + void start(long timestamp); + void finish(); + void finish(long timestamp); + void setOperationName(String operationName); + void setRemoteAddress(String remoteAddress); + void setProtocolType(ProtocolType type); + void addTag(String key, String value); + void addError(Throwable throwable); + void setAsync(); + void setComponent(String componentName); + boolean isAsync(); +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TraceService.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java similarity index 52% rename from eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TraceService.java rename to eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java index c98a3bb8ca..e59f986785 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TraceService.java +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java @@ -17,39 +17,14 @@ package org.apache.eventmesh.trace.api; -import org.apache.eventmesh.spi.EventMeshExtensionType; -import org.apache.eventmesh.spi.EventMeshSPI; +public class EventMeshTraceContext { + private EventMeshSpan span; -import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.context.propagation.TextMapPropagator; + public EventMeshSpan getSpan() { + return span; + } -/** - * The top-level interface of trace - */ -@EventMeshSPI(isSingleton = true, eventMeshExtensionType = EventMeshExtensionType.TRACE) -public interface TraceService { - /** - * init the trace service - */ - void init(); - - /** - * close the trace service - */ - void shutdown(); - - /** - * get the tracer - * - * @param instrumentationName - * @return - */ - Tracer getTracer(String instrumentationName); - - /** - * get TextMapPropagator - * - * @return - */ - TextMapPropagator getTextMapPropagator(); + public void setSpan(EventMeshSpan span) { + this.span = span; + } } diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java new file mode 100644 index 0000000000..52828601bb --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api; + +import org.apache.eventmesh.spi.EventMeshExtensionType; +import org.apache.eventmesh.spi.EventMeshSPI; +import org.apache.eventmesh.trace.api.exception.TraceException; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.context.Context; + +@EventMeshSPI(isSingleton = true, eventMeshExtensionType = EventMeshExtensionType.TRACE) +public interface EventMeshTraceService { + void init() throws TraceException; + + //extract attr from carrier to context + Context extractFrom(Context context, Map carrier) throws TraceException; + + //inject attr from context to carrier + void inject(Context context, Map carrier); + + Span createSpan(String spanName, SpanKind spanKind, long startTimestamp, TimeUnit timeUnit, + Context context, boolean isSpanFinishInOtherThread) throws TraceException; + + Span createSpan(String spanName, SpanKind spanKind, Context context, + boolean isSpanFinishInOtherThread) throws TraceException; + + void shutdown() throws TraceException; +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TracePluginFactory.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TracePluginFactory.java index 91e8d5342c..e0bac19ed1 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TracePluginFactory.java +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/TracePluginFactory.java @@ -34,10 +34,10 @@ public class TracePluginFactory { * @param traceServiceType * @return */ - public static TraceService getTraceService(String traceServiceType) { + public static EventMeshTraceService getEventMeshTraceService(String traceServiceType) { checkNotNull(traceServiceType, "traceServiceType cannot be null"); - TraceService traceService = EventMeshExtensionFactory.getExtension(TraceService.class, traceServiceType); - return checkNotNull(traceService, "traceServiceType: " + traceServiceType + " is not supported"); + EventMeshTraceService eventMeshTraceService = EventMeshExtensionFactory.getExtension(EventMeshTraceService.class, traceServiceType); + return checkNotNull(eventMeshTraceService, "traceServiceType: " + traceServiceType + " is not supported"); } } diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/EventMeshTraceConstants.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/EventMeshTraceConstants.java new file mode 100644 index 0000000000..82dc63ead4 --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/EventMeshTraceConstants.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api.common; + + +public class EventMeshTraceConstants { + + public static final String TRACE_EVENTMESH_SDK_CLIENT_SPAN = "eventmesh-sdk-client-span"; + + public static final String TRACE_UPSTREAM_EVENTMESH_SERVER_SPAN = "upstream-eventmesh-server-span"; + public static final String TRACE_UPSTREAM_EVENTMESH_CLIENT_SPAN = "upstream-eventmesh-client-span"; + + public static final String TRACE_DOWNSTREAM_EVENTMESH_SERVER_SPAN = "downstream-eventmesh-server-span"; + public static final String TRACE_DOWNSTREAM_EVENTMESH_CLIENT_SPAN = "downstream-eventmesh-client-span"; + + public static final String TRACE_EVENTMESH_SDK_SERVER_SPAN = "eventmesh-sdk-server-span"; + +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/ProtocolType.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/ProtocolType.java new file mode 100644 index 0000000000..734f4c638d --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/common/ProtocolType.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api.common; + +public enum ProtocolType { + TCP, + HTTP +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/exception/TraceException.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/exception/TraceException.java new file mode 100644 index 0000000000..8a953ba381 --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/exception/TraceException.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api.exception; + +public class TraceException extends RuntimeException { + + public TraceException(String message) { + super(message); + } + + public TraceException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java new file mode 100644 index 0000000000..babb8cc0a3 --- /dev/null +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.trace.api.propagation; + +import java.util.Map; + +public interface EventMeshContextCarrier { + void extractFromMap(Map carrier); + void injectIntoMap(Map carrier); + Map getContext(); +} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java index 9b166aabcf..09d7bf5c28 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java @@ -29,18 +29,18 @@ public class TracePluginFactoryTest { @Test public void testFailedGetTraceService() { - NullPointerException nullPointerException1 = Assert.assertThrows(NullPointerException.class, () -> TracePluginFactory.getTraceService(null)); + NullPointerException nullPointerException1 = Assert.assertThrows(NullPointerException.class, () -> TracePluginFactory.getEventMeshTraceService(null)); MatcherAssert.assertThat(nullPointerException1.getMessage(), is("traceServiceType cannot be null")); String traceServiceType = "non-Existing"; NullPointerException nullPointerException2 = - Assert.assertThrows(NullPointerException.class, () -> TracePluginFactory.getTraceService(traceServiceType)); + Assert.assertThrows(NullPointerException.class, () -> TracePluginFactory.getEventMeshTraceService(traceServiceType)); MatcherAssert.assertThat(nullPointerException2.getMessage(), is("traceServiceType: " + traceServiceType + " is not supported")); } @Test public void testSuccessfulGetTraceService() { - TraceService zipkinTraceService = TracePluginFactory.getTraceService("zipkin"); + EventMeshTraceService zipkinTraceService = TracePluginFactory.getEventMeshTraceService("zipkin"); Assert.assertNotNull(zipkinTraceService); Assert.assertTrue(zipkinTraceService instanceof ZipkinTraceService); } diff --git a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java index df907c2446..13a4555d73 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java +++ b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java @@ -19,18 +19,25 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey; -import org.apache.eventmesh.trace.api.TraceService; +import org.apache.eventmesh.trace.api.EventMeshTraceService; import org.apache.eventmesh.trace.api.config.ExporterConfiguration; +import org.apache.eventmesh.trace.api.exception.TraceException; import org.apache.eventmesh.trace.zipkin.config.ZipkinConfiguration; +import java.util.Map; import java.util.concurrent.TimeUnit; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.context.propagation.TextMapGetter; import io.opentelemetry.context.propagation.TextMapPropagator; +import io.opentelemetry.context.propagation.TextMapSetter; import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.resources.Resource; @@ -38,10 +45,12 @@ import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; +import javax.annotation.Nullable; + /** * */ -public class ZipkinTraceService implements TraceService { +public class ZipkinTraceService implements EventMeshTraceService { // Zipkin API Endpoints for uploading spans private static final String ENDPOINT_V2_SPANS = "/api/v2/spans"; // Name of the service(using the instrumentationName) @@ -58,6 +67,9 @@ public class ZipkinTraceService implements TraceService { protected Thread shutdownHook; + private Tracer tracer; + private TextMapPropagator textMapPropagator; + @Override public void init() { //zipkin's config @@ -94,28 +106,67 @@ public void init() { .setTracerProvider(sdkTracerProvider) .build(); + //TODO serviceName??? + tracer = openTelemetry.getTracer(serviceName); + textMapPropagator = openTelemetry.getPropagators().getTextMapPropagator(); + shutdownHook = new Thread(sdkTracerProvider::close); Runtime.getRuntime().addShutdownHook(shutdownHook); } @Override - public void shutdown() { - //todo: check the spanProcessor if it was already close + public Context extractFrom(Context context, Map map) throws TraceException { + textMapPropagator.extract(context, map, new TextMapGetter>() { + @Override + public Iterable keys(Map carrier) { + return carrier.keySet(); + } + + @Override + public String get(Map carrier, String key) { + return carrier.get(key).toString(); + } + }); + return context; + } - sdkTracerProvider.close(); + @Override + public void inject(Context context, Map map) { + textMapPropagator.inject(context, map, new TextMapSetter>() { + @Override + public void set(@Nullable Map carrier, String key, String value) { + map.put(key, value); + } + }); + } - //todo: turn the value of useTrace in AbstractHTTPServer into false + @Override + public Span createSpan(String spanName, SpanKind spanKind, long startTime, TimeUnit timeUnit, + Context context, boolean isSpanFinishInOtherThread) + throws TraceException { + return tracer.spanBuilder(spanName) + .setParent(context) + .setSpanKind(spanKind) + .setStartTimestamp(startTime, timeUnit) + .startSpan(); } - //Gets or creates a named tracer instance @Override - public Tracer getTracer(String instrumentationName) { - return openTelemetry.getTracer(instrumentationName); + public Span createSpan(String spanName, SpanKind spanKind, Context context, + boolean isSpanFinishInOtherThread) throws TraceException { + return tracer.spanBuilder(spanName) + .setParent(context) + .setSpanKind(spanKind) + .setStartTimestamp(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .startSpan(); } - //to inject or extract span context @Override - public TextMapPropagator getTextMapPropagator() { - return openTelemetry.getPropagators().getTextMapPropagator(); + public void shutdown() { + //todo: check the spanProcessor if it was already close + + sdkTracerProvider.close(); + + //todo: turn the value of useTrace in AbstractHTTPServer into false } } From 35ae9aea56001b195e7cc798e1bd763f117a02b3 Mon Sep 17 00:00:00 2001 From: lrhkobe Date: Thu, 23 Jun 2022 14:37:17 +0800 Subject: [PATCH 2/4] fix check style problem --- .../eventmesh/trace/api/EventMeshSpan.java | 35 ------------------- .../trace/api/EventMeshTraceContext.java | 30 ---------------- .../trace/api/EventMeshTraceService.java | 3 ++ .../propagation/EventMeshContextCarrier.java | 26 -------------- 4 files changed, 3 insertions(+), 91 deletions(-) delete mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java delete mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java delete mode 100644 eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java deleted file mode 100644 index 5b27c9f80f..0000000000 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshSpan.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.trace.api; - -import org.apache.eventmesh.trace.api.common.ProtocolType; - -public interface EventMeshSpan { - void start(); - void start(long timestamp); - void finish(); - void finish(long timestamp); - void setOperationName(String operationName); - void setRemoteAddress(String remoteAddress); - void setProtocolType(ProtocolType type); - void addTag(String key, String value); - void addError(Throwable throwable); - void setAsync(); - void setComponent(String componentName); - boolean isAsync(); -} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java deleted file mode 100644 index e59f986785..0000000000 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceContext.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.trace.api; - -public class EventMeshTraceContext { - private EventMeshSpan span; - - public EventMeshSpan getSpan() { - return span; - } - - public void setSpan(EventMeshSpan span) { - this.span = span; - } -} diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java index 52828601bb..c7d5661990 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/EventMeshTraceService.java @@ -28,6 +28,9 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.context.Context; +/** + * EventMeshTraceService + */ @EventMeshSPI(isSingleton = true, eventMeshExtensionType = EventMeshExtensionType.TRACE) public interface EventMeshTraceService { void init() throws TraceException; diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java deleted file mode 100644 index babb8cc0a3..0000000000 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/main/java/org/apache/eventmesh/trace/api/propagation/EventMeshContextCarrier.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.eventmesh.trace.api.propagation; - -import java.util.Map; - -public interface EventMeshContextCarrier { - void extractFromMap(Map carrier); - void injectIntoMap(Map carrier); - Map getContext(); -} From e5533208df0ca4c3b3127776e83a8af012c1ac54 Mon Sep 17 00:00:00 2001 From: lrhkobe Date: Thu, 23 Jun 2022 15:16:43 +0800 Subject: [PATCH 3/4] fix checkstyleTest problem --- .../org/apache/eventmesh/trace/api/TracePluginFactoryTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java b/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java index 09d7bf5c28..58583fa063 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java +++ b/eventmesh-trace-plugin/eventmesh-trace-api/src/test/java/org/apache/eventmesh/trace/api/TracePluginFactoryTest.java @@ -29,7 +29,8 @@ public class TracePluginFactoryTest { @Test public void testFailedGetTraceService() { - NullPointerException nullPointerException1 = Assert.assertThrows(NullPointerException.class, () -> TracePluginFactory.getEventMeshTraceService(null)); + NullPointerException nullPointerException1 = Assert.assertThrows(NullPointerException.class, + () -> TracePluginFactory.getEventMeshTraceService(null)); MatcherAssert.assertThat(nullPointerException1.getMessage(), is("traceServiceType cannot be null")); String traceServiceType = "non-Existing"; From 466b05bb4d8e433e5903f79bf0eb4dfd4f49a61d Mon Sep 17 00:00:00 2001 From: lrhkobe Date: Thu, 23 Jun 2022 17:03:46 +0800 Subject: [PATCH 4/4] fix unit test problem --- .../org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java | 3 ++- ...ce => org.apache.eventmesh.trace.api.EventMeshTraceService} | 0 2 files changed, 2 insertions(+), 1 deletion(-) rename eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/resources/META-INF/eventmesh/{org.apache.eventmesh.trace.api.TraceService => org.apache.eventmesh.trace.api.EventMeshTraceService} (100%) diff --git a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java index 13a4555d73..cf36da592b 100644 --- a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java +++ b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/java/org/apache/eventmesh/trace/zipkin/ZipkinTraceService.java @@ -27,6 +27,8 @@ import java.util.Map; import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; + import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; @@ -45,7 +47,6 @@ import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; -import javax.annotation.Nullable; /** * diff --git a/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.trace.api.TraceService b/eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.trace.api.EventMeshTraceService similarity index 100% rename from eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.trace.api.TraceService rename to eventmesh-trace-plugin/eventmesh-trace-zipkin/src/main/resources/META-INF/eventmesh/org.apache.eventmesh.trace.api.EventMeshTraceService