Search code examples
javascriptsentry

How do I customise the Sentry transport?


Using "@sentry/browser": "5.30.0".

How do I configure Sentry so that I can customise the messages that Sentry sends?

My reading of the documentation is that the transport property is intended to allow this, but I can't figure out what I'm supposed to pass as the argument.

Sentry.init({
  dsn: '...',
  autoSessionTracking: false,
  transport: (options: TransportOptions)=>{
    return new MyFetchTransport(options);
  },
});

class MyFetchTransport extends FetchTransport {
  constructor(options: TransportOptions){
    super(options);
  }

  sendEvent(event: Event): PromiseLike<Response>{
    return super.sendEvent(event);
  }

  sendSession(session: Session): PromiseLike<Response>{
    return super.sendSession(session);
  }
}

The above gives a Typescript error of:

Type '(options: TransportOptions) => MyFetchTransport' is not assignable to type 'TransportClass<Transport>'.
  Type '(options: TransportOptions) => MyFetchTransport' provides no match for the signature 'new (options: TransportOptions): Transport'.  TS2322

What should the init invocation look like if I want to customise how Sentry sends messages?


Solution

  • Found an example in the Sentry docs, the init() call should look like:

    Sentry.init({
      dsn: '...',
      autoSessionTracking: false,
      transport: MyFetchTransport,
    });