-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(debugging): implement x-goog-spanner-request-id propagation per …
…request Implements propagation of the x-goog-spanner-request-id that'll be propagated for every call. Once an error has been encountered, that error will have `.requestId` set. Fixes #2200
- Loading branch information
Showing
7 changed files
with
226 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,80 @@ | ||
/*! | ||
* Copyright 2024 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {randomBytes} from 'crypto'; | ||
const randIdForProcess = randomBytes(8).readBigUint64LE(0).toString(); | ||
const X_GOOG_SPANNER_REQUEST_ID_HEADER = 'x-goog-spanner-request-id'; | ||
|
||
class AtomicCounter { | ||
private backingBuffer: BigInt64Array; | ||
|
||
constructor(initialValue?: number) { | ||
this.backingBuffer = new BigInt64Array( | ||
new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT) | ||
); | ||
if (initialValue) { | ||
this.increment(initialValue); | ||
} | ||
} | ||
|
||
public increment(n?: number): number { | ||
if (!n) { | ||
n = 1; | ||
} | ||
Atomics.store(this.backingBuffer, 0, BigInt(n)); | ||
return this.value(); | ||
} | ||
|
||
public value(): number { | ||
return Number(Atomics.load(this.backingBuffer, 0)); | ||
} | ||
|
||
public toString(): string { | ||
return `${this.value()}`; | ||
} | ||
} | ||
|
||
function craftRequestId( | ||
nthClientId: number, | ||
channelId: number, | ||
nthRequest: number, | ||
attempt: number | ||
) { | ||
return `1.${randIdForProcess}.${nthClientId}.${channelId}.${nthRequest}.${attempt}`; | ||
} | ||
|
||
const nthClientId = new AtomicCounter(); | ||
|
||
/* | ||
* nextSpannerClientId increments the internal | ||
* counter for created SpannerClients, for use | ||
* with x-goog-spanner-request-id. | ||
*/ | ||
function nextSpannerClientId(): number { | ||
return nthClientId.increment(1); | ||
} | ||
|
||
function newAtomicCounter(n?: number): AtomicCounter { | ||
return new AtomicCounter(n); | ||
} | ||
|
||
export { | ||
AtomicCounter, | ||
X_GOOG_SPANNER_REQUEST_ID_HEADER, | ||
craftRequestId, | ||
nextSpannerClientId, | ||
newAtomicCounter, | ||
}; |
This file contains 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 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
Oops, something went wrong.