jojochuang commented on a change in pull request #1846: HADOOP-15566. Support 
Opentracing
URL: https://github.com/apache/hadoop/pull/1846#discussion_r382861120
 
 

 ##########
 File path: 
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tracing/TraceUtils.java
 ##########
 @@ -72,4 +97,71 @@ private String getInternal(String key) {
       }
     };
   }
+
+  public static Tracer createAndRegisterTracer(String name) {
+    if (!GlobalTracer.isRegistered()) {
+      io.jaegertracing.Configuration config =
+          io.jaegertracing.Configuration.fromEnv(name);
+      Tracer tracer = config.getTracerBuilder().build();
+      GlobalTracer.register(tracer);
+    }
+
+    return GlobalTracer.get();
+  }
+
+  public static SpanContext byteStringToSpanContext(ByteString byteString) {
+    if (byteString == null || byteString.isEmpty()) {
+      LOG.debug("The provided serialized context was null or empty");
+      return null;
+    }
+
+    SpanContext context = null;
+    ByteArrayInputStream stream =
+        new ByteArrayInputStream(byteString.toByteArray());
+
+    try {
+      ObjectInputStream objStream = new ObjectInputStream(stream);
+      Map<String, String> carrier =
+          (Map<String, String>) objStream.readObject();
+
+      context = GlobalTracer.get().extract(Format.Builtin.TEXT_MAP,
+          new TextMapExtractAdapter(carrier));
+    } catch (Exception e) {
+      LOG.warn("Could not deserialize context {}", e);
+    }
+
+    return context;
+  }
+
+  public static ByteString spanContextToByteString(SpanContext context) {
+    if (context == null) {
+      LOG.debug("No SpanContext was provided");
+      return null;
+    }
+
+    Map<String, String> carrier = new HashMap<String, String>();
+    GlobalTracer.get().inject(context, Format.Builtin.TEXT_MAP,
+        new TextMapInjectAdapter(carrier));
+    if (carrier.isEmpty()) {
+      LOG.warn("SpanContext was not properly injected by the Tracer.");
+      return null;
+    }
+
+    ByteString byteString = null;
+    ByteArrayOutputStream stream = new ByteArrayOutputStream();
+
+    try {
+      ObjectOutputStream objStream = new ObjectOutputStream(stream);
+      objStream.writeObject(carrier);
+      objStream.flush();
+
+      byteString = ByteString.copyFrom(stream.toByteArray());
+      LOG.debug("SpanContext serialized, resulting byte length is {}",
+          byteString.size());
+    } catch (IOException e) {
+      LOG.warn("Could not serialize context {}", e);
 
 Review comment:
   missing a parameter here?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to