-
Notifications
You must be signed in to change notification settings - Fork 1.3k
adding cloud trace storage provisioning to crashlytics onboarding #11219
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
Open
ssakhamu
wants to merge
3
commits into
main
Choose a base branch
from
welcome-span-to-cloud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,62 @@ | ||
| import { expect } from "chai"; | ||
| import * as sinon from "sinon"; | ||
| import nock from "../test/helpers/nock"; | ||
|
|
||
| import * as cloudtrace from "./cloudtrace"; | ||
| import { cloudTraceOrigin } from "../api"; | ||
| import { FirebaseError } from "../error"; | ||
|
|
||
| describe("cloudtrace", () => { | ||
| let clock: sinon.SinonFakeTimers; | ||
|
|
||
| before(() => { | ||
| nock.disableNetConnect(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| nock.enableNetConnect(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| clock = sinon.useFakeTimers(new Date("2026-01-01T00:00:00.000Z").getTime()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clock.restore(); | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| describe("provisionTraceStorage", () => { | ||
| it("should send a welcome span to batchWrite endpoint", async () => { | ||
| const batchWriteReq = nock(cloudTraceOrigin()) | ||
| .post("/v2/projects/test-project/traces:batchWrite", { | ||
| name: "projects/test-project", | ||
| spans: [ | ||
| { | ||
| name: "projects/test-project/traces/33fc0d8c45bb4e5cebb29f047931270d/spans/f8fde40b437488e5", | ||
| spanId: "f8fde40b437488e5", | ||
| displayName: { value: "/welcome" }, | ||
| startTime: "2026-01-01T00:00:00.000Z", | ||
| endTime: "2026-01-01T00:00:01.000Z", | ||
| }, | ||
| ], | ||
| }) | ||
| .reply(200, {}); | ||
|
|
||
| await cloudtrace.provisionTraceStorage("test-project"); | ||
|
|
||
| expect(batchWriteReq.isDone()).to.be.true; | ||
| }); | ||
|
|
||
| it("should throw a FirebaseError when batchWrite fails", async () => { | ||
| nock(cloudTraceOrigin()) | ||
| .post("/v2/projects/test-project/traces:batchWrite") | ||
| .reply(500, { error: { message: "Internal error" } }); | ||
|
|
||
| await expect(cloudtrace.provisionTraceStorage("test-project")).to.be.rejectedWith( | ||
| FirebaseError, | ||
| "Failed to provision trace storage for project test-project", | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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,63 @@ | ||
| import { cloudTraceOrigin } from "../api"; | ||
| import { Client } from "../apiv2"; | ||
| import { FirebaseError, getError } from "../error"; | ||
|
|
||
| const API_VERSION = "v2"; | ||
|
|
||
| const getTraceClient = (): Client => | ||
| new Client({ | ||
| urlPrefix: cloudTraceOrigin(), | ||
| auth: true, | ||
| apiVersion: API_VERSION, | ||
| }); | ||
|
|
||
| interface Span { | ||
| name: string; | ||
| spanId: string; | ||
| displayName: { value: string }; | ||
| startTime: string; | ||
| endTime: string; | ||
| } | ||
|
|
||
| interface BatchWriteSpansRequest { | ||
| name: string; | ||
| spans: Span[]; | ||
| } | ||
|
|
||
| /** | ||
| * Sends a mock span to Cloud Trace to trigger BigQuery _Trace dataset provisioning. | ||
| * Ref: https://cloud.google.com/trace/docs/reference/v2/rest/v2/projects.traces/batchWrite | ||
| */ | ||
| export async function provisionTraceStorage(projectId: string): Promise<void> { | ||
| // Use the same arbitrary IDs that the GCP backend uses | ||
| const TRACE_ID = "33fc0d8c45bb4e5cebb29f047931270d"; | ||
| const SPAN_ID = "f8fde40b437488e5"; | ||
|
|
||
| const now = new Date(); | ||
| const later = new Date(now.getTime() + 1000); | ||
|
|
||
| const payload: BatchWriteSpansRequest = { | ||
| name: `projects/${projectId}`, | ||
| spans: [ | ||
| { | ||
| name: `projects/${projectId}/traces/${TRACE_ID}/spans/${SPAN_ID}`, | ||
| spanId: SPAN_ID, | ||
| displayName: { value: "/welcome" }, | ||
| startTime: now.toISOString(), | ||
| endTime: later.toISOString(), | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| try { | ||
| // Send the mock span to trigger BigQuery _Trace database creation | ||
| await getTraceClient().post<BatchWriteSpansRequest, void>( | ||
| `/projects/${projectId}/traces:batchWrite`, | ||
| payload, | ||
| ); | ||
| } catch (err: unknown) { | ||
| throw new FirebaseError(`Failed to provision trace storage for project ${projectId}`, { | ||
| original: getError(err), | ||
| }); | ||
| } | ||
|
ssakhamu marked this conversation as resolved.
|
||
| } | ||
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.
should this test just be
to.not.have.been.called?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.
esnureStub is called twice with different parameters above so this is to make sure it doesn't get called with these parameters. Otherwise, it would fail as it is called twice.