Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support XRay Exceptions #60

Open
cemo opened this issue Nov 10, 2017 · 6 comments
Open

Support XRay Exceptions #60

cemo opened this issue Nov 10, 2017 · 6 comments
Labels
X-Ray AWS X-Ray

Comments

@cemo
Copy link
Contributor

cemo commented Nov 10, 2017

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

@codefromthecrypt
Copy link
Member

codefromthecrypt commented Nov 10, 2017 via email

@cemo
Copy link
Contributor Author

cemo commented Nov 10, 2017

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.

@devinsba
Copy link
Member

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 devinsba added the X-Ray AWS X-Ray label Mar 27, 2018
@codefromthecrypt
Copy link
Member

codefromthecrypt commented Mar 28, 2018 via email

@devinsba
Copy link
Member

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?

@codefromthecrypt
Copy link
Member

codefromthecrypt commented Mar 28, 2018 via email

@devinsba devinsba mentioned this issue Dec 6, 2018
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
X-Ray AWS X-Ray
Projects
None yet
Development

No branches or pull requests

3 participants