Repository navigation
feat(cli): Retry writes Sentry marks idempotent #1706
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89a19d8
feat(cli): Retry writes Sentry marks idempotent
sentry-junior[bot] 4ce8373
fix(cli): Commit the idempotent operations list
sentry-junior[bot] df7b3e5
fix(cli): Read the idempotent catalog at runtime
sentry-junior[bot] 01f7b77
fix(cli): Generate the idempotent operations list at build time
sentry-junior[bot] 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [ | ||
| { | ||
| "method": "POST", | ||
| "path": "/api/0/organizations/{organization_id_or_slug}/artifactbundle/assemble/" | ||
| }, | ||
| { | ||
| "method": "POST", | ||
| "path": "/api/0/organizations/{organization_id_or_slug}/chunk-upload/" | ||
| }, | ||
| { | ||
| "method": "POST", | ||
| "path": "/api/0/projects/{organization_id_or_slug}/{project_id_or_slug}/files/difs/assemble/" | ||
| } | ||
| ] |
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
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Writes Sentry declares safe to replay. | ||
| * | ||
| * `script/generate-api-schema.ts` copies the non-safe operations that the | ||
| * `@sentry/api` catalog marks `idempotent` (from Sentry's | ||
| * `x-sentry-idempotent` OpenAPI extension). Endpoints opt in on the server, | ||
| * so the CLI no longer hard-codes which assemble and upload calls may retry. | ||
| * The generated file is committed and refreshed by `generate:schema`; commit | ||
| * it again after bumping `@sentry/api`. | ||
| */ | ||
|
|
||
| import generated from "../generated/idempotent-operations.json"; | ||
|
|
||
| /** A non-safe operation Sentry marks idempotent. */ | ||
| export type IdempotentOperation = { | ||
| /** Uppercase HTTP method */ | ||
| method: string; | ||
| /** OpenAPI path template, such as `/api/0/organizations/{org}/chunk-upload/` */ | ||
| path: string; | ||
| }; | ||
|
|
||
| type Matcher = { method: string; pattern: RegExp }; | ||
|
|
||
| const TEMPLATE_SEGMENT = /^\{[^/]+\}$/; | ||
| const REGEXP_SPECIAL = /[.*+?^${}()|[\]\\]/g; | ||
|
|
||
| function toMatcher({ method, path }: IdempotentOperation): Matcher { | ||
| const source = path | ||
| .split("/") | ||
| .map((segment) => | ||
| TEMPLATE_SEGMENT.test(segment) | ||
| ? "[^/]+" | ||
| : segment.replace(REGEXP_SPECIAL, "\\$&"), | ||
| ) | ||
| .join("/"); | ||
| return { method: method.toUpperCase(), pattern: new RegExp(`^${source}$`) }; | ||
| } | ||
|
|
||
| let matchers: Matcher[] | undefined; | ||
|
|
||
| /** | ||
| * Whether Sentry marks this request's operation idempotent. | ||
| * | ||
| * Matches from the `/api/` segment on, so a self-hosted install served under | ||
| * a path prefix still matches. | ||
| * | ||
| * @param method - HTTP method (any case) | ||
| * @param url - Absolute request URL | ||
| */ | ||
| export function isIdempotentOperation(method: string, url: string): boolean { | ||
| if (!URL.canParse(url)) { | ||
| return false; | ||
| } | ||
| const pathname = new URL(url).pathname; | ||
| const apiIndex = pathname.indexOf("/api/"); | ||
| if (apiIndex === -1) { | ||
| return false; | ||
| } | ||
| const apiPath = pathname.slice(apiIndex); | ||
| const normalized = apiPath.endsWith("/") ? apiPath : `${apiPath}/`; | ||
| const upper = method.toUpperCase(); | ||
|
|
||
| matchers ??= (generated satisfies IdempotentOperation[]).map(toMatcher); | ||
| return matchers.some( | ||
| (matcher) => matcher.method === upper && matcher.pattern.test(normalized), | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { isIdempotentOperation } from "../../src/lib/idempotent-operations.js"; | ||
|
|
||
| const REGION_URL = "https://us.sentry.io"; | ||
|
|
||
| describe("isIdempotentOperation", () => { | ||
| test.each([ | ||
| "/api/0/organizations/acme/chunk-upload/", | ||
| "/api/0/organizations/acme/artifactbundle/assemble/", | ||
| "/api/0/projects/acme/web/files/difs/assemble/", | ||
| ])("matches POST %s", (path) => { | ||
| expect(isIdempotentOperation("POST", `${REGION_URL}${path}`)).toBe(true); | ||
| }); | ||
|
|
||
| test("ignores method case and a missing trailing slash", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "post", | ||
| `${REGION_URL}/api/0/organizations/acme/chunk-upload`, | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test("matches under a self-hosted path prefix", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "POST", | ||
| "https://sentry.example.com/sentry/api/0/organizations/acme/chunk-upload/", | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test("ignores the query string", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "POST", | ||
| `${REGION_URL}/api/0/organizations/acme/chunk-upload/?foo=bar`, | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test("does not match other methods on a marked path", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "PUT", | ||
| `${REGION_URL}/api/0/organizations/acme/chunk-upload/`, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("does not match writes Sentry leaves unmarked", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "POST", | ||
| `${REGION_URL}/api/0/projects/acme/web/files/preprodartifacts/assemble/`, | ||
| ), | ||
| ).toBe(false); | ||
| expect( | ||
| isIdempotentOperation( | ||
| "POST", | ||
| `${REGION_URL}/api/0/organizations/acme/issues/1/autofix/`, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("does not let a template segment span several path segments", () => { | ||
| expect( | ||
| isIdempotentOperation( | ||
| "POST", | ||
| `${REGION_URL}/api/0/organizations/acme/extra/chunk-upload/`, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| test("returns false for URLs it cannot parse or that are not API paths", () => { | ||
| expect(isIdempotentOperation("POST", "not a url")).toBe(false); | ||
| expect( | ||
| isIdempotentOperation("POST", `${REGION_URL}/organizations/acme/`), | ||
| ).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.