You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2021/09/30 15:49:23 UTC

[GitHub] [incubator-eventmesh] tydhot commented on a change in pull request #527: [ISSUE #340]Add http trace http point

tydhot commented on a change in pull request #527:
URL: https://github.com/apache/incubator-eventmesh/pull/527#discussion_r719536697



##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -256,7 +294,18 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest)
                 requestCommand.setHttpMethod(httpRequest.method().name());
                 requestCommand.setHttpVersion(httpRequest.protocolVersion().protocolName());
                 requestCommand.setRequestCode(requestCode);
-                // todo record command method, version and requestCode in span.
+
+                //todo record command opaque in span.
+                span = tracer.spanBuilder("HTTP "+requestCommand.httpMethod).setParent(context).setSpanKind(SpanKind.SERVER).startSpan();
+                span.addEvent("Span Start");

Review comment:
       avoid span event here

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -256,7 +294,18 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest)
                 requestCommand.setHttpMethod(httpRequest.method().name());
                 requestCommand.setHttpVersion(httpRequest.protocolVersion().protocolName());
                 requestCommand.setRequestCode(requestCode);
-                // todo record command method, version and requestCode in span.
+
+                //todo record command opaque in span.
+                span = tracer.spanBuilder("HTTP "+requestCommand.httpMethod).setParent(context).setSpanKind(SpanKind.SERVER).startSpan();
+                span.addEvent("Span Start");
+                //attach the span to the server context
+                context = context.with(SpanKey.SERVER_KEY,span);
+                //put the context in channel
+                ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).set(context);
+                //todo record command method, version and requestCode in span.
+                span.setAttribute("HttpMethod",httpRequest.method().name());

Review comment:
       ues otel key instaed : https://github.com/open-telemetry/opentelemetry-java/blob/main/semconv/src/main/java/io/opentelemetry/semconv/trace/attributes/SemanticAttributes.java

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -132,13 +142,26 @@ public void sendError(ChannelHandlerContext ctx,
                 "; charset=" + EventMeshConstants.DEFAULT_CHARSET);
         response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
         response.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
