Skip to content

Attachment API isn't working as expected #5080

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

Closed
3 tasks done
yousefa00 opened this issue May 11, 2022 · 2 comments
Closed
3 tasks done

Attachment API isn't working as expected #5080

yousefa00 opened this issue May 11, 2022 · 2 comments

Comments

@yousefa00
Copy link

yousefa00 commented May 11, 2022

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

6.19.6

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

Any idea why this might not be working for me? I have the following Sentry configuration:

const attachmentUrlFromDsn = (dsn: DsnComponents, eventId: string) => {
  const { host, path, projectId, port, protocol, user } = dsn
  return `${protocol}://${host}${port !== '' ? `:${port}` : ''}${
    path !== '' ? `/${path}` : ''
  }/api/${projectId}/events/${eventId}/attachments/?sentry_key=${user}&sentry_version=7&sentry_client=custom-javascript`
}

export const attachAsJson = async (
  attachments: string[],
  event: Sentry.Event,
  client: any
) => {
  const dsn = client?.getDsn()

  if (!dsn || !event.event_id) {
    return
  }

  const endpoint = attachmentUrlFromDsn(dsn, event.event_id)
  logger.info(endpoint)

  const formData = new FormData()
  for (const attachment of attachments) {
    formData.append('attachments', attachment)
  }
  logger.info(formData)

  try {
    await axios.request({
      method: 'POST',
      url: endpoint,
      data: formData,
      headers: {
        'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`
      }
    })
    return event
  } catch (err) {
    logger.error(err)
    return null
  }
}

And include this where the attachment is in scope:

Sentry.addGlobalEventProcessor(async event => {
    try {
      const client = Sentry.getCurrentHub().getClient()
      if (!client) {
        return null
      }
      await attachAsJson([attachment], event, client)
    } catch (ex) {
      logger.error(ex)
    }
    return event
  })

Expected Result

An event is created with the relevant attachment.

Actual Result

I see the Sentry events created correctly, and logging the attachment API request status code is 201 created, yet there are no attachments on the actual event created. Any help would be greatly appreciated.

@smeubank
Copy link
Member

hi @yousefa00

there is an existing issue requesting proper Attachments API support, and this PR which should be merged relatively soon in a V7 beta release #5004. I might be best to look for that unless you need it right away.

@vladanpaunovic
Copy link
Contributor

Attachments API was released in v7. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants