Unverified Commit a5bfc1ee authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Simplify trace span id generation (#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`
parent 0bbbfa48
......@@ -12,9 +12,9 @@ const localEndpoint = {
type Event = {
traceId: string
parentId?: string
parentId?: number
name: string
id: string
id: number
timestamp: number
duration: number
localEndpoint?: typeof localEndpoint
......@@ -119,8 +119,8 @@ const reportToLocalHost = (
name: string,
duration: number,
timestamp: number,
id: string,
parentId?: string,
id: number,
parentId?: number,
attrs?: Object
) => {
const distDir = traceGlobals.get('distDir')
......
// eslint typescript has a bug with TS enums
export type SpanId = string
export type SpanId = number
export const traceGlobals: Map<any, any> = new Map()
export const setGlobal = (key: any, val: any) => {
......
import { customAlphabet } from 'next/dist/compiled/nanoid/index.cjs'
import { SpanId } from './shared'
import { reporter } from './report'
const NUM_OF_MICROSEC_IN_SEC = BigInt('1000')
const nanoid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8)
const getId = () => Buffer.from(nanoid(), 'utf8').toString('hex')
let count = 0
const getId = () => {
count++
return count
}
// eslint typescript has a bug with TS enums
/* eslint-disable no-shadow */
......
import fs from 'fs'
import eventStream from 'event-stream'
import retry from 'async-retry'
import fetch from 'node-fetch'
const file = fs.createReadStream(process.argv[2])
const localEndpoint = {
serviceName: 'nextjs',
ipv4: '127.0.0.1',
port: 9411,
}
// Jaeger supports Zipkin's reporting API
const zipkinUrl = `http://${localEndpoint.ipv4}:${localEndpoint.port}`
const jaegerWebUiUrl = `http://${localEndpoint.ipv4}:16686`
const zipkinAPI = `${zipkinUrl}/api/v2/spans`
let loggedUrl = false
function logWebUrl(traceId) {
console.log(
`Jaeger trace will be available on ${jaegerWebUiUrl}/trace/${traceId}`
)
}
file.pipe(eventStream.split()).pipe(
eventStream.map((data, cb) => {
if (data === '') {
return cb(null, '')
}
const eventsJson = JSON.parse(data).map((item) => {
item.localEndpoint = localEndpoint
return item
})
if (!loggedUrl) {
logWebUrl(eventsJson[0].traceId)
loggedUrl = true
}
retry(
() =>
// Send events to zipkin
fetch(zipkinAPI, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(eventsJson),
}),
{ minTimeout: 500, retries: 3, factor: 1 }
)
.then(async (res) => {
if (res.status !== 202) {
console.dir(
{
status: res.status,
body: await res.text(),
events: eventsJson,
},
{ depth: null }
)
}
cb(null, '')
})
.catch((err) => {
console.log(err)
cb(null, '')
})
})
)
......@@ -37,6 +37,15 @@ fn send_json_to_zipkin(zipkin_api: &str, value: String) {
println!("body = {:?}", res.text());
}
// function to append zero to a number until 16 characters
fn pad_zeros(num: u64) -> String {
let mut num_str = num.to_string();
while num_str.len() < 16 {
num_str = format!("0{}", num_str);
}
num_str
}
fn main() {
let service_name = "nextjs";
let ipv4 = "127.0.0.1";
......@@ -68,6 +77,13 @@ fn main() {
logged_url = true;
}
data["localEndpoint"] = Value::Object(local_endpoint.clone());
data["id"] = Value::String(pad_zeros(data["id"].as_u64().unwrap()));
if data["parentId"] != Value::Null {
data["parentId"] =
Value::String(pad_zeros(data["parentId"].as_u64().unwrap()));
}
data
})
.collect::<Value>(),
......@@ -79,6 +95,8 @@ fn main() {
let json_map = serde_json::to_string(&v).expect("Failed to serialize");
// println!("{:}", json_map);
send_json_to_zipkin(&zipkin_api, json_map);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment