zipkin-aws icon indicating copy to clipboard operation
zipkin-aws copied to clipboard

Support XRay Exceptions

Open cemo opened this issue 8 years ago • 6 comments

XRay has nice support for Exceptions. It would be cool to support it.

cemo avatar Nov 10 '17 06:11 cemo

Feel free to paste your earlier code here, wrapped in ``` for an example. And also offer potential integration (ex via HttpClientParser)

codefromthecrypt avatar Nov 10 '17 07:11 codefromthecrypt

import com.squareup.moshi.JsonWriter;
import java.io.IOException;
import okio.Buffer;

/**
 * This class is an utility class providing some basic ways to create simple JSON's.
 * Since a span can be annotated by only a String value, passing input as JSON is required.
 */
public final class XRayFormatter {

  /**
   *
   * Formating an exception to be consumed by XRay by Spans annotations
   *
   * @param exceptionId an unique exception id @see brave.internal.Platform#randomLong()
   * @param isRemote Any calls to a remote service/broker should be true
   * @param maxStackTraceElement XRay has a limit of 60KB per segment. Choose a wise number
   * @param throwable The exception occured
   * @return a JSON String which is ready to be consumed by XRay "cause.exception"
   * @throws IOException
   */
  public static String formatCause(String exceptionId, boolean isRemote, int maxStackTraceElement,
                                   Throwable throwable) throws IOException {
    Buffer buffer = new Buffer();
    JsonWriter writer = JsonWriter.of(buffer);
    writer.beginArray();
    writer.beginObject();
    writer.name("id").value(exceptionId);
    writer.name("type").value(throwable.getClass().getName());
    writer.name("remote").value(isRemote);
    writer.name("stack").beginArray();
    StackTraceElement[] stackTrace = throwable.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
      StackTraceElement aTrace = stackTrace[i];
      writer.beginObject();
      writer.name("path").value(aTrace.getFileName());
      writer.name("line").value(aTrace.getLineNumber());
      writer.name("label").value(aTrace.getClassName() + "." + aTrace.getMethodName());
      writer.endObject();
      if(maxStackTraceElement < i) break;
    }
    writer.endArray();
    writer.name("truncated").value(stackTrace.length > maxStackTraceElement);
    writer.endObject();
    writer.endArray();
    return buffer.readByteString().utf8();
  }

}

I am in the process of integrations and testing.

cemo avatar Nov 10 '17 07:11 cemo

It seems like we would need support in brave for this functionality, either by giving us access to the Throwables or some ExceptionTagBuilder on the tracer that could be backend specific.

I'm not sure the latter would be flexible enough in this case, the XRay spec allows multiple exception objects so we couldn't just pass a single exception and build the string right then, we'd need all the exceptions in order to do that.

devinsba avatar Mar 27 '18 18:03 devinsba

right now, exception management isn't really a feature as you noticed.. This might be possible as a context rider, ex an "extra" thing that holds exceptions for a span?

codefromthecrypt avatar Mar 28 '18 00:03 codefromthecrypt

That's about what I was thinking. I'm mulling it over now. It seems like a lot of work to support a single backed, though once it's there maybe people will want this functionality in zipkin or stackdriver might have something similar?

devinsba avatar Mar 28 '18 00:03 devinsba

I look at it as a nice way to experiment with a feature :) might not work out, but maybe worth exploring.

codefromthecrypt avatar Mar 28 '18 00:03 codefromthecrypt