From cc5c2b0e2da5a3375aa6397da38bb0b03088c7d1 Mon Sep 17 00:00:00 2001 From: stepanrodimanov Date: Thu, 2 Jul 2026 13:58:25 +0300 Subject: [PATCH] fix: del unnecessary metrics --- controller/internal/grpcserver/data_server.go | 7 ---- .../proto/communication/communication.proto | 15 -------- controller/test/worker_test.go | 38 ------------------- worker/communication.proto | 15 -------- worker/include/worker.hpp | 7 ---- worker/src/main.cpp | 7 ---- worker/src/worker.cpp | 34 ----------------- 7 files changed, 123 deletions(-) diff --git a/controller/internal/grpcserver/data_server.go b/controller/internal/grpcserver/data_server.go index 15ddc7e..98ddae7 100644 --- a/controller/internal/grpcserver/data_server.go +++ b/controller/internal/grpcserver/data_server.go @@ -9,7 +9,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/types/known/emptypb" "github.com/moevm/grpc_server/internal/service/service" ) @@ -108,9 +107,3 @@ func (s *DataServer) Classify(ctx context.Context, req *pb.ClassifyRequest) (*pb TrustLevel: 0, }, nil } - -func (s *DataServer) SendStats(ctx context.Context, report *pb.StatsReport) (*emptypb.Empty, error) { - log.Printf("gRPC Stats from worker %d: blocked=%d allowed=%d", - report.WorkerId, report.TotalBlocked, report.TotalAllowed) - return &emptypb.Empty{}, nil -} diff --git a/controller/pkg/proto/communication/communication.proto b/controller/pkg/proto/communication/communication.proto index 9ad73f9..5a2fddb 100644 --- a/controller/pkg/proto/communication/communication.proto +++ b/controller/pkg/proto/communication/communication.proto @@ -6,7 +6,6 @@ import "google/protobuf/empty.proto"; service DataService { rpc GetPolicy(GetPolicyRequest) returns (GetPolicyResponse); rpc Classify(ClassifyRequest) returns (ClassifyResponse); - rpc SendStats(StatsReport) returns (google.protobuf.Empty); } message GetPolicyRequest { @@ -49,17 +48,3 @@ message ClassifyResponse { repeated string categories = 1; int32 trust_level = 2; } - -message ResourceStats { - string domain = 1; - uint64 blocked = 2; - uint64 allowed = 3; -} - -message StatsReport { - uint64 worker_id = 1; - uint64 time = 2; - uint64 total_blocked = 3; - uint64 total_allowed = 4; - repeated ResourceStats resources = 5; -} diff --git a/controller/test/worker_test.go b/controller/test/worker_test.go index 87120bf..40cf78d 100644 --- a/controller/test/worker_test.go +++ b/controller/test/worker_test.go @@ -12,7 +12,6 @@ import ( pb "github.com/moevm/grpc_server/pkg/proto/communication" "github.com/stretchr/testify/assert" "google.golang.org/grpc" - "google.golang.org/protobuf/types/known/emptypb" ) type MockController struct { @@ -37,11 +36,6 @@ func (m *MockController) Classify(ctx context.Context, req *pb.ClassifyRequest) }, nil } -func (m *MockController) SendStats(ctx context.Context, req *pb.StatsReport) (*emptypb.Empty, error) { - m.t.Logf("SendStats called: worker_id=%d", req.WorkerId) - return &emptypb.Empty{}, nil -} - func StartMockController(t *testing.T, policy *pb.WorkerPolicy) (string, func()) { listenAddr := os.Getenv("TEST_CONTROLLER_ADDR") if listenAddr == "" { @@ -174,35 +168,3 @@ func TestWorkerClassify(t *testing.T) { assert.NoError(t, err, "Worker failed: %s", string(output)) assert.Contains(t, outputStr, "Target 'example.com' classified as categories [news, technology] with trust level 3") } - -func TestWorkerSendStats(t *testing.T) { - root := findProjectRoot() - workerBin := filepath.Join(root, "worker", "bazel-bin", "worker") - - if _, err := os.Stat(workerBin); err != nil { - t.Skipf("Worker binary not found: %v", err) - } - - addr, cleanup := StartMockController(t, nil) - defer cleanup() - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - worker := exec.CommandContext(ctx, workerBin) - worker.Env = []string{ - "WORKER_ID=1", - "CONTROLLER_GRPC_ADDR=" + addr, - "METRICS_GATEWAY_ADDRESS=localhost", - "METRICS_GATEWAY_PORT=9091", - "TEST_STATS=true", - } - - output, err := worker.CombinedOutput() - assert.NoError(t, err, "Worker failed: %s", string(output)) - - outputStr := string(output) - - assert.Contains(t, outputStr, "Stats sent successfully") - -} diff --git a/worker/communication.proto b/worker/communication.proto index 9ad73f9..5a2fddb 100644 --- a/worker/communication.proto +++ b/worker/communication.proto @@ -6,7 +6,6 @@ import "google/protobuf/empty.proto"; service DataService { rpc GetPolicy(GetPolicyRequest) returns (GetPolicyResponse); rpc Classify(ClassifyRequest) returns (ClassifyResponse); - rpc SendStats(StatsReport) returns (google.protobuf.Empty); } message GetPolicyRequest { @@ -49,17 +48,3 @@ message ClassifyResponse { repeated string categories = 1; int32 trust_level = 2; } - -message ResourceStats { - string domain = 1; - uint64 blocked = 2; - uint64 allowed = 3; -} - -message StatsReport { - uint64 worker_id = 1; - uint64 time = 2; - uint64 total_blocked = 3; - uint64 total_allowed = 4; - repeated ResourceStats resources = 5; -} diff --git a/worker/include/worker.hpp b/worker/include/worker.hpp index 768b5b3..0aa18c4 100644 --- a/worker/include/worker.hpp +++ b/worker/include/worker.hpp @@ -23,10 +23,6 @@ extern "C" { #define MIN_POLICY_TIME 30 #define MAX_POLICY_TIME 45 -#define EXPECTED_STATS_TIME 60 -#define MIN_STATS_TIME 30 -#define MAX_STATS_TIME 45 - enum class WorkerState { FREE, // Ожидает задачи SHUTTING_DOWN, // Завершение работы @@ -37,10 +33,8 @@ class Worker { uint64_t current_config_version = 0; std::chrono::time_point last_policy_time; - std::chrono::time_point last_stats_time; int64_t policy_interval = MIN_POLICY_TIME; - int64_t stats_interval = MIN_STATS_TIME; std::atomic enable{true}; struct net_port *port_in = nullptr; @@ -69,7 +63,6 @@ class Worker { struct requested_classification *out_req); void forward_to_out(struct net_port *incoming_port, struct net_port *outgoing_port, uint16_t queue_number); - void statsReport(); WorkerState GetState() const { return state; } static Worker *getInstance(); void MainLoop(); diff --git a/worker/src/main.cpp b/worker/src/main.cpp index 5d1ea2e..2cf8005 100644 --- a/worker/src/main.cpp +++ b/worker/src/main.cpp @@ -51,13 +51,6 @@ int main(int argc, char **argv) { worker.requestPolicyFromController(); } - if (getenv("TEST_STATS") != nullptr) { - test_mode = true; - spdlog::info("Test mode: send stats"); - std::this_thread::sleep_for(std::chrono::seconds(2)); - worker.statsReport(); - } - if (const char *target = getenv("TEST_CLASSIFY_TARGET")) { const char *type = getenv("TEST_CLASSIFY_TYPE"); if (!type) diff --git a/worker/src/worker.cpp b/worker/src/worker.cpp index 3c245fb..8e855b5 100644 --- a/worker/src/worker.cpp +++ b/worker/src/worker.cpp @@ -312,30 +312,6 @@ bool Worker::classify(const std::string &type, const std::string &target, } } -void Worker::statsReport() { - try { - spdlog::info("Worker {} send stats", worker_id); - - StatsReport report; - report.set_worker_id(worker_id); - report.set_time(time(nullptr)); - - grpc::ClientContext context; - google::protobuf::Empty response; - - auto status = stub_->SendStats(&context, report, &response); - if (!status.ok()) { - spdlog::error("SendStats failed: " + status.error_message()); - return; - } - - spdlog::info("Stats sent successfully"); - - } catch (const std::exception &e) { - spdlog::error("statsReport failed: {}", e.what()); - } -} - Worker::Worker(uint64_t id) : worker_id(id), state(WorkerState::FREE) { instance = this; std::string controller_addr = "localhost:50051"; @@ -381,7 +357,6 @@ void Worker::MainLoop() { using namespace std::chrono; last_policy_time = steady_clock::now(); - last_stats_time = steady_clock::now(); struct rte_mbuf *pkts[32]; uint16_t nb_pkts = 32; @@ -410,15 +385,6 @@ void Worker::MainLoop() { auto now = steady_clock::now(); - int64_t seconds_since_stats = (now - last_stats_time) / 1s; - if (seconds_since_stats >= stats_interval) { - std::thread([this]() { statsReport(); }).detach(); - last_stats_time = now; - stats_interval = - MIN_STATS_TIME + (rand() % (MAX_STATS_TIME - MIN_STATS_TIME + 1)); - spdlog::info("Next stats report in {}s", stats_interval); - } - int64_t seconds_since_policy = (now - last_policy_time) / 1s; if (seconds_since_policy >= policy_interval) { std::thread([this]() { requestPolicyFromController(); }).detach();