@@ -64,6 +64,7 @@ using v8::String;
64
64
using v8::Symbol;
65
65
using v8::Uint32;
66
66
using v8::Uint32Array;
67
+ using v8::Uint8Array;
67
68
using v8::Value;
68
69
using v8::WeakCallbackInfo;
69
70
using v8::WeakCallbackType;
@@ -1779,14 +1780,12 @@ void EnvList::q_cb_timeout_cb_(nsuv::ns_timer* handle, QCbTimeoutStor* ptr) {
1779
1780
}
1780
1781
1781
1782
1782
- void EnvList::popSpanId (std::string & span_id) {
1783
+ void EnvList::popSpanId (std::array< uint8_t , 8 > & span_id) {
1783
1784
int er;
1784
1785
size_t s;
1785
1786
if (!span_id_q_.dequeue (span_id, s)) {
1786
1787
// Generate the buffer synchronously
1787
- unsigned char buf[8 ];
1788
- utils::generate_random_buf (buf, sizeof (buf));
1789
- span_id = utils::buffer_to_hex (buf, sizeof (buf));
1788
+ utils::generate_random_buf (span_id.data (), span_id.size ());
1790
1789
// Notify the nsolid thread to fill the q
1791
1790
er = fill_tracing_ids_msg_.send ();
1792
1791
CHECK_EQ (er, 0 );
@@ -1801,14 +1800,12 @@ void EnvList::popSpanId(std::string& span_id) {
1801
1800
}
1802
1801
1803
1802
1804
- void EnvList::popTraceId (std::string & trace_id) {
1803
+ void EnvList::popTraceId (std::array< uint8_t , 16 > & trace_id) {
1805
1804
int er;
1806
1805
size_t s;
1807
1806
if (!trace_id_q_.dequeue (trace_id, s)) {
1808
1807
// Generate the buffer synchronously
1809
- unsigned char buf[16 ];
1810
- utils::generate_random_buf (buf, sizeof (buf));
1811
- trace_id = utils::buffer_to_hex (buf, sizeof (buf));
1808
+ utils::generate_random_buf (trace_id.data (), trace_id.size ());
1812
1809
// Notify the nsolid thread to fill the q
1813
1810
er = fill_tracing_ids_msg_.send ();
1814
1811
CHECK_EQ (er, 0 );
@@ -1824,19 +1821,19 @@ void EnvList::popTraceId(std::string& trace_id) {
1824
1821
1825
1822
1826
1823
void EnvList::fill_span_id_q () {
1827
- unsigned char buf[SPAN_ID_Q_REFILL_ITEMS*8 ];
1828
- utils::generate_random_buf (buf, sizeof (buf));
1829
1824
for (unsigned int i = 0 ; i < SPAN_ID_Q_REFILL_ITEMS; ++i) {
1830
- span_id_q_.enqueue (utils::buffer_to_hex (buf + i*8 , 8 ));
1825
+ std::array<uint8_t , 8 > span_id;
1826
+ utils::generate_random_buf (span_id.data (), span_id.size ());
1827
+ span_id_q_.enqueue (span_id);
1831
1828
}
1832
1829
}
1833
1830
1834
1831
1835
1832
void EnvList::fill_trace_id_q () {
1836
- unsigned char buf[TRACE_ID_Q_REFILL_ITEMS*16 ];
1837
- utils::generate_random_buf (buf, sizeof (buf));
1838
1833
for (unsigned int i = 0 ; i < TRACE_ID_Q_REFILL_ITEMS; ++i) {
1839
- trace_id_q_.enqueue (utils::buffer_to_hex (buf + i*16 , 16 ));
1834
+ std::array<uint8_t , 16 > trace_id;
1835
+ utils::generate_random_buf (trace_id.data (), trace_id.size ());
1836
+ trace_id_q_.enqueue (trace_id);
1840
1837
}
1841
1838
}
1842
1839
@@ -2366,6 +2363,80 @@ void BindingData::PushSpanDataStringImpl(BindingData* data,
2366
2363
}
2367
2364
2368
2365
2366
+ void BindingData::SlowGetSpanId (const FunctionCallbackInfo<Value>& args) {
2367
+ CHECK (args[0 ]->IsUint8Array ());
2368
+ Local<Uint8Array> output = args[0 ].As <Uint8Array>();
2369
+
2370
+ DCHECK_EQ (output->Length (), 8 );
2371
+
2372
+ std::array<uint8_t , 8 > span_id;
2373
+ EnvList* envlist = EnvList::Inst ();
2374
+ envlist->popSpanId (span_id);
2375
+
2376
+ // Copy binary data to the TypedArray
2377
+ uint8_t * buffer =
2378
+ static_cast <uint8_t *>(output->Buffer ()->Data ()) + output->ByteOffset ();
2379
+ for (size_t i = 0 ; i < span_id.size (); i++) {
2380
+ buffer[i] = span_id[i];
2381
+ }
2382
+ }
2383
+
2384
+
2385
+ void BindingData::FastGetSpanId (v8::Local<v8::Value> receiver,
2386
+ const v8::FastApiTypedArray<uint8_t >& output) {
2387
+ std::array<uint8_t , 8 > span_id;
2388
+ EnvList* envlist = EnvList::Inst ();
2389
+ envlist->popSpanId (span_id);
2390
+
2391
+ DCHECK_EQ (output.length (), span_id.size ());
2392
+
2393
+ uint8_t * dst_data;
2394
+ CHECK (output.getStorageIfAligned (&dst_data));
2395
+
2396
+ // Copy binary data directly to the output buffer
2397
+ for (size_t i = 0 ; i < span_id.size (); i++) {
2398
+ dst_data[i] = span_id[i];
2399
+ }
2400
+ }
2401
+
2402
+
2403
+ void BindingData::SlowGetTraceId (const FunctionCallbackInfo<Value>& args) {
2404
+ CHECK (args[0 ]->IsUint8Array ());
2405
+ Local<Uint8Array> output = args[0 ].As <Uint8Array>();
2406
+
2407
+ DCHECK_EQ (output->Length (), 16 );
2408
+
2409
+ std::array<uint8_t , 16 > trace_id;
2410
+ EnvList* envlist = EnvList::Inst ();
2411
+ envlist->popTraceId (trace_id);
2412
+
2413
+ // Copy binary data to the TypedArray
2414
+ uint8_t * buffer =
2415
+ static_cast <uint8_t *>(output->Buffer ()->Data ()) + output->ByteOffset ();
2416
+ for (size_t i = 0 ; i < trace_id.size (); i++) {
2417
+ buffer[i] = trace_id[i];
2418
+ }
2419
+ }
2420
+
2421
+
2422
+ void BindingData::FastGetTraceId (v8::Local<v8::Value> receiver,
2423
+ const v8::FastApiTypedArray<uint8_t >& output) {
2424
+ std::array<uint8_t , 16 > trace_id;
2425
+ EnvList* envlist = EnvList::Inst ();
2426
+ envlist->popTraceId (trace_id);
2427
+
2428
+ DCHECK_EQ (output.length (), trace_id.size ());
2429
+
2430
+ uint8_t * dst_data;
2431
+ CHECK (output.getStorageIfAligned (&dst_data));
2432
+
2433
+ // Copy binary data directly to the output buffer
2434
+ for (size_t i = 0 ; i < trace_id.size (); i++) {
2435
+ dst_data[i] = trace_id[i];
2436
+ }
2437
+ }
2438
+
2439
+
2369
2440
static void GetEnvMetrics (const FunctionCallbackInfo<Value>& args) {
2370
2441
Isolate* isolate = args.GetIsolate ();
2371
2442
EnvInst* envinst = EnvInst::GetEnvLocalInst (isolate);
@@ -2620,22 +2691,6 @@ static void SetTrackPromisesFn(const FunctionCallbackInfo<Value>& args) {
2620
2691
}
2621
2692
2622
2693
2623
- static void GetSpanId (const FunctionCallbackInfo<Value>& args) {
2624
- std::string span_id;
2625
- EnvList* envlist = EnvList::Inst ();
2626
- envlist->popSpanId (span_id);
2627
- args.GetReturnValue ().Set (OneByteString (args.GetIsolate (), span_id.c_str ()));
2628
- }
2629
-
2630
-
2631
- static void GetTraceId (const FunctionCallbackInfo<Value>& args) {
2632
- std::string trace_id;
2633
- EnvList* envlist = EnvList::Inst ();
2634
- envlist->popTraceId (trace_id);
2635
- args.GetReturnValue ().Set (OneByteString (args.GetIsolate (), trace_id.c_str ()));
2636
- }
2637
-
2638
-
2639
2694
static void heapprofile_js_cb (SharedEnvInst envinst_sp,
2640
2695
int status,
2641
2696
std::string profile,
@@ -2918,6 +2973,10 @@ v8::CFunction BindingData::fast_push_span_data_uint64_(
2918
2973
v8::CFunction::Make (FastPushSpanDataUint64));
2919
2974
v8::CFunction BindingData::fast_push_span_data_string_ (
2920
2975
v8::CFunction::Make (FastPushSpanDataString));
2976
+ v8::CFunction BindingData::fast_get_span_id_ (
2977
+ v8::CFunction::Make (FastGetSpanId));
2978
+ v8::CFunction BindingData::fast_get_trace_id_ (
2979
+ v8::CFunction::Make (FastGetTraceId));
2921
2980
2922
2981
2923
2982
void BindingData::Initialize (Local<Object> target,
@@ -2962,6 +3021,16 @@ void BindingData::Initialize(Local<Object> target,
2962
3021
" pushSpanDataString" ,
2963
3022
SlowPushSpanDataString,
2964
3023
&fast_push_span_data_string_);
3024
+ SetFastMethod (context,
3025
+ target,
3026
+ " getSpanId" ,
3027
+ SlowGetSpanId,
3028
+ &fast_get_span_id_);
3029
+ SetFastMethod (context,
3030
+ target,
3031
+ " getTraceId" ,
3032
+ SlowGetTraceId,
3033
+ &fast_get_trace_id_);
2965
3034
2966
3035
SetMethod (context, target, " agentId" , AgentId);
2967
3036
SetMethod (context, target, " writeLog" , WriteLog);
@@ -2991,8 +3060,6 @@ void BindingData::Initialize(Local<Object> target,
2991
3060
SetMethod (context, target, " setThreadName" , setThreadName);
2992
3061
SetMethod (context, target, " setToggleTracingFn" , SetToggleTracingFn);
2993
3062
SetMethod (context, target, " setTrackPromisesFn" , SetTrackPromisesFn);
2994
- SetMethod (context, target, " getSpanId" , GetSpanId);
2995
- SetMethod (context, target, " getTraceId" , GetTraceId);
2996
3063
SetMethod (context, target, " heapProfile" , HeapProfile);
2997
3064
SetMethod (context, target, " heapProfileEnd" , HeapProfileEnd);
2998
3065
SetMethod (context, target, " heapSampling" , HeapSampling);
@@ -3098,6 +3165,14 @@ void BindingData::RegisterExternalReferences(
3098
3165
registry->Register (FastPushSpanDataString);
3099
3166
registry->Register (fast_push_span_data_string_.GetTypeInfo ());
3100
3167
3168
+ registry->Register (SlowGetSpanId);
3169
+ registry->Register (FastGetSpanId);
3170
+ registry->Register (fast_get_span_id_.GetTypeInfo ());
3171
+
3172
+ registry->Register (SlowGetTraceId);
3173
+ registry->Register (FastGetTraceId);
3174
+ registry->Register (fast_get_trace_id_.GetTypeInfo ());
3175
+
3101
3176
registry->Register (AgentId);
3102
3177
registry->Register (WriteLog);
3103
3178
registry->Register (GetEnvMetrics);
@@ -3123,8 +3198,6 @@ void BindingData::RegisterExternalReferences(
3123
3198
registry->Register (setThreadName);
3124
3199
registry->Register (SetToggleTracingFn);
3125
3200
registry->Register (SetTrackPromisesFn);
3126
- registry->Register (GetSpanId);
3127
- registry->Register (GetTraceId);
3128
3201
registry->Register (HeapProfile);
3129
3202
registry->Register (HeapProfileEnd);
3130
3203
registry->Register (HeapSampling);
0 commit comments