feat: add goroutine count metric to the MetricService and update related files

This commit is contained in:
Marvin Zhang
2025-07-08 14:08:31 +08:00
parent 7e4187f388
commit 8bd3ef0b72
10 changed files with 39 additions and 11 deletions

View File

@@ -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)

View File

@@ -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"`
}

View File

@@ -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"
}

View File

@@ -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',

View File

@@ -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: '缓存命中率',

View File

@@ -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;

View File

@@ -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';
}

0
grpc/bin/compile_all.sh Normal file → Executable file
View File

View File

@@ -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 (

View File

@@ -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 {