Skip to content

Commit a3c2803

Browse files
authored
Add CPU info to the service info (#1511)
1 parent dffa4ae commit a3c2803

File tree

14 files changed

+556
-249
lines changed

14 files changed

+556
-249
lines changed

packages/api/internal/api/spec.gen.go

Lines changed: 119 additions & 117 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/api/types.gen.go

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/internal/orchestrator/admin.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ func (o *Orchestrator) AdminNodes(ctx context.Context) []*api.Node {
2424

2525
meta := n.Metadata()
2626
metrics := n.GetAPIMetric()
27+
machineInfo := n.MachineInfo()
2728
apiNodes[n.ID] = &api.Node{
28-
NodeID: n.NomadNodeShortID,
29-
Id: n.ID,
30-
ServiceInstanceID: meta.ServiceInstanceID,
31-
ClusterID: n.ClusterID.String(),
29+
NodeID: n.NomadNodeShortID,
30+
Id: n.ID,
31+
ServiceInstanceID: meta.ServiceInstanceID,
32+
ClusterID: n.ClusterID.String(),
33+
MachineInfo: api.MachineInfo{
34+
CpuArchitecture: machineInfo.CPUArchitecture,
35+
CpuFamily: machineInfo.CPUFamily,
36+
CpuModel: machineInfo.CPUModel,
37+
CpuModelName: machineInfo.CPUModelName,
38+
},
3239
Status: n.Status(),
3340
CreateSuccesses: n.PlacementMetrics.SuccessCount(),
3441
CreateFails: n.PlacementMetrics.FailsCount(),
@@ -70,13 +77,19 @@ func (o *Orchestrator) AdminNodeDetail(clusterID uuid.UUID, nodeIDOrNomadNodeSho
7077

7178
meta := n.Metadata()
7279
metrics := n.GetAPIMetric()
80+
machineInfo := n.MachineInfo()
7381

7482
node := &api.NodeDetail{
7583
Id: n.ID,
7684
NodeID: n.NomadNodeShortID,
7785
ClusterID: n.ClusterID.String(),
7886
ServiceInstanceID: meta.ServiceInstanceID,
79-
87+
MachineInfo: api.MachineInfo{
88+
CpuArchitecture: machineInfo.CPUArchitecture,
89+
CpuFamily: machineInfo.CPUFamily,
90+
CpuModel: machineInfo.CPUModel,
91+
CpuModelName: machineInfo.CPUModelName,
92+
},
8093
Status: n.Status(),
8194
CreateSuccesses: n.PlacementMetrics.SuccessCount(),
8295
CreateFails: n.PlacementMetrics.FailsCount(),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package nodemanager
2+
3+
import (
4+
infogrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
5+
)
6+
7+
type MachineInfo struct {
8+
CPUArchitecture string
9+
CPUFamily string
10+
CPUModel string
11+
CPUModelName string
12+
CPUFlags []string
13+
}
14+
15+
func (n *Node) setMachineInfo(info *infogrpc.MachineInfo) {
16+
n.mutex.Lock()
17+
defer n.mutex.Unlock()
18+
19+
if info == nil {
20+
n.machineInfo = MachineInfo{}
21+
22+
return
23+
}
24+
25+
n.machineInfo = MachineInfo{
26+
CPUArchitecture: info.GetCpuArchitecture(),
27+
CPUFamily: info.GetCpuFamily(),
28+
CPUModel: info.GetCpuModel(),
29+
CPUModelName: info.GetCpuModelName(),
30+
CPUFlags: info.GetCpuFlags(),
31+
}
32+
}
33+
34+
func (n *Node) MachineInfo() MachineInfo {
35+
n.mutex.RLock()
36+
defer n.mutex.RUnlock()
37+
38+
return n.machineInfo
39+
}

packages/api/internal/orchestrator/nodemanager/node.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ type Node struct {
4646
metrics Metrics
4747
metricsMu sync.RWMutex
4848

49-
meta NodeMetadata
49+
machineInfo MachineInfo
50+
meta NodeMetadata
5051

5152
buildCache *ttlcache.Cache[string, any]
5253

@@ -107,6 +108,7 @@ func New(
107108
},
108109
}
109110
n.UpdateMetricsFromServiceInfoResponse(nodeInfo)
111+
n.setMachineInfo(nodeInfo.GetMachineInfo())
110112

111113
return n, nil
112114
}
@@ -156,6 +158,7 @@ func NewClusterNode(ctx context.Context, client *grpclient.GRPCClient, clusterID
156158
}
157159

158160
n.UpdateMetricsFromServiceInfoResponse(nodeInfo)
161+
n.setMachineInfo(nodeInfo.GetMachineInfo())
159162

160163
return n, nil
161164
}

packages/api/internal/orchestrator/nodemanager/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (n *Node) Sync(ctx context.Context, store *sandbox.Store) {
3333
}
3434

3535
n.setStatus(ctx, nodeStatus)
36+
n.setMachineInfo(nodeInfo.GetMachineInfo())
3637
n.setMetadata(
3738
NodeMetadata{
3839
ServiceInstanceID: nodeInfo.GetServiceId(),

packages/orchestrator/info.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ message DiskMetrics {
2525
uint64 total_bytes = 5;
2626
}
2727

28+
29+
message MachineInfo {
30+
string cpu_architecture = 1;
31+
string cpu_family = 2;
32+
string cpu_model = 3;
33+
string cpu_model_name = 4;
34+
repeated string cpu_flags = 5;
35+
}
36+
2837
message ServiceInfoResponse {
2938
string node_id = 1;
3039
string service_id = 2;
@@ -34,6 +43,7 @@ message ServiceInfoResponse {
3443
ServiceInfoStatus service_status = 51;
3544
repeated ServiceInfoRole service_roles = 52;
3645
google.protobuf.Timestamp service_startup = 53;
46+
MachineInfo machine_info = 54;
3747

3848
int64 metric_vcpu_used = 101 [deprecated = true];
3949
int64 metric_memory_used_mb = 102 [deprecated = true];

packages/orchestrator/internal/service/info.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go.uber.org/zap"
99

1010
"github.com/e2b-dev/infra/packages/orchestrator/internal/cfg"
11+
"github.com/e2b-dev/infra/packages/orchestrator/internal/service/machineinfo"
1112
orchestratorinfo "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
1213
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
1314
)
@@ -19,8 +20,9 @@ type ServiceInfo struct {
1920
SourceVersion string
2021
SourceCommit string
2122

22-
Startup time.Time
23-
Roles []orchestratorinfo.ServiceInfoRole
23+
Startup time.Time
24+
Roles []orchestratorinfo.ServiceInfoRole
25+
MachineInfo machineinfo.MachineInfo
2426

2527
status orchestratorinfo.ServiceInfoStatus
2628
statusMu sync.RWMutex
@@ -48,7 +50,7 @@ func (s *ServiceInfo) SetStatus(ctx context.Context, status orchestratorinfo.Ser
4850
}
4951
}
5052

51-
func NewInfoContainer(ctx context.Context, clientId string, version string, commit string, instanceID string, config cfg.Config) *ServiceInfo {
53+
func NewInfoContainer(ctx context.Context, clientId string, version string, commit string, instanceID string, machineInfo machineinfo.MachineInfo, config cfg.Config) *ServiceInfo {
5254
services := cfg.GetServices(config)
5355
serviceRoles := make([]orchestratorinfo.ServiceInfoRole, 0)
5456

@@ -59,10 +61,11 @@ func NewInfoContainer(ctx context.Context, clientId string, version string, comm
5961
}
6062

6163
serviceInfo := &ServiceInfo{
62-
ClientId: clientId,
63-
ServiceId: instanceID,
64-
Startup: time.Now(),
65-
Roles: serviceRoles,
64+
ClientId: clientId,
65+
ServiceId: instanceID,
66+
Startup: time.Now(),
67+
Roles: serviceRoles,
68+
MachineInfo: machineInfo,
6669

6770
SourceVersion: version,
6871
SourceCommit: commit,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package machineinfo
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/shirou/gopsutil/v4/cpu"
8+
)
9+
10+
type MachineInfo struct {
11+
Family string
12+
Model string
13+
ModelName string
14+
Flags []string
15+
Arch string
16+
}
17+
18+
func Detect() (MachineInfo, error) {
19+
info, err := cpu.Info()
20+
if err != nil {
21+
return MachineInfo{}, fmt.Errorf("failed to get CPU info: %w", err)
22+
}
23+
24+
if len(info) > 0 {
25+
if info[0].Family == "" || info[0].Model == "" {
26+
return MachineInfo{}, fmt.Errorf("unable to detect CPU platform from CPU info: %+v", info[0])
27+
}
28+
29+
return MachineInfo{
30+
Family: info[0].Family,
31+
Model: info[0].Model,
32+
ModelName: info[0].ModelName,
33+
Flags: info[0].Flags,
34+
Arch: runtime.GOARCH,
35+
}, nil
36+
}
37+
38+
return MachineInfo{}, fmt.Errorf("unable to detect CPU platform from any source")
39+
}

packages/orchestrator/internal/service/service_info.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/e2b-dev/infra/packages/orchestrator/internal/metrics"
1111
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
12+
"github.com/e2b-dev/infra/packages/orchestrator/internal/service/machineinfo"
1213
orchestratorinfo "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
1314
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
1415
)
@@ -72,6 +73,7 @@ func (s *Server) ServiceInfo(ctx context.Context, _ *emptypb.Empty) (*orchestrat
7273

7374
ServiceStartup: timestamppb.New(info.Startup),
7475
ServiceRoles: info.Roles,
76+
MachineInfo: convertMachineInfo(info.MachineInfo),
7577

7678
// Allocated resources to sandboxes
7779
MetricCpuAllocated: sandboxVCpuAllocated,
@@ -113,6 +115,17 @@ func convertDiskMetrics(disks []metrics.DiskInfo) []*orchestratorinfo.DiskMetric
113115
return result
114116
}
115117

118+
// convertDiskMetrics converts internal DiskInfo to protobuf DiskMetrics
119+
func convertMachineInfo(machineInfo machineinfo.MachineInfo) *orchestratorinfo.MachineInfo {
120+
return &orchestratorinfo.MachineInfo{
121+
CpuArchitecture: machineInfo.Arch,
122+
CpuFamily: machineInfo.Family,
123+
CpuModel: machineInfo.Model,
124+
CpuModelName: machineInfo.ModelName,
125+
CpuFlags: machineInfo.Flags,
126+
}
127+
}
128+
116129
func (s *Server) ServiceStatusOverride(ctx context.Context, req *orchestratorinfo.ServiceStatusChangeRequest) (*emptypb.Empty, error) {
117130
logger.L().Info(ctx, "service status override request received", zap.String("status", req.GetServiceStatus().String()))
118131
s.info.SetStatus(ctx, req.GetServiceStatus())

0 commit comments

Comments
 (0)