From e5e310844dc00f196f74deb8eabf6aca336b8cc3 Mon Sep 17 00:00:00 2001 From: xiongchuanyu Date: Tue, 22 Aug 2023 10:40:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20cat=20=E6=94=AF=E6=8C=81node12+?= =?UTF-8?q?=E4=BB=A5=E4=B8=8A=E7=89=88=E6=9C=AC=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/node.js/src/nodecat.cc | 336 ++++++++++++++++++++++++++----------- 1 file changed, 241 insertions(+), 95 deletions(-) diff --git a/lib/node.js/src/nodecat.cc b/lib/node.js/src/nodecat.cc index 77393b9c58..3f3a63c6a4 100644 --- a/lib/node.js/src/nodecat.cc +++ b/lib/node.js/src/nodecat.cc @@ -5,105 +5,251 @@ using namespace v8; using namespace std; - -namespace catapi { - - /** - * Initialize cat - * @param args - */ - void Init(const FunctionCallbackInfo &args) { - Isolate *isolate = args.GetIsolate(); - if (args.Length() < 1) { - isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Appkey is required"))); - return; - } - String::Utf8Value str(isolate, args[0]); - - CatClientConfig config = DEFAULT_CCAT_CONFIG; - config.enableHeartbeat = 0; - config.enableMultiprocessing = 1; - catClientInitWithConfig((const char *) (*str), &config); +#define MAJOR_VERSION NODE_MAJOR_VERSION +#define MINOR_VERSION NODE_MINOR_VERSION + +namespace catapi +{ + + /** + * Initialize cat + * @param args + */ + void Init(const FunctionCallbackInfo &args) + { + Isolate *isolate = args.GetIsolate(); + if (args.Length() < 1) + { +#if (MAJOR_VERSION <= 12) + isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Appkey is required"))); +#else + isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Appkey is required").ToLocalChecked())); +#endif + return; } - - /** - * Destroy cat - * @param args - */ - void Destroy(const FunctionCallbackInfo &args) { - catClientDestroy(); + String::Utf8Value str(isolate, args[0]); + + CatClientConfig config = DEFAULT_CCAT_CONFIG; + config.enableHeartbeat = 0; + config.enableMultiprocessing = 1; + catClientInitWithConfig((const char *)(*str), &config); + } + + /** + * Destroy cat + * @param args + */ + void Destroy(const FunctionCallbackInfo &args) + { + catClientDestroy(); + } + + void InnerSendNode(Isolate *isolate, Local node) + { + if (node->IsNull() || node->IsUndefined()) + { + return; } - - void InnerSendNode(Isolate *isolate, Local node) { - if (node->IsNull() || node->IsUndefined()) { - return; - } - Local ctx = isolate->GetCurrentContext(); - String::Utf8Value messageType(isolate, node->Get(String::NewFromUtf8(isolate, "messageType"))); - if (strcmp(*messageType, "transaction") == 0) { - String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); - String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); - String::Utf8Value status(isolate, node->Get(String::NewFromUtf8(isolate, "status"))); - String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); - double begin = (node->Get(String::NewFromUtf8(isolate, "beginTimestamp"))->NumberValue(ctx)).ToChecked(); - double end = (node->Get(String::NewFromUtf8(isolate, "endTimestamp"))->NumberValue(ctx)).ToChecked(); - - CatTransaction *t = newTransaction((const char *) (*type), (const char *) (*name)); - t->setDurationInMillis(t, static_cast(end - begin)); - t->setTimestamp(t, static_cast(begin)); - t->setStatus(t, (const char *) (*status)); - t->addData(t, (const char *) (*data)); - - // Iterate children recursively - Array *children = Array::Cast((*(node->Get(String::NewFromUtf8(isolate, "children"))))); - int count = children->Length(); - for (u_int32_t i = 0; i < count; i++) { - InnerSendNode(isolate, Object::Cast((*(children->Get(i))))->Clone()); - } - - t->complete(t); - } else if (strcmp(*messageType, "event") == 0) { - String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); - String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); - String::Utf8Value status(isolate, node->Get(String::NewFromUtf8(isolate, "status"))); - String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); - double begin(((node->Get(String::NewFromUtf8(isolate, "beginTimestamp")))->NumberValue(ctx)).ToChecked()); - - CatEvent *e = newEvent((const char *) (*type), (const char *) (*name)); - e->setTimestamp(e, static_cast(begin)); - e->addData(e, (const char *) (*data)); - e->setStatus(e, (const char *) (*status)); - e->complete(e); - } else if (strcmp(*messageType, "heartbeat") == 0) { - String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); - String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); - String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); - - CatHeartBeat *h = newHeartBeat((const char *) (*type), (const char *) (*name)); - h->addData(h, (const char *) (*data)); - h->setStatus(h, CAT_SUCCESS); - h->complete(h); - } + Local ctx = isolate->GetCurrentContext(); + cout << NODE_MODULE_VERSION << endl; + std::cout << "Value is: " << NODE_MODULE_VERSION << std::endl; + +#if (MAJOR_VERSION <= 12) + String::Utf8Value messageType(isolate, node->Get(String::NewFromUtf8(isolate, "messageType"))); +#else + v8::Local messageTypeKey = v8::String::NewFromUtf8(isolate, "messageType").ToLocalChecked(); + v8::Local messageTypeValue = node->Get(ctx, messageTypeKey).ToLocalChecked(); + v8::String::Utf8Value messageType(isolate, messageTypeValue); +#endif + if (strcmp(*messageType, "transaction") == 0) + { +#if (MAJOR_VERSION <= 12) + String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); +#else + v8::Local typeKey = v8::String::NewFromUtf8(isolate, "type").ToLocalChecked(); + v8::Local typeValue = node->Get(ctx, typeKey).ToLocalChecked(); + v8::String::Utf8Value type(isolate, typeValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); +#else + v8::Local nameKey = v8::String::NewFromUtf8(isolate, "name").ToLocalChecked(); + v8::Local nameValue = node->Get(ctx, nameKey).ToLocalChecked(); + v8::String::Utf8Value name(isolate, nameValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value status(isolate, node->Get(String::NewFromUtf8(isolate, "status"))); +#else + v8::Local statusKey = v8::String::NewFromUtf8(isolate, "status").ToLocalChecked(); + v8::Local statusValue = node->Get(ctx, statusKey).ToLocalChecked(); + v8::String::Utf8Value status(isolate, statusValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); +#else + v8::Local dataKey = v8::String::NewFromUtf8(isolate, "data").ToLocalChecked(); + v8::Local dataValue = node->Get(ctx, dataKey).ToLocalChecked(); + v8::String::Utf8Value data(isolate, dataValue); +#endif + +#if (MAJOR_VERSION <= 12) + double begin = (node->Get(String::NewFromUtf8(isolate, "beginTimestamp"))->NumberValue(ctx)).ToChecked(); +#else + v8::Local beginKey = v8::String::NewFromUtf8(isolate, "beginTimestamp").ToLocalChecked(); + v8::Local beginValue = node->Get(ctx, beginKey).ToLocalChecked(); + double begin = beginValue->NumberValue(ctx).ToChecked(); +#endif + +#if (MAJOR_VERSION <= 12) + double end = (node->Get(String::NewFromUtf8(isolate, "endTimestamp"))->NumberValue(ctx)).ToChecked(); +#else + v8::Local endKey = v8::String::NewFromUtf8(isolate, "endTimestamp").ToLocalChecked(); + v8::Local endValue = node->Get(ctx, endKey).ToLocalChecked(); + double end = endValue->NumberValue(ctx).ToChecked(); +#endif + + CatTransaction *t = newTransaction((const char *)(*type), (const char *)(*name)); + t->setDurationInMillis(t, static_cast(end - begin)); + t->setTimestamp(t, static_cast(begin)); + t->setStatus(t, (const char *)(*status)); + t->addData(t, (const char *)(*data)); + +// Iterate children recursively +#if (MAJOR_VERSION <= 12) + Array *children = Array::Cast((*(node->Get(String::NewFromUtf8(isolate, "children"))))); +#else + v8::Local childrenKey = v8::String::NewFromUtf8(isolate, "children").ToLocalChecked(); + v8::Local childrenValue = node->Get(ctx, childrenKey).ToLocalChecked(); + v8::Local children = v8::Local::Cast(childrenValue); +#endif + + int count = children->Length(); + for (int i = 0; i < count; i++) + { +#if (MAJOR_VERSION <= 12) + InnerSendNode(isolate, Object::Cast((*(children->Get(i))))->Clone()); +#else + v8::Local childValue = children->Get(ctx, i).ToLocalChecked(); + v8::Local childObject = v8::Local::Cast(childValue); + v8::Local clonedChildObject = childObject->Clone(); + InnerSendNode(isolate, clonedChildObject); +#endif + } + + t->complete(t); } - - void SendTree(const FunctionCallbackInfo &args) { - Isolate *isolate = args.GetIsolate(); - if (args.Length() == 0) { - return; - } - Array *tree = Array::Cast((*args[0])); - - Array *root = Array::Cast((*(tree->Get(String::NewFromUtf8(isolate, "root"))))); - - InnerSendNode(isolate, root->Clone()); + else if (strcmp(*messageType, "event") == 0) + { +#if (MAJOR_VERSION <= 12) + String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); +#else + v8::Local typeKey = v8::String::NewFromUtf8(isolate, "type").ToLocalChecked(); + v8::Local typeValue = node->Get(ctx, typeKey).ToLocalChecked(); + v8::String::Utf8Value type(isolate, typeValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); +#else + v8::Local nameKey = v8::String::NewFromUtf8(isolate, "name").ToLocalChecked(); + v8::Local nameValue = node->Get(ctx, nameKey).ToLocalChecked(); + v8::String::Utf8Value name(isolate, nameValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value status(isolate, node->Get(String::NewFromUtf8(isolate, "status"))); +#else + v8::Local statusKey = v8::String::NewFromUtf8(isolate, "status").ToLocalChecked(); + v8::Local statusValue = node->Get(ctx, statusKey).ToLocalChecked(); + v8::String::Utf8Value status(isolate, statusValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); +#else + v8::Local dataKey = v8::String::NewFromUtf8(isolate, "data").ToLocalChecked(); + v8::Local dataValue = node->Get(ctx, dataKey).ToLocalChecked(); + v8::String::Utf8Value data(isolate, dataValue); +#endif + +#if (MAJOR_VERSION <= 12) + double begin(((node->Get(String::NewFromUtf8(isolate, "beginTimestamp")))->NumberValue(ctx)).ToChecked()); +#else + v8::Local beginKey = v8::String::NewFromUtf8(isolate, "beginTimestamp").ToLocalChecked(); + v8::Local beginValue = node->Get(ctx, beginKey).ToLocalChecked(); + double begin = beginValue->NumberValue(ctx).ToChecked(); +#endif + + CatEvent *e = newEvent((const char *)(*type), (const char *)(*name)); + e->setTimestamp(e, static_cast(begin)); + e->addData(e, (const char *)(*data)); + e->setStatus(e, (const char *)(*status)); + e->complete(e); } - - void exports(Local exports) { - NODE_SET_METHOD(exports, "init", Init); - NODE_SET_METHOD(exports, "destroy", Destroy); - NODE_SET_METHOD(exports, "sendTree", SendTree); + else if (strcmp(*messageType, "heartbeat") == 0) + { +#if (MAJOR_VERSION <= 12) + String::Utf8Value type(isolate, node->Get(String::NewFromUtf8(isolate, "type"))); +#else + v8::Local typeKey = v8::String::NewFromUtf8(isolate, "type").ToLocalChecked(); + v8::Local typeValue = node->Get(ctx, typeKey).ToLocalChecked(); + v8::String::Utf8Value type(isolate, typeValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value name(isolate, node->Get(String::NewFromUtf8(isolate, "name"))); +#else + v8::Local nameKey = v8::String::NewFromUtf8(isolate, "name").ToLocalChecked(); + v8::Local nameValue = node->Get(ctx, nameKey).ToLocalChecked(); + v8::String::Utf8Value name(isolate, nameValue); +#endif + +#if (MAJOR_VERSION <= 12) + String::Utf8Value data(isolate, node->Get(String::NewFromUtf8(isolate, "data"))); +#else + v8::Local dataKey = v8::String::NewFromUtf8(isolate, "data").ToLocalChecked(); + v8::Local dataValue = node->Get(ctx, dataKey).ToLocalChecked(); + v8::String::Utf8Value data(isolate, dataValue); +#endif + + CatHeartBeat *h = newHeartBeat((const char *)(*type), (const char *)(*name)); + h->addData(h, (const char *)(*data)); + h->setStatus(h, CAT_SUCCESS); + h->complete(h); } - - NODE_MODULE(nodecat, exports) + } + + void SendTree(const FunctionCallbackInfo &args) + { + Isolate *isolate = args.GetIsolate(); + if (args.Length() == 0) + { + return; + } + Array *tree = Array::Cast((*args[0])); + +#if (MAJOR_VERSION <= 12) + Array *root = Array::Cast((*(tree->Get(String::NewFromUtf8(isolate, "root"))))); +#else + Local ctx = isolate->GetCurrentContext(); + v8::Local rootKey = v8::String::NewFromUtf8(isolate, "root").ToLocalChecked(); + v8::Local rootValue = tree->Get(ctx, rootKey).ToLocalChecked(); + v8::Local root = v8::Local::Cast(rootValue); +#endif + + InnerSendNode(isolate, root->Clone()); + } + + void exports(Local exports) + { + NODE_SET_METHOD(exports, "init", Init); + NODE_SET_METHOD(exports, "destroy", Destroy); + NODE_SET_METHOD(exports, "sendTree", SendTree); + } + + NODE_MODULE(nodecat, exports) } // namespace catapi