From 25956aab17f8d374f7172c07f0c696f89d6f0b28 Mon Sep 17 00:00:00 2001 From: heyanlong Date: Mon, 24 Dec 2018 15:46:45 +0800 Subject: [PATCH] fix bugs --- sky-php7ext/skywalking.c | 1 + sky-php7ext/src/report/report_client.cpp | 45 +++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/sky-php7ext/skywalking.c b/sky-php7ext/skywalking.c index 160f890..f14ecd6 100644 --- a/sky-php7ext/skywalking.c +++ b/sky-php7ext/skywalking.c @@ -619,6 +619,7 @@ static void request_init() { generate_context(); add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_instance", application_instance); + add_assoc_long(&SKYWALKING_G(UpstreamSegment), "pid", getppid()); add_assoc_long(&SKYWALKING_G(UpstreamSegment), "application_id", application_id); add_assoc_long(&SKYWALKING_G(UpstreamSegment), "version", SKYWALKING_G(version)); SKY_ADD_ASSOC_ZVAL(&SKYWALKING_G(UpstreamSegment), "segment"); diff --git a/sky-php7ext/src/report/report_client.cpp b/sky-php7ext/src/report/report_client.cpp index 2177c65..6c85f48 100644 --- a/sky-php7ext/src/report/report_client.cpp +++ b/sky-php7ext/src/report/report_client.cpp @@ -28,6 +28,7 @@ #include #include #include "json.hpp" +#include #include #include @@ -46,7 +47,7 @@ using json = nlohmann::json; class GreeterClient { public: GreeterClient(std::shared_ptr channel) - : stub_(TraceSegmentService::NewStub(channel)) {} + : stub_(TraceSegmentService::NewStub(channel)), discoveryStub_(InstanceDiscoveryService::NewStub(channel)) {} int collect(UpstreamSegment request) { @@ -70,8 +71,24 @@ public: return 1; } + int heartbeat(ApplicationInstanceHeartbeat request) { + Downstream reply; + + ClientContext context; + + Status status = discoveryStub_->heartbeat(&context, request, &reply); + if (status.ok()) { + std::cout << "send heartbeat ok!" << std::endl; + } else { + std::cout << "send heartbeat error!" << status.error_message() << std::endl; + } + + return 1; + } + private: std::unique_ptr stub_; + std::unique_ptr discoveryStub_; }; int main(int argc, char **argv) { @@ -94,6 +111,8 @@ int main(int argc, char **argv) { GreeterClient greeter(grpc::CreateChannel(argv[1], grpc::InsecureChannelCredentials())); + std::map appInstances; + std::map sendTime; while (1) { @@ -104,6 +123,24 @@ int main(int argc, char **argv) { return 0; } + // heartbeat + for (auto &i: appInstances) { + + struct timeval tv; + gettimeofday(&tv, NULL); + + if(tv.tv_sec - sendTime[i.first] > 40) { + if (0 == kill(appInstances[i.first], 0)) { + sendTime[i.first] = tv.tv_sec; + std::cout << "send heartbeat ..." << std::endl; + ApplicationInstanceHeartbeat request; + request.set_applicationinstanceid(i.first); + request.set_heartbeattime(tv.tv_sec*1000 + tv.tv_usec/1000); + greeter.heartbeat(request); + } + } + } + while ((dir = readdir(dp)) != NULL) { if (strcmp(".", dir->d_name) == 0 || strcmp("..", dir->d_name) == 0) { continue; @@ -148,6 +185,12 @@ int main(int argc, char **argv) { traceResult, std::regex("([\\-0-9]+)\\.(\\d+)\\.(\\d+)")); if (valid) { + // add to map + if(!appInstances[j["application_instance"]]) { + appInstances[j["application_instance"]] = j["pid"]; + sendTime[j["application_instance"]] = 0; + } + for (int i = 0; i < j["globalTraceIds"].size(); i++) { -- GitLab