From 8bd3ef0b726754a5c17db426dd693dea8a45ba05 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Tue, 8 Jul 2025 14:08:31 +0800 Subject: [PATCH] feat: add goroutine count metric to the MetricService and update related files --- core/grpc/server/metric_service_server.go | 6 +++-- core/models/models/metric.go | 1 + core/notification/service.go | 7 ++++-- .../src/i18n/lang/en/components/metric.ts | 1 + .../src/i18n/lang/zh/components/metric.ts | 1 + .../interfaces/i18n/components/metric.d.ts | 1 + frontend/crawlab-ui/src/utils/metric.ts | 7 ++++++ grpc/bin/compile_all.sh | 0 grpc/metric_service.pb.go | 25 +++++++++++++------ grpc/proto/services/metric_service.proto | 1 + 10 files changed, 39 insertions(+), 11 deletions(-) mode change 100644 => 100755 grpc/bin/compile_all.sh diff --git a/core/grpc/server/metric_service_server.go b/core/grpc/server/metric_service_server.go index 2288f614..02632c0b 100644 --- a/core/grpc/server/metric_service_server.go +++ b/core/grpc/server/metric_service_server.go @@ -2,14 +2,15 @@ package server import ( "context" + "sync" + "time" + "github.com/crawlab-team/crawlab/core/interfaces" "github.com/crawlab-team/crawlab/core/models/models" "github.com/crawlab-team/crawlab/core/models/service" "github.com/crawlab-team/crawlab/core/utils" "github.com/crawlab-team/crawlab/grpc" "go.mongodb.org/mongo-driver/bson" - "sync" - "time" ) type MetricServiceServer struct { @@ -40,6 +41,7 @@ func (svr MetricServiceServer) Send(_ context.Context, req *grpc.MetricServiceSe DiskWriteBytesRate: req.DiskWriteBytesRate, NetworkBytesSentRate: req.NetworkBytesSentRate, NetworkBytesRecvRate: req.NetworkBytesRecvRate, + GoroutineCount: req.GoroutineCount, } metric.CreatedAt = time.Unix(req.Timestamp, 0) _, err = service.NewModelService[models.Metric]().InsertOne(metric) diff --git a/core/models/models/metric.go b/core/models/models/metric.go index cc1f4603..21d644dc 100644 --- a/core/models/models/metric.go +++ b/core/models/models/metric.go @@ -22,4 +22,5 @@ type Metric struct { DiskWriteBytesRate float32 `json:"disk_write_bytes_rate" bson:"disk_write_bytes_rate" description:"Disk write bytes rate"` NetworkBytesSentRate float32 `json:"network_bytes_sent_rate" bson:"network_bytes_sent_rate" description:"Network bytes sent rate"` NetworkBytesRecvRate float32 `json:"network_bytes_recv_rate" bson:"network_bytes_recv_rate" description:"Network bytes recv rate"` + GoroutineCount int32 `json:"goroutine_count" bson:"goroutine_count" description:"Current goroutine count"` } diff --git a/core/notification/service.go b/core/notification/service.go index c7f00e77..a49b68bc 100644 --- a/core/notification/service.go +++ b/core/notification/service.go @@ -2,13 +2,14 @@ package notification import ( "fmt" - "github.com/crawlab-team/crawlab/core/interfaces" - "github.com/crawlab-team/crawlab/core/utils" "regexp" "strings" "sync" "time" + "github.com/crawlab-team/crawlab/core/interfaces" + "github.com/crawlab-team/crawlab/core/utils" + "github.com/crawlab-team/crawlab/core/constants" "github.com/crawlab-team/crawlab/core/entity" "github.com/crawlab-team/crawlab/core/models/models" @@ -471,6 +472,8 @@ func (svc *Service) getFormattedMetricValue(metricName string, m *models.Metric) return fmt.Sprintf("%.2fMB/s", m.NetworkBytesSentRate/(1024*1024)) case "network_bytes_recv_rate": return fmt.Sprintf("%.2fMB/s", m.NetworkBytesRecvRate/(1024*1024)) + case "goroutine_count": + return fmt.Sprintf("%d", m.GoroutineCount) default: return "N/A" } diff --git a/frontend/crawlab-ui/src/i18n/lang/en/components/metric.ts b/frontend/crawlab-ui/src/i18n/lang/en/components/metric.ts index 72787033..4bde5e85 100644 --- a/frontend/crawlab-ui/src/i18n/lang/en/components/metric.ts +++ b/frontend/crawlab-ui/src/i18n/lang/en/components/metric.ts @@ -16,6 +16,7 @@ const metric: LComponentsMetric = { disk_write_bytes_rate: 'Disk Write IO', network_bytes_sent_rate: 'Network Sent IO', network_bytes_recv_rate: 'Network Recv IO', + goroutine_count: 'Goroutine Count', connections: 'Connections', query_per_second: 'Queries/sec', cache_hit_ratio: 'Cache Hit Ratio', diff --git a/frontend/crawlab-ui/src/i18n/lang/zh/components/metric.ts b/frontend/crawlab-ui/src/i18n/lang/zh/components/metric.ts index 53e92105..a97c17e8 100644 --- a/frontend/crawlab-ui/src/i18n/lang/zh/components/metric.ts +++ b/frontend/crawlab-ui/src/i18n/lang/zh/components/metric.ts @@ -16,6 +16,7 @@ const metric: LComponentsMetric = { disk_write_bytes_rate: '磁盘写入 IO', network_bytes_sent_rate: '网络发送 IO', network_bytes_recv_rate: '网络接收 IO', + goroutine_count: '协程数量', connections: '连接数', query_per_second: '查询数/秒', cache_hit_ratio: '缓存命中率', diff --git a/frontend/crawlab-ui/src/interfaces/i18n/components/metric.d.ts b/frontend/crawlab-ui/src/interfaces/i18n/components/metric.d.ts index 676e9b77..84daecea 100644 --- a/frontend/crawlab-ui/src/interfaces/i18n/components/metric.d.ts +++ b/frontend/crawlab-ui/src/interfaces/i18n/components/metric.d.ts @@ -21,6 +21,7 @@ interface LComponentsMetric { cache_hit_ratio: string; replication_lag: string; lock_wait_time: string; + goroutine_count: string; }; groups: { disk_io_bytes_rate: string; diff --git a/frontend/crawlab-ui/src/utils/metric.ts b/frontend/crawlab-ui/src/utils/metric.ts index b114747e..853e3be9 100644 --- a/frontend/crawlab-ui/src/utils/metric.ts +++ b/frontend/crawlab-ui/src/utils/metric.ts @@ -84,6 +84,11 @@ export const getAllMetricGroups = (): MetricGroup[] => [ label: t('components.metric.metrics.used_disk'), metrics: ['used_disk'], }, + { + name: 'goroutine_count', + label: t('components.metric.metrics.goroutine_count'), + metrics: ['goroutine_count'], + }, ]; export const getMetricUnitLabel = (metricName: string) => { @@ -95,6 +100,8 @@ export const getMetricUnitLabel = (metricName: string) => { return 'GB'; } else if (metricName.endsWith('_memory')) { return 'MB'; + } else if (metricName === 'goroutine_count') { + return ''; } else { return 'MB'; } diff --git a/grpc/bin/compile_all.sh b/grpc/bin/compile_all.sh old mode 100644 new mode 100755 diff --git a/grpc/metric_service.pb.go b/grpc/metric_service.pb.go index 2d6ea660..8d8b9236 100644 --- a/grpc/metric_service.pb.go +++ b/grpc/metric_service.pb.go @@ -38,6 +38,7 @@ type MetricServiceSendRequest struct { DiskWriteBytesRate float32 `protobuf:"fixed32,16,opt,name=disk_write_bytes_rate,json=diskWriteBytesRate,proto3" json:"disk_write_bytes_rate,omitempty"` NetworkBytesSentRate float32 `protobuf:"fixed32,17,opt,name=network_bytes_sent_rate,json=networkBytesSentRate,proto3" json:"network_bytes_sent_rate,omitempty"` NetworkBytesRecvRate float32 `protobuf:"fixed32,18,opt,name=network_bytes_recv_rate,json=networkBytesRecvRate,proto3" json:"network_bytes_recv_rate,omitempty"` + GoroutineCount int32 `protobuf:"varint,19,opt,name=goroutine_count,json=goroutineCount,proto3" json:"goroutine_count,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -184,13 +185,20 @@ func (x *MetricServiceSendRequest) GetNetworkBytesRecvRate() float32 { return 0 } +func (x *MetricServiceSendRequest) GetGoroutineCount() int32 { + if x != nil { + return x.GoroutineCount + } + return 0 +} + var File_services_metric_service_proto protoreflect.FileDescriptor var file_services_metric_service_proto_rawDesc = []byte{ 0x0a, 0x1d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x67, 0x72, 0x70, 0x63, 0x1a, 0x15, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x2f, 0x72, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x93, 0x05, 0x0a, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xbc, 0x05, 0x0a, 0x18, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x19, 0x0a, @@ -232,12 +240,15 @@ var file_services_metric_service_proto_rawDesc = []byte{ 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x5f, 0x72, 0x65, 0x63, 0x76, 0x5f, 0x72, 0x61, 0x74, 0x65, 0x18, 0x12, 0x20, 0x01, 0x28, 0x02, 0x52, 0x14, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x42, 0x79, 0x74, 0x65, 0x73, 0x52, 0x65, 0x63, 0x76, 0x52, 0x61, - 0x74, 0x65, 0x32, 0x49, 0x0a, 0x0d, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x12, 0x38, 0x0a, 0x04, 0x53, 0x65, 0x6e, 0x64, 0x12, 0x1e, 0x2e, 0x67, 0x72, - 0x70, 0x63, 0x2e, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, - 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x67, 0x72, - 0x70, 0x63, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x08, 0x5a, - 0x06, 0x2e, 0x3b, 0x67, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x67, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5f, + 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x13, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0e, 0x67, 0x6f, 0x72, + 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0x49, 0x0a, 0x0d, 0x4d, + 0x65, 0x74, 0x72, 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x38, 0x0a, 0x04, + 0x53, 0x65, 0x6e, 0x64, 0x12, 0x1e, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x4d, 0x65, 0x74, 0x72, + 0x69, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x3b, 0x67, 0x72, 0x70, 0x63, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/grpc/proto/services/metric_service.proto b/grpc/proto/services/metric_service.proto index 3162b8cd..4ed7f993 100644 --- a/grpc/proto/services/metric_service.proto +++ b/grpc/proto/services/metric_service.proto @@ -22,6 +22,7 @@ message MetricServiceSendRequest { float disk_write_bytes_rate = 16; float network_bytes_sent_rate = 17; float network_bytes_recv_rate = 18; + int32 goroutine_count = 19; } service MetricService {