2026-08-15 01:36:51 +08:00

61 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package convert 提供领域模型internal/model与 protobuf 数据契约
// pkg/collectorpb之间的转换函数供 Agent 上报与 Gateway 接收两端共用。
package convert
import (
"git.opencomputing.cn/yumoqing/host-metrics-collector/internal/model"
"git.opencomputing.cn/yumoqing/host-metrics-collector/pkg/collectorpb"
)
// SampleToPB 将内部指标样本转换为 protobuf MetricSample。
func SampleToPB(s model.Sample) *collectorpb.MetricSample {
return &collectorpb.MetricSample{
Name: s.Name,
Value: s.Value,
Timestamp: s.Timestamp,
Labels: s.Labels,
}
}
// SampleFromPB 将 protobuf MetricSample 转换为内部指标样本。
func SampleFromPB(s *collectorpb.MetricSample) model.Sample {
if s == nil {
return model.Sample{}
}
return model.Sample{
Name: s.Name,
Value: s.Value,
Timestamp: s.Timestamp,
Labels: s.Labels,
}
}
// LogToPB 将内部日志记录转换为 protobuf LogEntry。
func LogToPB(r model.LogRecord) *collectorpb.LogEntry {
return &collectorpb.LogEntry{
Timestamp: r.Timestamp,
Level: r.Level,
Source: r.Source,
Message: r.Message,
Fields: r.Fields,
File: r.File,
Offset: r.Offset,
}
}
// LogFromPB 将 protobuf LogEntry 转换为内部日志记录。
func LogFromPB(e *collectorpb.LogEntry) model.LogRecord {
if e == nil {
return model.LogRecord{}
}
return model.LogRecord{
Timestamp: e.Timestamp,
Level: e.Level,
Source: e.Source,
Message: e.Message,
Fields: e.Fields,
File: e.File,
Offset: e.Offset,
}
}