mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 08:45:38 +02:00
⭐ feat: Add protobuf (#171)
This is a second attempt to add protobuf to Nestri, after the first one failed --------- Co-authored-by: Philipp Neumann <3daquawolf@gmail.com> Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com>
This commit is contained in:
@@ -2,20 +2,21 @@ package relay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
gen "relay/internal/proto"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimestampEntry struct {
|
||||
Stage string `json:"stage"`
|
||||
Time string `json:"time"` // ISO 8601 string
|
||||
Stage string `json:"stage"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
// LatencyTracker provides a generic structure for measuring time taken at various stages in message processing.
|
||||
// It can be embedded in message structs for tracking the flow of data and calculating round-trip latency.
|
||||
type LatencyTracker struct {
|
||||
SequenceID string `json:"sequence_id"`
|
||||
Timestamps []TimestampEntry `json:"timestamps"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
SequenceID string `json:"sequence_id"`
|
||||
Timestamps []TimestampEntry `json:"timestamps"`
|
||||
}
|
||||
|
||||
// NewLatencyTracker initializes a new LatencyTracker with the given sequence ID
|
||||
@@ -23,7 +24,6 @@ func NewLatencyTracker(sequenceID string) *LatencyTracker {
|
||||
return &LatencyTracker{
|
||||
SequenceID: sequenceID,
|
||||
Timestamps: make([]TimestampEntry, 0),
|
||||
Metadata: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func (lt *LatencyTracker) AddTimestamp(stage string) {
|
||||
lt.Timestamps = append(lt.Timestamps, TimestampEntry{
|
||||
Stage: stage,
|
||||
// Ensure extremely precise UTC RFC3339 timestamps (down to nanoseconds)
|
||||
Time: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
Time: time.Now().UTC(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -44,15 +44,11 @@ func (lt *LatencyTracker) TotalLatency() (int64, error) {
|
||||
|
||||
var earliest, latest time.Time
|
||||
for _, ts := range lt.Timestamps {
|
||||
t, err := time.Parse(time.RFC3339, ts.Time)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
if earliest.IsZero() || ts.Time.Before(earliest) {
|
||||
earliest = ts.Time
|
||||
}
|
||||
if earliest.IsZero() || t.Before(earliest) {
|
||||
earliest = t
|
||||
}
|
||||
if latest.IsZero() || t.After(latest) {
|
||||
latest = t
|
||||
if latest.IsZero() || ts.Time.After(latest) {
|
||||
latest = ts.Time
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,14 +63,13 @@ func (lt *LatencyTracker) PainPoints(threshold time.Duration) []string {
|
||||
|
||||
for _, ts := range lt.Timestamps {
|
||||
stage := ts.Stage
|
||||
t := ts.Time
|
||||
if lastStage == "" {
|
||||
lastStage = stage
|
||||
lastTime, _ = time.Parse(time.RFC3339, t)
|
||||
lastTime = ts.Time
|
||||
continue
|
||||
}
|
||||
|
||||
currentTime, _ := time.Parse(time.RFC3339, t)
|
||||
currentTime := ts.Time
|
||||
if currentTime.Sub(lastTime) > threshold {
|
||||
painPoints = append(painPoints, fmt.Sprintf("%s -> %s", lastStage, stage))
|
||||
}
|
||||
@@ -87,7 +82,7 @@ func (lt *LatencyTracker) PainPoints(threshold time.Duration) []string {
|
||||
|
||||
// StageLatency calculates the time taken between two specific stages.
|
||||
func (lt *LatencyTracker) StageLatency(startStage, endStage string) (time.Duration, error) {
|
||||
startTime, endTime := "", ""
|
||||
var startTime, endTime time.Time
|
||||
for _, ts := range lt.Timestamps {
|
||||
if ts.Stage == startStage {
|
||||
startTime = ts.Time
|
||||
@@ -97,18 +92,41 @@ func (lt *LatencyTracker) StageLatency(startStage, endStage string) (time.Durati
|
||||
}
|
||||
}
|
||||
|
||||
if startTime == "" || endTime == "" {
|
||||
/*if startTime == "" || endTime == "" {
|
||||
return 0, fmt.Errorf("missing timestamps for stages: %s -> %s", startStage, endStage)
|
||||
}
|
||||
}*/
|
||||
|
||||
start, err := time.Parse(time.RFC3339, startTime)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
end, err := time.Parse(time.RFC3339, endTime)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return end.Sub(start), nil
|
||||
return endTime.Sub(startTime), nil
|
||||
}
|
||||
|
||||
func LatencyTrackerFromProto(protolt *gen.ProtoLatencyTracker) *LatencyTracker {
|
||||
ret := &LatencyTracker{
|
||||
SequenceID: protolt.GetSequenceId(),
|
||||
Timestamps: make([]TimestampEntry, 0),
|
||||
}
|
||||
|
||||
for _, ts := range protolt.GetTimestamps() {
|
||||
ret.Timestamps = append(ret.Timestamps, TimestampEntry{
|
||||
Stage: ts.GetStage(),
|
||||
Time: ts.GetTime().AsTime(),
|
||||
})
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (lt *LatencyTracker) ToProto() *gen.ProtoLatencyTracker {
|
||||
ret := &gen.ProtoLatencyTracker{
|
||||
SequenceId: lt.SequenceID,
|
||||
Timestamps: make([]*gen.ProtoTimestampEntry, len(lt.Timestamps)),
|
||||
}
|
||||
|
||||
for i, timestamp := range lt.Timestamps {
|
||||
ret.Timestamps[i] = &gen.ProtoTimestampEntry{
|
||||
Stage: timestamp.Stage,
|
||||
Time: timestamppb.New(timestamp.Time),
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user