Skip to content

Why breadcrumb variables are stringified? (wrongly filtered data) (workaround available) #485

@Martinocom-Switcho

Description

@Martinocom-Switcho

Hello, and thank you for simplifying our world on managing details sent to Sentry! I have a question about how the data is managed before sent.

The problem

In very simple words, our use-case is about filtering + performance optimisations.

  1. We expected to have details returned as object, within apply Sentry Filters (example, filtering userId)
    • But we got a string that gets entirely filtered
  2. Library serializes data, that internally we deserialize before sending them
    • wasting precious computational time

Image

Our workaround

In order to "fix" this, in our code we are wasting some computational time to deserialize previously serialized data, with context and variables keys.

function parseData(breadcrumb: Breadcrumb, key: string) {
  const breadcrumbData = breadcrumb.data;
  if (!breadcrumbData || !(key in breadcrumbData)) {
    // No interesting data, just return breadcrumb
    return breadcrumb;
  }

  // Extract data "as object" or fail silently
  try {
    breadcrumbData[key] = JSON.parse(breadcrumbData[key]);
  } catch {}

  return breadcrumb;
}

function parseVariables(breadcrumb: Breadcrumb) {
  return parseData(breadcrumb, 'variables');
}

function parseContext(breadcrumb: Breadcrumb) {
  return parseData(breadcrumb, 'context');
}


Sentry.init({
    beforeBreadcrumb: (breadcrumb, _) => {
      // Drop meaningless breadcrumbs before sending to sentry
      if (breadcrumbToIgnore(breadcrumb)) {
        return null;
      }

      // Parse context and variables before sending breadcrumb
      return parseVariables(parseContext(breadcrumb));
    }
});

Analyzing the code

Exploring the code I saw that breadcrumbs are added with attachBreadcrumbToSentry(operation, breadcrumb, options);

Why we need to stringify the object here?

...
  transformed.data = stringifyObjectKeys(
    transformed.data as Record<string, unknown>,
  );
...

The question

Why this is necessary? Am I missing some point on serializing data? Is there a way to disable serializing in order to remove our "workaround"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions