48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
)
|
|
|
|
// GenerateDatadogHeaders generates Datadog APM tracing headers
|
|
// matching the format used by the OpenAI frontend.
|
|
func GenerateDatadogHeaders() map[string]string {
|
|
traceID := randomDigits(19)
|
|
parentID := randomDigits(16)
|
|
traceHex := fmt.Sprintf("%016x", mustParseUint(traceID))
|
|
parentHex := fmt.Sprintf("%016x", mustParseUint(parentID))
|
|
|
|
return map[string]string{
|
|
"traceparent": fmt.Sprintf("00-0000000000000000%s-%s-01", traceHex, parentHex),
|
|
"tracestate": "dd=s:1;o:rum",
|
|
"x-datadog-origin": "rum",
|
|
"x-datadog-parent-id": parentID,
|
|
"x-datadog-sampling-priority": "1",
|
|
"x-datadog-trace-id": traceID,
|
|
}
|
|
}
|
|
|
|
func randomDigits(n int) string {
|
|
result := make([]byte, n)
|
|
for i := range result {
|
|
v, _ := rand.Int(rand.Reader, big.NewInt(10))
|
|
result[i] = '0' + byte(v.Int64())
|
|
}
|
|
// Ensure first digit is not 0
|
|
if result[0] == '0' {
|
|
v, _ := rand.Int(rand.Reader, big.NewInt(9))
|
|
result[0] = '1' + byte(v.Int64())
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
func mustParseUint(s string) uint64 {
|
|
var result uint64
|
|
for _, c := range s {
|
|
result = result*10 + uint64(c-'0')
|
|
}
|
|
return result
|
|
}
|