Skip to content

Commit c568645

Browse files
authoredFeb 14, 2024··
generate a random span id for the built-in tracer (#2147)
1 parent de1b226 commit c568645

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎.changeset/nasty-onions-fold.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
generate a random span id for the built-in tracer
6+
7+
This ensures the same span id isn't used between application runs.

‎packages/effect/src/internal/tracer.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
*/
44
import * as Context from "../Context.js"
55
import type * as Exit from "../Exit.js"
6-
import { globalValue } from "../GlobalValue.js"
7-
import * as MutableRef from "../MutableRef.js"
86
import type * as Option from "../Option.js"
97
import type * as Tracer from "../Tracer.js"
108

@@ -23,7 +21,17 @@ export const tracerTag = Context.GenericTag<Tracer.Tracer>("effect/Tracer")
2321
/** @internal */
2422
export const spanTag = Context.GenericTag<Tracer.ParentSpan>("effect/ParentSpan")
2523

26-
const ids = globalValue("effect/Tracer/SpanId.ids", () => MutableRef.make(0))
24+
const randomString = (function() {
25+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
26+
const charactersLength = characters.length
27+
return function(length: number) {
28+
let result = ""
29+
for (let i = 0; i < length; i++) {
30+
result += characters.charAt(Math.floor(Math.random() * charactersLength))
31+
}
32+
return result
33+
}
34+
})()
2735

2836
/** @internal */
2937
export class NativeSpan implements Tracer.Span {
@@ -48,7 +56,7 @@ export class NativeSpan implements Tracer.Span {
4856
startTime
4957
}
5058
this.attributes = new Map()
51-
this.spanId = `span${MutableRef.incrementAndGet(ids)}`
59+
this.spanId = `span${randomString(16)}`
5260
}
5361

5462
end = (endTime: bigint, exit: Exit.Exit<unknown, unknown>): void => {

0 commit comments

Comments
 (0)
Please sign in to comment.