@@ -70,6 +70,7 @@ using v8::String;
7070using v8::Symbol;
7171using v8::Uint32;
7272using v8::Uint32Array;
73+ using v8::Uint8Array;
7374using v8::Value;
7475using v8::WeakCallbackInfo;
7576using v8::WeakCallbackType;
@@ -1824,14 +1825,12 @@ void EnvList::q_cb_timeout_cb_(nsuv::ns_timer* handle, QCbTimeoutStor* ptr) {
18241825}
18251826
18261827
1827- void EnvList::popSpanId (std::string & span_id) {
1828+ void EnvList::popSpanId (std::array< uint8_t , 8 > & span_id) {
18281829 int er;
18291830 size_t s;
18301831 if (!span_id_q_.dequeue (span_id, s)) {
18311832 // Generate the buffer synchronously
1832- unsigned char buf[8 ];
1833- utils::generate_random_buf (buf, sizeof (buf));
1834- span_id = utils::buffer_to_hex (buf, sizeof (buf));
1833+ utils::generate_random_buf (span_id.data (), span_id.size ());
18351834 // Notify the nsolid thread to fill the q
18361835 er = fill_tracing_ids_msg_.send ();
18371836 CHECK_EQ (er, 0 );
@@ -1846,14 +1845,12 @@ void EnvList::popSpanId(std::string& span_id) {
18461845}
18471846
18481847
1849- void EnvList::popTraceId (std::string & trace_id) {
1848+ void EnvList::popTraceId (std::array< uint8_t , 16 > & trace_id) {
18501849 int er;
18511850 size_t s;
18521851 if (!trace_id_q_.dequeue (trace_id, s)) {
18531852 // Generate the buffer synchronously
1854- unsigned char buf[16 ];
1855- utils::generate_random_buf (buf, sizeof (buf));
1856- trace_id = utils::buffer_to_hex (buf, sizeof (buf));
1853+ utils::generate_random_buf (trace_id.data (), trace_id.size ());
18571854 // Notify the nsolid thread to fill the q
18581855 er = fill_tracing_ids_msg_.send ();
18591856 CHECK_EQ (er, 0 );
@@ -1869,19 +1866,19 @@ void EnvList::popTraceId(std::string& trace_id) {
18691866
18701867
18711868void EnvList::fill_span_id_q () {
1872- unsigned char buf[SPAN_ID_Q_REFILL_ITEMS *8 ];
1873- utils::generate_random_buf (buf, sizeof (buf));
18741869 for (unsigned int i = 0 ; i < SPAN_ID_Q_REFILL_ITEMS ; ++i) {
1875- span_id_q_.enqueue (utils::buffer_to_hex (buf + i*8 , 8 ));
1870+ std::array<uint8_t , 8 > span_id;
1871+ utils::generate_random_buf (span_id.data (), span_id.size ());
1872+ span_id_q_.enqueue (span_id);
18761873 }
18771874}
18781875
18791876
18801877void EnvList::fill_trace_id_q () {
1881- unsigned char buf[TRACE_ID_Q_REFILL_ITEMS *16 ];
1882- utils::generate_random_buf (buf, sizeof (buf));
18831878 for (unsigned int i = 0 ; i < TRACE_ID_Q_REFILL_ITEMS ; ++i) {
1884- trace_id_q_.enqueue (utils::buffer_to_hex (buf + i*16 , 16 ));
1879+ std::array<uint8_t , 16 > trace_id;
1880+ utils::generate_random_buf (trace_id.data (), trace_id.size ());
1881+ trace_id_q_.enqueue (trace_id);
18851882 }
18861883}
18871884
@@ -2455,6 +2452,80 @@ void BindingData::PushSpanDataStringImpl3(BindingData* data,
24552452}
24562453
24572454
2455+ void BindingData::SlowGetSpanId (const FunctionCallbackInfo<Value>& args) {
2456+ CHECK (args[0 ]->IsUint8Array ());
2457+ Local<Uint8Array> output = args[0 ].As <Uint8Array>();
2458+
2459+ DCHECK_EQ (output->Length (), 8 );
2460+
2461+ std::array<uint8_t , 8 > span_id;
2462+ EnvList* envlist = EnvList::Inst ();
2463+ envlist->popSpanId (span_id);
2464+
2465+ // Copy binary data to the TypedArray
2466+ uint8_t * buffer =
2467+ static_cast <uint8_t *>(output->Buffer ()->Data ()) + output->ByteOffset ();
2468+ for (size_t i = 0 ; i < span_id.size (); i++) {
2469+ buffer[i] = span_id[i];
2470+ }
2471+ }
2472+
2473+
2474+ void BindingData::FastGetSpanId (v8::Local<v8::Value> receiver,
2475+ const v8::FastApiTypedArray<uint8_t >& output) {
2476+ std::array<uint8_t , 8 > span_id;
2477+ EnvList* envlist = EnvList::Inst ();
2478+ envlist->popSpanId (span_id);
2479+
2480+ DCHECK_EQ (output.length (), span_id.size ());
2481+
2482+ uint8_t * dst_data;
2483+ CHECK (output.getStorageIfAligned (&dst_data));
2484+
2485+ // Copy binary data directly to the output buffer
2486+ for (size_t i = 0 ; i < span_id.size (); i++) {
2487+ dst_data[i] = span_id[i];
2488+ }
2489+ }
2490+
2491+
2492+ void BindingData::SlowGetTraceId (const FunctionCallbackInfo<Value>& args) {
2493+ CHECK (args[0 ]->IsUint8Array ());
2494+ Local<Uint8Array> output = args[0 ].As <Uint8Array>();
2495+
2496+ DCHECK_EQ (output->Length (), 16 );
2497+
2498+ std::array<uint8_t , 16 > trace_id;
2499+ EnvList* envlist = EnvList::Inst ();
2500+ envlist->popTraceId (trace_id);
2501+
2502+ // Copy binary data to the TypedArray
2503+ uint8_t * buffer =
2504+ static_cast <uint8_t *>(output->Buffer ()->Data ()) + output->ByteOffset ();
2505+ for (size_t i = 0 ; i < trace_id.size (); i++) {
2506+ buffer[i] = trace_id[i];
2507+ }
2508+ }
2509+
2510+
2511+ void BindingData::FastGetTraceId (v8::Local<v8::Value> receiver,
2512+ const v8::FastApiTypedArray<uint8_t >& output) {
2513+ std::array<uint8_t , 16 > trace_id;
2514+ EnvList* envlist = EnvList::Inst ();
2515+ envlist->popTraceId (trace_id);
2516+
2517+ DCHECK_EQ (output.length (), trace_id.size ());
2518+
2519+ uint8_t * dst_data;
2520+ CHECK (output.getStorageIfAligned (&dst_data));
2521+
2522+ // Copy binary data directly to the output buffer
2523+ for (size_t i = 0 ; i < trace_id.size (); i++) {
2524+ dst_data[i] = trace_id[i];
2525+ }
2526+ }
2527+
2528+
24582529static void GetEnvMetrics (const FunctionCallbackInfo<Value>& args) {
24592530 Isolate* isolate = args.GetIsolate ();
24602531 EnvInst* envinst = EnvInst::GetEnvLocalInst (isolate);
@@ -2805,22 +2876,6 @@ static void SetTrackPromisesFn(const FunctionCallbackInfo<Value>& args) {
28052876}
28062877
28072878
2808- static void GetSpanId (const FunctionCallbackInfo<Value>& args) {
2809- std::string span_id;
2810- EnvList* envlist = EnvList::Inst ();
2811- envlist->popSpanId (span_id);
2812- args.GetReturnValue ().Set (OneByteString (args.GetIsolate (), span_id.c_str ()));
2813- }
2814-
2815-
2816- static void GetTraceId (const FunctionCallbackInfo<Value>& args) {
2817- std::string trace_id;
2818- EnvList* envlist = EnvList::Inst ();
2819- envlist->popTraceId (trace_id);
2820- args.GetReturnValue ().Set (OneByteString (args.GetIsolate (), trace_id.c_str ()));
2821- }
2822-
2823-
28242879static void heapprofile_js_cb (SharedEnvInst envinst_sp,
28252880 int status,
28262881 std::string profile,
@@ -3105,6 +3160,10 @@ v8::CFunction BindingData::fast_push_span_data_string_(
31053160 v8::CFunction::Make (FastPushSpanDataString));
31063161v8::CFunction BindingData::fast_push_span_data_string3_ (
31073162 v8::CFunction::Make (FastPushSpanDataString3));
3163+ v8::CFunction BindingData::fast_get_span_id_ (
3164+ v8::CFunction::Make (FastGetSpanId));
3165+ v8::CFunction BindingData::fast_get_trace_id_ (
3166+ v8::CFunction::Make (FastGetTraceId));
31083167
31093168
31103169void BindingData::Initialize (Local<Object> target,
@@ -3154,6 +3213,16 @@ void BindingData::Initialize(Local<Object> target,
31543213 " pushSpanDataString3" ,
31553214 SlowPushSpanDataString3,
31563215 &fast_push_span_data_string3_);
3216+ SetFastMethod (context,
3217+ target,
3218+ " getSpanId" ,
3219+ SlowGetSpanId,
3220+ &fast_get_span_id_);
3221+ SetFastMethod (context,
3222+ target,
3223+ " getTraceId" ,
3224+ SlowGetTraceId,
3225+ &fast_get_trace_id_);
31573226
31583227 SetMethod (context, target, " agentId" , AgentId);
31593228 SetMethod (context, target, " writeLog" , WriteLog);
@@ -3184,8 +3253,6 @@ void BindingData::Initialize(Local<Object> target,
31843253 SetMethod (context, target, " setThreadName" , setThreadName);
31853254 SetMethod (context, target, " setToggleTracingFn" , SetToggleTracingFn);
31863255 SetMethod (context, target, " setTrackPromisesFn" , SetTrackPromisesFn);
3187- SetMethod (context, target, " getSpanId" , GetSpanId);
3188- SetMethod (context, target, " getTraceId" , GetTraceId);
31893256 SetMethod (context, target, " heapProfile" , HeapProfile);
31903257 SetMethod (context, target, " heapProfileEnd" , HeapProfileEnd);
31913258 SetMethod (context, target, " heapSampling" , HeapSampling);
@@ -3295,6 +3362,14 @@ void BindingData::RegisterExternalReferences(
32953362 registry->Register (FastPushSpanDataString3);
32963363 registry->Register (fast_push_span_data_string3_.GetTypeInfo ());
32973364
3365+ registry->Register (SlowGetSpanId);
3366+ registry->Register (FastGetSpanId);
3367+ registry->Register (fast_get_span_id_.GetTypeInfo ());
3368+
3369+ registry->Register (SlowGetTraceId);
3370+ registry->Register (FastGetTraceId);
3371+ registry->Register (fast_get_trace_id_.GetTypeInfo ());
3372+
32983373 registry->Register (AgentId);
32993374 registry->Register (WriteLog);
33003375 registry->Register (GetEnvMetrics);
@@ -3321,8 +3396,6 @@ void BindingData::RegisterExternalReferences(
33213396 registry->Register (setThreadName);
33223397 registry->Register (SetToggleTracingFn);
33233398 registry->Register (SetTrackPromisesFn);
3324- registry->Register (GetSpanId);
3325- registry->Register (GetTraceId);
33263399 registry->Register (HeapProfile);
33273400 registry->Register (HeapProfileEnd);
33283401 registry->Register (HeapSampling);
0 commit comments