You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-issues@hadoop.apache.org by GitBox <gi...@apache.org> on 2022/03/21 18:45:55 UTC

[GitHub] [hadoop] steveloughran commented on a change in pull request #3445: HADOOP-15566 Opentelemetry changes using java agent

steveloughran commented on a change in pull request #3445:
URL: https://github.com/apache/hadoop/pull/3445#discussion_r831412903



##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/Span.java
##########
@@ -17,28 +17,54 @@
  */
 package org.apache.hadoop.tracing;
 
+import io.opentelemetry.context.Scope;
+
 import java.io.Closeable;
 
 public class Span implements Closeable {
-
+  private io.opentelemetry.api.trace.Span span = null;

Review comment:
       1. add some javadoc to line 24 to warn this wraps the opentelemetry span.
   2. skip the =null assignment, as it will save the jvm from some needless init. assuming a lot of spans get created, this may matter
   3. give the field a different name. like 'openspan'
   
   

##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/Span.java
##########
@@ -17,28 +17,54 @@
  */
 package org.apache.hadoop.tracing;
 
+import io.opentelemetry.context.Scope;
+
 import java.io.Closeable;
 
 public class Span implements Closeable {
-
+  private io.opentelemetry.api.trace.Span span = null;
   public Span() {
   }
 
+  public Span(io.opentelemetry.api.trace.Span span){
+    this.span = span;
+  }
+
   public Span addKVAnnotation(String key, String value) {
+    if(span != null){
+      span.setAttribute(key, value);
+    }
     return this;
   }
 
   public Span addTimelineAnnotation(String msg) {
+    if(span != null){
+      span.addEvent(msg);
+    }
     return this;
   }
 
   public SpanContext getContext() {
+    if(span != null){
+      return  new SpanContext(span.getSpanContext());
+    }
     return null;
   }
 
   public void finish() {
+    close();
   }
 
   public void close() {
+    if(span != null){
+      span.end();
+    }
+  }
+
+  public Scope makeCurrent() {

Review comment:
       nit: add javadocs

##########
File path: hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/tracing/TestTracer.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.hadoop.tracing;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;

Review comment:
       * should extend AbstractHadoopTestBase or HadoopTestBase here.
   *  there is scope for a lot more tests

##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/Span.java
##########
@@ -17,28 +17,54 @@
  */
 package org.apache.hadoop.tracing;
 
+import io.opentelemetry.context.Scope;
+
 import java.io.Closeable;
 
 public class Span implements Closeable {
-
+  private io.opentelemetry.api.trace.Span span = null;
   public Span() {
   }
 
+  public Span(io.opentelemetry.api.trace.Span span){
+    this.span = span;
+  }
+
   public Span addKVAnnotation(String key, String value) {
+    if(span != null){
+      span.setAttribute(key, value);
+    }
     return this;
   }
 
   public Span addTimelineAnnotation(String msg) {
+    if(span != null){
+      span.addEvent(msg);
+    }
     return this;
   }
 
   public SpanContext getContext() {
+    if(span != null){
+      return  new SpanContext(span.getSpanContext());
+    }
     return null;
   }
 
   public void finish() {
+    close();
   }
 
   public void close() {
+    if(span != null){
+      span.end();

Review comment:
       would span need to be nullified here. or is it ok to invoke it after being ended?

##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/SpanContext.java
##########
@@ -18,15 +18,75 @@
 package org.apache.hadoop.tracing;
 
 import java.io.Closeable;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.TraceFlags;
+import io.opentelemetry.api.trace.TraceState;
+import io.opentelemetry.api.trace.TraceStateBuilder;
+import org.apache.hadoop.thirdparty.protobuf.ByteString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Wrapper class for SpanContext to avoid using OpenTracing/OpenTelemetry
  * SpanContext class directly for better separation.
  */
-public class SpanContext implements Closeable {
-  public SpanContext() {
+public class SpanContext implements Closeable  {
+  public static final Logger LOG = LoggerFactory.getLogger(SpanContext.class.getName());
+  private static final String TRACE_ID = "TRACE_ID";
+  private static final String SPAN_ID = "SPAN_ID";
+  private static final String TRACE_FLAGS = "TRACE_FLAGS";
+
+
+  private io.opentelemetry.api.trace.SpanContext spanContext = null;
+  public SpanContext(io.opentelemetry.api.trace.SpanContext spanContext) {
+    this.spanContext = spanContext;
   }
 
   public void close() {
+
+  }
+
+  public Map<String, String> getKVSpanContext(){
+    if(spanContext != null){
+      //TODO: may we should move this to Proto
+      Map<String, String> kvMap = new HashMap<>();
+      kvMap.put(TRACE_ID, spanContext.getTraceId());
+      kvMap.put(SPAN_ID, spanContext.getSpanId());
+      kvMap.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
+      kvMap.putAll(spanContext.getTraceState().asMap());
+      return kvMap;
+    }
+    return null;
+  }
+
+  static SpanContext buildFromKVMap(Map<String, String> kvMap){
+    try{
+      String traceId = kvMap.get(TRACE_ID);
+      String spanId = kvMap.get(SPAN_ID);
+      String traceFlagsHex = kvMap.get(TRACE_FLAGS);
+      TraceFlags traceFlags = TraceFlags.fromHex(traceFlagsHex, 0);

Review comment:
        what if traceFlagsHex isn't found? exit fast

##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/SpanContext.java
##########
@@ -18,15 +18,75 @@
 package org.apache.hadoop.tracing;
 
 import java.io.Closeable;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.TraceFlags;
+import io.opentelemetry.api.trace.TraceState;
+import io.opentelemetry.api.trace.TraceStateBuilder;
+import org.apache.hadoop.thirdparty.protobuf.ByteString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Wrapper class for SpanContext to avoid using OpenTracing/OpenTelemetry
  * SpanContext class directly for better separation.
  */
-public class SpanContext implements Closeable {
-  public SpanContext() {
+public class SpanContext implements Closeable  {
+  public static final Logger LOG = LoggerFactory.getLogger(SpanContext.class.getName());
+  private static final String TRACE_ID = "TRACE_ID";
+  private static final String SPAN_ID = "SPAN_ID";
+  private static final String TRACE_FLAGS = "TRACE_FLAGS";
+
+
+  private io.opentelemetry.api.trace.SpanContext spanContext = null;

Review comment:
       again, give a different field name to avoid more confusion; skip the assignment to null

##########
File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/TraceUtils.java
##########
@@ -20,28 +20,36 @@
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.thirdparty.protobuf.ByteString;
+import org.slf4j.Logger;

Review comment:
       nit: import rules/ordering




-- 
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: common-issues-unsubscribe@hadoop.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-issues-help@hadoop.apache.org