Grouping with Sentry

Juan David :

I want to group exception with Sentry, the exception comes from different servers, but I want all exception by type together, for example, all NPE be grouped. I know you can extend EventBuilderHelper and this is how sentry group things, but sentry java doesn't provide features to send an event with fingerprints of the method, error type, etc, as others SDK like this example in docs.sentry.io

function makeRequest(method, path, options) {
    return fetch(method, path, options).catch(err => {
        Sentry.withScope(scope => {
          // group errors together based on their request and response
          scope.setFingerprint([method, path, err.statusCode]);
          Sentry.captureException(err);
        });
    });
}

this is what I try to do, but in this scope, don't have knowledge about method, error, etc.

package com.test;

import io.sentry.SentryClient;
import io.sentry.event.EventBuilder;
import io.sentry.event.helper.ContextBuilderHelper;

public class FingerprintEventBuilderHelper extends ContextBuilderHelper {

    private static final String EXCEPTION_TYPE = "exception_type";

    public FingerprintEventBuilderHelper(SentryClient sentryClient) {
        super(sentryClient);
    }

    @Override
    public void helpBuildingEvent(EventBuilder eventBuilder) {
        super.helpBuildingEvent(eventBuilder);
        //Get the exception type
        String exceptionType =
        if (exceptionType != null) {
            eventBuilder.withTag(EXCEPTION_TYPE, exceptionType);
        }
        //Get method information and params
        if (paramX != null) {
            eventBuilder.withTag("PARAM", paramX);
        }
    }
}

the json send to the server has some information about the exception, but I dont know hoy to get it

...
    "release": null,
    "dist": null,
    "platform": "java",
    "culprit": "com.sun.ejb.containers.BaseContainer in checkExceptionClientTx",
    "message": "Task execution failed",
    "datetime": "2019-06-26T14:13:29.000000Z",
    "time_spent": null,
    "tags": [
        ["logger", "com.test.TestService"],
        ["server_name", "localhost"],
        ["level", "error"]
    ],
    "errors": [],
    "extra": {
        "Sentry-Threadname": "MainThread",
        "rid": "5ff37e943-f4b4-4hc9-870b-4f8c4d18cf84"
    },
    "fingerprint": ["{{ default }}"],
    "key_id": 3,
    "metadata": {
        "type": "NullPointerException",
        "value": ""
    },
...
Tarun Lalwani :

You can get the type of exception that was raised, but I have my doubts about getting the parameters related to functions in the trace

EventBuilderHelper myEventBuilderHelper = new EventBuilderHelper() {
    public void helpBuildingEvent(EventBuilder eventBuilder) {
        eventBuilder.withMessage("Overwritten by myEventBuilderHelper!");

        Map<String, SentryInterface> ifs = eventBuilder.getEvent().getSentryInterfaces();
        if (ifs.containsKey("sentry.interfaces.Exception"))
        {
            ExceptionInterface exI = (ExceptionInterface) ifs.get("sentry.interfaces.Exception");
            for (SentryException ex: exI.getExceptions()){
                String exceptionType = ex.getExceptionClassName();
            }
        }
    }
};

If you look at the sendException method of the client, it initiates the ExceptionInterface with the actual Exception

public void sendException(Throwable throwable) {
    EventBuilder eventBuilder = (new EventBuilder()).withMessage(throwable.getMessage()).withLevel(Level.ERROR).withSentryInterface(new ExceptionInterface(throwable));
    this.sendEvent(eventBuilder);
}

And the constructor for the same is like

public ExceptionInterface(Throwable throwable) {
  this(SentryException.extractExceptionQueue(throwable));
}

public ExceptionInterface(Deque<SentryException> exceptions) {
  this.exceptions = exceptions;
}

So each exception get converted to a SentryException, but the original exception is not stored. So if you need params also, you will need to throw a custom exception with those parameter and also override the sendException method, not a straightforward way

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=121375&siteId=1