Skip to content

Commit a5c2c86

Browse files
committed
fix: also patch otel pkg
1 parent 28665c7 commit a5c2c86

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

packages/core/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,8 @@ export type {
513513
UnstableRollupPluginOptions,
514514
UnstableWebpackPluginOptions,
515515
} from './build-time-plugins/buildTimeOptionsBase';
516+
export {
517+
runInRandomSafeContext as _INTERNAL_runInRandomSafeContext,
518+
safeDateNow as _INTERNAL_safeDateNow,
519+
safeMathRandom as _INTERNAL_safeMathRandom,
520+
} from './utils/safeRandomGeneratorRunner';

packages/opentelemetry/.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@ module.exports = {
33
node: true,
44
},
55
extends: ['../../.eslintrc.js'],
6+
rules: {
7+
'@sentry-internal/sdk/no-unsafe-random-apis': 'error',
8+
},
9+
overrides: [
10+
{
11+
files: ['test/**/*.ts', 'test/**/*.tsx'],
12+
rules: {
13+
'@sentry-internal/sdk/no-unsafe-random-apis': 'off',
14+
},
15+
},
16+
],
617
};

packages/opentelemetry/src/sampler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@opentelemetry/semantic-conventions';
1313
import type { Client, SpanAttributes } from '@sentry/core';
1414
import {
15+
_INTERNAL_safeMathRandom,
1516
baggageHeaderToDynamicSamplingContext,
1617
debug,
1718
hasSpansEnabled,
@@ -121,7 +122,7 @@ export class SentrySampler implements Sampler {
121122
const dscString = parentContext?.traceState ? parentContext.traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
122123
const dsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;
123124

124-
const sampleRand = parseSampleRate(dsc?.sample_rand) ?? Math.random();
125+
const sampleRand = parseSampleRate(dsc?.sample_rand) ?? _INTERNAL_safeMathRandom();
125126

126127
const [sampled, sampleRate, localSampleRateWasApplied] = sampleSpan(
127128
options,

packages/opentelemetry/src/spanExporter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
TransactionSource,
1313
} from '@sentry/core';
1414
import {
15+
_INTERNAL_safeDateNow,
1516
captureEvent,
1617
convertSpanLinksForEnvelope,
1718
debounce,
@@ -82,7 +83,7 @@ export class SentrySpanExporter {
8283
}) {
8384
this._finishedSpanBucketSize = options?.timeout || DEFAULT_TIMEOUT;
8485
this._finishedSpanBuckets = new Array(this._finishedSpanBucketSize).fill(undefined);
85-
this._lastCleanupTimestampInS = Math.floor(Date.now() / 1000);
86+
this._lastCleanupTimestampInS = Math.floor(_INTERNAL_safeDateNow() / 1000);
8687
this._spansToBucketEntry = new WeakMap();
8788
this._sentSpans = new Map<string, number>();
8889
this._debouncedFlush = debounce(this.flush.bind(this), 1, { maxWait: 100 });
@@ -93,7 +94,7 @@ export class SentrySpanExporter {
9394
* This is called by the span processor whenever a span is ended.
9495
*/
9596
public export(span: ReadableSpan): void {
96-
const currentTimestampInS = Math.floor(Date.now() / 1000);
97+
const currentTimestampInS = Math.floor(_INTERNAL_safeDateNow() / 1000);
9798

9899
if (this._lastCleanupTimestampInS !== currentTimestampInS) {
99100
let droppedSpanCount = 0;
@@ -146,7 +147,7 @@ export class SentrySpanExporter {
146147
`SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} spans are waiting for their parent spans to finish`,
147148
);
148149

149-
const expirationDate = Date.now() + DEFAULT_TIMEOUT * 1000;
150+
const expirationDate = _INTERNAL_safeDateNow() + DEFAULT_TIMEOUT * 1000;
150151

151152
for (const span of sentSpans) {
152153
this._sentSpans.set(span.spanContext().spanId, expirationDate);
@@ -226,7 +227,7 @@ export class SentrySpanExporter {
226227

227228
/** Remove "expired" span id entries from the _sentSpans cache. */
228229
private _flushSentSpanCache(): void {
229-
const currentTimestamp = Date.now();
230+
const currentTimestamp = _INTERNAL_safeDateNow();
230231
// Note, it is safe to delete items from the map as we go: https://stackoverflow.com/a/35943995/90297
231232
for (const [spanId, expirationTime] of this._sentSpans.entries()) {
232233
if (expirationTime <= currentTimestamp) {

0 commit comments

Comments
 (0)