-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(feedback): use custom prepare/send, re-use createEventEnvelope from core #9432
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6cdc0d
add Feedback types
billyvg d3633c0
feat: send feedback envelopes using `captureEvent`
billyvg c193917
feat: use custom prepare/send, re-use createEventEnvelope from core
billyvg 6c6cd63
remove debugger
billyvg 37cb3c9
remove console.log
billyvg 2e55764
export the correct function
billyvg 210c65f
clean up
billyvg e53b283
lint
billyvg a8faa72
update comment, fix test
billyvg 380d8fe
debug build
billyvg 07fa7cd
fix test
billyvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { sendFeedbackRequest } from './util/sendFeedbackRequest'; | ||
export { sendFeedback } from './sendFeedback'; | ||
export { Feedback } from './integration'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,36 @@ | ||
import { getCurrentHub } from '@sentry/core'; | ||
import { dsnToString } from '@sentry/utils'; | ||
import { createEventEnvelope, getCurrentHub } from '@sentry/core'; | ||
import type { FeedbackEvent, TransportMakeRequestResponse } from '@sentry/types'; | ||
|
||
import type { SendFeedbackData } from '../types'; | ||
import { prepareFeedbackEvent } from './prepareFeedbackEvent'; | ||
|
||
/** | ||
* Send feedback using `fetch()` | ||
* Send feedback using transport | ||
*/ | ||
export async function sendFeedbackRequest({ | ||
feedback: { message, email, name, replay_id, url }, | ||
}: SendFeedbackData): Promise<Response | null> { | ||
}: SendFeedbackData): Promise<void | TransportMakeRequestResponse> { | ||
const hub = getCurrentHub(); | ||
|
||
if (!hub) { | ||
return null; | ||
} | ||
|
||
const client = hub.getClient(); | ||
const scope = hub.getScope(); | ||
const transport = client && client.getTransport(); | ||
const dsn = client && client.getDsn(); | ||
|
||
if (!client || !transport || !dsn) { | ||
return null; | ||
return; | ||
} | ||
|
||
const baseEvent = { | ||
feedback: { | ||
contact_email: email, | ||
name, | ||
message, | ||
replay_id, | ||
url, | ||
const baseEvent: FeedbackEvent = { | ||
contexts: { | ||
feedback: { | ||
contact_email: email, | ||
name, | ||
message, | ||
replay_id, | ||
url, | ||
}, | ||
}, | ||
// type: 'feedback_event', | ||
type: 'feedback', | ||
}; | ||
|
||
const feedbackEvent = await prepareFeedbackEvent({ | ||
|
@@ -42,72 +39,80 @@ export async function sendFeedbackRequest({ | |
event: baseEvent, | ||
}); | ||
|
||
if (!feedbackEvent) { | ||
// Taken from baseclient's `_processEvent` method, where this is handled for errors/transactions | ||
// client.recordDroppedEvent('event_processor', 'feedback', baseEvent); | ||
return null; | ||
if (feedbackEvent === null) { | ||
return; | ||
} | ||
|
||
/* | ||
For reference, the fully built event looks something like this: | ||
{ | ||
"data": { | ||
"dist": "abc123", | ||
"type": "feedback", | ||
"event_id": "d2132d31b39445f1938d7e21b6bf0ec4", | ||
"timestamp": 1597977777.6189718, | ||
"dist": "1.12", | ||
"platform": "javascript", | ||
"environment": "production", | ||
"feedback": { | ||
"contact_email": "[email protected]", | ||
"message": "I really like this user-feedback feature!", | ||
"replay_id": "ec3b4dc8b79f417596f7a1aa4fcca5d2", | ||
"url": "https://docs.sentry.io/platforms/javascript/" | ||
"release": 42, | ||
"tags": {"transaction": "/organizations/:orgId/performance/:eventSlug/"}, | ||
"sdk": {"name": "name", "version": "version"}, | ||
"user": { | ||
"id": "123", | ||
"username": "user", | ||
"email": "[email protected]", | ||
"ip_address": "192.168.11.12", | ||
}, | ||
"id": "1ffe0775ac0f4417aed9de36d9f6f8dc", | ||
"platform": "javascript", | ||
"release": "[email protected]", | ||
"request": { | ||
"headers": { | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" | ||
} | ||
}, | ||
"sdk": { | ||
"name": "sentry.javascript.react", | ||
"version": "6.18.1" | ||
"url": None, | ||
"headers": { | ||
"user-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15" | ||
}, | ||
}, | ||
"tags": { | ||
"key": "value" | ||
"contexts": { | ||
"feedback": { | ||
"message": "test message", | ||
"contact_email": "[email protected]", | ||
"type": "feedback", | ||
}, | ||
"trace": { | ||
"trace_id": "4C79F60C11214EB38604F4AE0781BFB2", | ||
"span_id": "FA90FDEAD5F74052", | ||
"type": "trace", | ||
}, | ||
"replay": { | ||
"replay_id": "e2d42047b1c5431c8cba85ee2a8ab25d", | ||
}, | ||
}, | ||
"timestamp": "2023-08-31T14:10:34.954048", | ||
"user": { | ||
"email": "[email protected]", | ||
"id": "123", | ||
"ip_address": "127.0.0.1", | ||
"name": "user", | ||
"username": "user2270129" | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
// Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to | ||
// sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may | ||
// have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid | ||
// of this `delete`, lest we miss putting it back in the next time the property is in use.) | ||
delete feedbackEvent.sdkProcessingMetadata; | ||
const envelope = createEventEnvelope(feedbackEvent, dsn, client.getOptions()._metadata, client.getOptions().tunnel); | ||
|
||
let response: void | TransportMakeRequestResponse; | ||
|
||
try { | ||
const path = 'https://sentry.io/api/0/feedback/'; | ||
const response = await fetch(path, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `DSN ${dsnToString(dsn)}`, | ||
}, | ||
body: JSON.stringify(feedbackEvent), | ||
}); | ||
if (!response.ok) { | ||
return null; | ||
response = await transport.send(envelope); | ||
} catch (err) { | ||
const error = new Error('Unable to send Feedback'); | ||
|
||
try { | ||
// In case browsers don't allow this property to be writable | ||
// @ts-expect-error This needs lib es2022 and newer | ||
error.cause = err; | ||
} catch { | ||
// nothing to do | ||
} | ||
throw error; | ||
} | ||
|
||
// TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore | ||
if (!response) { | ||
return response; | ||
} catch (err) { | ||
return null; | ||
} | ||
|
||
// Require valid status codes, otherwise can assume feedback was not sent successfully | ||
if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) { | ||
throw new Error('Unable to send Feedback'); | ||
} | ||
|
||
return response; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we good to export this @mydea?
This also means we'll have to wait for an SDK release before we can release a new version of the SDK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine by me! 👍