Skip to content

Instantly share code, notes, and snippets.

@brunos3d
Last active September 15, 2023 13:54
Show Gist options
  • Save brunos3d/9a0c61c6a7a25543e68af2a4530ecb4a to your computer and use it in GitHub Desktop.
Save brunos3d/9a0c61c6a7a25543e68af2a4530ecb4a to your computer and use it in GitHub Desktop.
Sentry Hubs on Microfrontends with Module Federation

Remote that shares the components defines a Sentry.hub instance defining some tags that will be used by the host during the dispatch of the logs to Sentry server

import * as Sentry from "@sentry/browser";

const hub = new Sentry.Hub();

hub.configureScope((scope) => {
  scope.setTag("Host", "shop");
  scope.setTag("Team", "shop-team");
});

export default hub;

Some shared/federated component on the same remote as the hub.

import { ErrorInfo } from "react";
import { ErrorBoundary } from "react-error-boundary";
import hub from './hub';

function Content(props: any) {
  function onError(error: Error, errorInfo: ErrorInfo) {
    hub.withScope((scope) => {
      scope.setExtra('remote', 'checkout');
      scope.setExtra('errorInfo', errorInfo);
      hub.captureException(error);
    });
  }

  return (
    <ErrorBoundary
      onError={onError}
      fallback={<div>An error occured while rendering checkout.</div>}
    >
      <Checkout />
    </ErrorBoundary>
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment