Skip to content

Commit a5bfc1e

Browse files
authored
Simplify trace span id generation (vercel#32946)
Uses a simple counter for span ids, previously these required to be randomly generated but I've changed the importer script to ensure it gets prepended `0`s to make sure it gets to 16 characters which Jaeger requires. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
1 parent 0bbbfa4 commit a5bfc1e

File tree

5 files changed

+28
-78
lines changed

5 files changed

+28
-78
lines changed

packages/next/trace/report/to-json.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const localEndpoint = {
1212

1313
type Event = {
1414
traceId: string
15-
parentId?: string
15+
parentId?: number
1616
name: string
17-
id: string
17+
id: number
1818
timestamp: number
1919
duration: number
2020
localEndpoint?: typeof localEndpoint
@@ -119,8 +119,8 @@ const reportToLocalHost = (
119119
name: string,
120120
duration: number,
121121
timestamp: number,
122-
id: string,
123-
parentId?: string,
122+
id: number,
123+
parentId?: number,
124124
attrs?: Object
125125
) => {
126126
const distDir = traceGlobals.get('distDir')

packages/next/trace/shared.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// eslint typescript has a bug with TS enums
2-
3-
export type SpanId = string
1+
export type SpanId = number
42

53
export const traceGlobals: Map<any, any> = new Map()
64
export const setGlobal = (key: any, val: any) => {

packages/next/trace/trace.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { customAlphabet } from 'next/dist/compiled/nanoid/index.cjs'
21
import { SpanId } from './shared'
32
import { reporter } from './report'
43

54
const NUM_OF_MICROSEC_IN_SEC = BigInt('1000')
6-
const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8)
7-
const getId = () => Buffer.from(nanoid(), 'utf8').toString('hex')
5+
let count = 0
6+
const getId = () => {
7+
count++
8+
return count
9+
}
810

911
// eslint typescript has a bug with TS enums
1012
/* eslint-disable no-shadow */

scripts/send-trace-to-jaeger.mjs

Lines changed: 0 additions & 68 deletions
This file was deleted.

scripts/send-trace-to-jaeger/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ fn send_json_to_zipkin(zipkin_api: &str, value: String) {
3737
println!("body = {:?}", res.text());
3838
}
3939

40+
// function to append zero to a number until 16 characters
41+
fn pad_zeros(num: u64) -> String {
42+
let mut num_str = num.to_string();
43+
while num_str.len() < 16 {
44+
num_str = format!("0{}", num_str);
45+
}
46+
num_str
47+
}
48+
4049
fn main() {
4150
let service_name = "nextjs";
4251
let ipv4 = "127.0.0.1";
@@ -68,6 +77,13 @@ fn main() {
6877
logged_url = true;
6978
}
7079
data["localEndpoint"] = Value::Object(local_endpoint.clone());
80+
81+
data["id"] = Value::String(pad_zeros(data["id"].as_u64().unwrap()));
82+
if data["parentId"] != Value::Null {
83+
data["parentId"] =
84+
Value::String(pad_zeros(data["parentId"].as_u64().unwrap()));
85+
}
86+
7187
data
7288
})
7389
.collect::<Value>(),
@@ -79,6 +95,8 @@ fn main() {
7995

8096
let json_map = serde_json::to_string(&v).expect("Failed to serialize");
8197

98+
// println!("{:}", json_map);
99+
82100
send_json_to_zipkin(&zipkin_api, json_map);
83101
}
84102
}

0 commit comments

Comments
 (0)