-        // todo server span end with error, record status, we should get channel here to get span in channel's context in async call..
+        //todo server span end with error, record status, we should get channel here to get span in channel's context in async call..
+        Context context = ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).get();
+        Span span = context.get(SpanKey.SERVER_KEY);
+        try (Scope ignored = context.makeCurrent()) {
+            span.setStatus(StatusCode.ERROR);//set this span's status to ERROR
+            span.end();// closing the scope does not end the span, this has to be done manually
+        }
         ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
     }
 
     public void sendResponse(ChannelHandlerContext ctx,
                              DefaultFullHttpResponse response) {
-        // todo end server span, we should get channel here to get span in channel's context in async call.
+        //todo end server span, we should get channel here to get span in channel's context in async call.
+        Context context = ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).get();
+        Span span = context.get(SpanKey.SERVER_KEY);
+        try (Scope ignored = context.makeCurrent()) {
+            span.addEvent("Send Response");

Review comment:
       no need to add event about response 

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -132,13 +142,26 @@ public void sendError(ChannelHandlerContext ctx,
                 "; charset=" + EventMeshConstants.DEFAULT_CHARSET);
         response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
         response.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
-        // todo server span end with error, record status, we should get channel here to get span in channel's context in async call..
+        //todo server span end with error, record status, we should get channel here to get span in channel's context in async call..

Review comment:
       You may need to add a switch to determine whether you want to enter here

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -298,7 +347,11 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest)
                 processEventMeshRequest(ctx, asyncContext);
             } catch (Exception ex) {
                 httpServerLogger.error("AbrstractHTTPServer.HTTPHandler.channelRead0 err", ex);
-                // todo span end with exception.
+                //todo span end with exception.
+                span.addEvent("End Exceptional");

Review comment:
       we should add an exception here, but we need to record the cause in event atttribute

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -132,13 +142,26 @@ public void sendError(ChannelHandlerContext ctx,
                 "; charset=" + EventMeshConstants.DEFAULT_CHARSET);
         response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
         response.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
-        // todo server span end with error, record status, we should get channel here to get span in channel's context in async call..
+        //todo server span end with error, record status, we should get channel here to get span in channel's context in async call..
+        Context context = ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).get();
+        Span span = context.get(SpanKey.SERVER_KEY);
+        try (Scope ignored = context.makeCurrent()) {
+            span.setStatus(StatusCode.ERROR);//set this span's status to ERROR
+            span.end();// closing the scope does not end the span, this has to be done manually
+        }
         ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
     }
 
     public void sendResponse(ChannelHandlerContext ctx,
                              DefaultFullHttpResponse response) {
-        // todo end server span, we should get channel here to get span in channel's context in async call.
+        //todo end server span, we should get channel here to get span in channel's context in async call.

Review comment:
       You may need to add a switch to determine whether you want to enter here

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/trace/OpenTelemetryTraceFactory.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.runtime.trace;
+
+import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.api.trace.Tracer;
+import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
+import io.opentelemetry.context.propagation.ContextPropagators;
+import io.opentelemetry.context.propagation.TextMapPropagator;
+import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.resources.Resource;
+import io.opentelemetry.sdk.trace.SdkTracerProvider;
+import io.opentelemetry.sdk.trace.SpanProcessor;
+import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
+import io.opentelemetry.sdk.trace.export.SpanExporter;
+import org.apache.eventmesh.common.config.CommonConfiguration;
+import org.apache.eventmesh.runtime.exporter.EventMeshExporter;
+import org.apache.eventmesh.runtime.exporter.LogExporter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.TimeUnit;
+import static io.opentelemetry.api.common.AttributeKey.stringKey;
+
+public class OpenTelemetryTraceFactory {
+    private static final Logger logger = LoggerFactory.getLogger(OpenTelemetryTraceFactory.class);
+
+    private OpenTelemetry openTelemetry;
+
+    private SpanExporter spanExporter;
+
+    private SpanExporter defaultExporter = new LogExporter();
+
+    private SpanProcessor spanProcessor;
+
+    // Name of the service(using the instrumentationName)
+    private final String SERVICE_NAME = "eventmesh_trace";
+
+    public OpenTelemetryTraceFactory(CommonConfiguration configuration){
+        try {
+            //different spanExporter
+            String exporterName = configuration.eventMeshTraceExporterType;
+            //use reflection to get spanExporter
+            String className = String.format("org.apache.eventmesh.runtime.exporter.%sExporter",exporterName);
+            EventMeshExporter eventMeshExporter = (EventMeshExporter) Class.forName(className).newInstance();
+            spanExporter = eventMeshExporter.getSpanExporter(configuration);
+        }catch (Exception ex){
+            logger.error("fail to set tracer's exporter,due to{}",ex.getMessage());

Review comment:
       fail to set tracer's exporter,due to{} 
   add a blank before{}

##########
File path: eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java
##########
@@ -256,7 +294,18 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpRequest httpRequest)
                 requestCommand.setHttpMethod(httpRequest.method().name());
                 requestCommand.setHttpVersion(httpRequest.protocolVersion().protocolName());
                 requestCommand.setRequestCode(requestCode);
-                // todo record command method, version and requestCode in span.
+
+                //todo record command opaque in span.
+                span = tracer.spanBuilder("HTTP "+requestCommand.httpMethod).setParent(context).setSpanKind(SpanKind.SERVER).startSpan();
+                span.addEvent("Span Start");
+                //attach the span to the server context
+                context = context.with(SpanKey.SERVER_KEY,span);
+                //put the context in channel
+                ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).set(context);
+                //todo record command method, version and requestCode in span.
+                span.setAttribute("HttpMethod",httpRequest.method().name());
+                span.setAttribute("HttpVersion",httpRequest.protocolVersion().protocolName());
+                span.setAttribute("RequestCode",requestCode);

Review comment:
       ues otel key instaed : https://github.com/open-telemetry/opentelemetry-java/blob/main/semconv/src/main/java/io/opentelemetry/semconv/trace/attributes/SemanticAttributes.java




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org