Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Ding <[email protected]>
  • Loading branch information
jzding committed Nov 16, 2023
1 parent 95e151d commit da640e4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 45 deletions.
99 changes: 69 additions & 30 deletions plugins/ptp_operator/metrics/logparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func extractSummaryMetrics(processName, output string) (iface string, ptpOffset,
return
}

func extractRegularMetrics(processName, output string) (interfaceName string, nmeaPpsStatus, ptpOffset, maxPtpOffset, frequencyAdjustment, delay float64, clockState ptp.SyncState) {
func extractRegularMetrics(processName, output string) (interfaceName string, ptpOffset, maxPtpOffset, frequencyAdjustment, delay float64, clockState ptp.SyncState) {
// remove everything before the rms string
// This makes the out to equal
// ptp4l[5196819.100]: [ptp4l.0.config] master offset -2162130 s2 freq +22451884 path delay 374976
Expand All @@ -102,8 +102,6 @@ func extractRegularMetrics(processName, output string) (interfaceName string, nm
// ts2phc[82674.465]: [ts2phc.0.cfg] nmea delay: 88403525 ns
// ts2phc[82674.465]: [ts2phc.0.cfg] ens2f1 extts index 0 at 1673031129.000000000 corr 0 src 1673031129.911642976 diff 0
// ts2phc[82674.465]: [ts2phc.0.cfg] ens2f1 master offset 0 s2 freq -0
// ts2phc[1699929121]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0
// ts2phc[1700086474]:[ts2phc.0.config] ens7fx offset 0 pps_status 1 s2
// 0 1 2 3 4 5 6 7 8 9 10 11
// 1 2 3 4 5 6 7 8 9
// ptp4l 5196819.100 ptp4l.0.config master offset -2162130 s2 freq +22451884 path delay 374976
Expand All @@ -124,7 +122,6 @@ func extractRegularMetrics(processName, output string) (interfaceName string, nm
// 0 1 2 3 4 5 6 7 8
// ptp4l.0.config master offset -2162130 s2 freq +22451884 delay 374976
// ts2phc.0.cfg ens2f1 master offset 0 s2 freq -0
// ts2phc.0.config ens2f0 nmea_status 0 offset 999999 s0
// ts2phc.0.config ens7fx offset 0 pps_status 1 s2
// (ts2phc.0.cfg master offset 0 s2 freq -0)
if len(fields) < 7 {
Expand All @@ -139,32 +136,6 @@ func extractRegularMetrics(processName, output string) (interfaceName string, nm
fields = fields[:len(fields)-1] // Truncate slice.
}

// .0.config nmea_status 0 offset 999999 s0
if fields[1] == "nmea_status" || fields[1] == "pps_status" {
nmeaPpsStatus, err = strconv.ParseFloat(fields[2], 64)
if err != nil {
log.Errorf("%s failed to parse nmea/pps status from master output %s error %v", processName, fields[2], err)
}
ptpOffset, err = strconv.ParseFloat(fields[4], 64)
if err != nil {
log.Errorf("%s failed to parse nmea offset from master output %s error %v", processName, fields[4], err)
}
return
}

// .0.config ens7fx offset 9999 pps_status 0 s2
if fields[4] == "pps_status" {
nmeaPpsStatus, err = strconv.ParseFloat(fields[5], 64)
if err != nil {
log.Errorf("%s failed to parse pps status from master output %s error %v", processName, fields[5], err)
}
ptpOffset, err = strconv.ParseFloat(fields[3], 64)
if err != nil {
log.Errorf("%s failed to parse pps offset from master output %s error %v", processName, fields[3], err)
}
return
}

if fields[2] != offset {
log.Errorf("%s failed to parse offset from master output %s error %s", processName, fields[2], "offset is not in right order")
return
Expand Down Expand Up @@ -211,6 +182,74 @@ func extractRegularMetrics(processName, output string) (interfaceName string, nm
return
}

func extractPpsStatusMetrics(processName, output string) (interfaceName string, status, ptpOffset float64) {
// ts2phc[1700086474]:[ts2phc.0.config] ens7fx offset 0 pps_status 1 s2
var err error
index := FindInLogForCfgFileIndex(output)
if index == -1 {
log.Errorf("config name is not found in log outpt")
return
}

output = strings.Replace(output, "path", "", 1)
replacer := strings.NewReplacer("[", " ", "]", " ", ":", " ", "phc", "", "sys", "")
output = replacer.Replace(output)

output = output[index:]
fields := strings.Fields(output)

// 0 1 2 3 4 5 6
// ts2phc.0.config ens7fx offset 0 pps_status 1 s2
if len(fields) < 7 {
return
}

interfaceName = fields[1]
status, err = strconv.ParseFloat(fields[5], 64)
if err != nil {
log.Errorf("%s failed to parse pps status from master output %s error %v", processName, fields[5], err)
}
ptpOffset, err = strconv.ParseFloat(fields[3], 64)
if err != nil {
log.Errorf("%s failed to parse pps offset from master output %s error %v", processName, fields[3], err)
}
return
}

func extractNmeaStatusMetrics(processName, output string) (interfaceName string, status, ptpOffset float64) {
// ts2phc[1699929121]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0
var err error
index := FindInLogForCfgFileIndex(output)
if index == -1 {
log.Errorf("config name is not found in log outpt")
return
}

output = strings.Replace(output, "path", "", 1)
replacer := strings.NewReplacer("[", " ", "]", " ", ":", " ", "phc", "", "sys", "")
output = replacer.Replace(output)

output = output[index:]
fields := strings.Fields(output)

// 0 1 2 3 4 5 6
// ts2phc.0.config ens2f0 nmea_status 0 offset 999999 s0
if len(fields) < 7 {
return
}

interfaceName = fields[1]
status, err = strconv.ParseFloat(fields[3], 64)
if err != nil {
log.Errorf("%s failed to parse nmea status from master output %s error %v", processName, fields[3], err)
}
ptpOffset, err = strconv.ParseFloat(fields[5], 64)
if err != nil {
log.Errorf("%s failed to parse nmea offset from master output %s error %v", processName, fields[5], err)
}
return
}

// ExtractPTP4lEvent ... extract event form ptp4l logs
//
// "INITIALIZING to LISTENING on INIT_COMPLETE"
Expand Down
56 changes: 41 additions & 15 deletions plugins/ptp_operator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ func (p *PTPEventManager) ExtractMetrics(msg string) {
UpdatePTPMetrics(master, processName, alias, ptpOffset, maxPtpOffset, frequencyAdjustment, delay)
}
}
} else if strings.Contains(output, "nmea_status") && processName == ts2phcProcessName {
// ts2phc[1699929121]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0
interfaceName, status, ptpOffset := extractNmeaStatusMetrics(processName, output)

offsetSource := master
interfaceType := types.IFace(master)
// ts2phc return actual interface name unlike ptp4l
ptpInterface = ptp4lconf.PTPInterface{Name: interfaceName}

var alias string
r := []rune(interfaceName)
alias = string(r[:len(r)-1]) + "x"
ptpStats[master].SetAlias(alias)
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
if status == UNAVAILABLE {
p.GenPTPEvent(profileName, ptpStats[interfaceType], masterResource, int64(ptpOffset), ptp.FREERUN, ptp.PtpStateChange)
} else {
UpdatePTPOffsetMetrics(offsetSource, processName, alias, ptpOffset)
}
UpdateNmeaStatusMetrics(processName, alias, status)
} else if strings.Contains(output, "pps_status") && processName == ts2phcProcessName {
// ts2phc[1700086474]:[ts2phc.0.config] ens7fx offset 9999 pps_status 0 s2
interfaceName, status, ptpOffset := extractPpsStatusMetrics(processName, output)
offsetSource := master
interfaceType := types.IFace(master)
// ts2phc return actual interface name unlike ptp4l
ptpInterface = ptp4lconf.PTPInterface{Name: interfaceName}

var alias string
r := []rune(interfaceName)
alias = string(r[:len(r)-1]) + "x"
ptpStats[master].SetAlias(alias)
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
if status == UNAVAILABLE {
p.GenPTPEvent(profileName, ptpStats[interfaceType], masterResource, int64(ptpOffset), ptp.FREERUN, ptp.PtpStateChange)
} else {
UpdatePTPOffsetMetrics(offsetSource, processName, alias, ptpOffset)
}
UpdateNmeaStatusMetrics(processName, alias, status)

Check failure on line 215 in plugins/ptp_operator/metrics/metrics.go

View workflow job for this annotation

GitHub Actions / Linting

unnecessary trailing newline (whitespace)
} else if strings.Contains(output, " offset ") &&
(processName != gnssProcessName && processName != dpllProcessName && processName != gmProcessName) {
// ptp4l[5196819.100]: [ptp4l.0.config] master offset -2162130 s2 freq +22451884 path delay 374976
Expand All @@ -183,11 +223,10 @@ func (p *PTPEventManager) ExtractMetrics(msg string) {
// ts2phc[82674.465]: [ts2phc.0.cfg] ens2f1 extts index 0 at 1673031129.000000000 corr 0 src 1673031129.911642976 diff 0
// ts2phc[82674.465]: [ts2phc.0.cfg] ens2f1 master offset 0 s2 freq -0
// ts2phc[82674.465]: [ts2phc.0.cfg] ens7f1 master offset 0 s2 freq -0
// ts2phc[1699894580]:[ts2phc.0.config] ens2fx nmea_status 1 offset 0 s2

// Use threshold to CLOCK_REALTIME==SLAVE, rest send clock state to metrics no events

interfaceName, nmeaPpsStatus, ptpOffset, _, frequencyAdjustment, delay, syncState := extractRegularMetrics(processName, output)
interfaceName, ptpOffset, _, frequencyAdjustment, delay, syncState := extractRegularMetrics(processName, output)
if interfaceName == "" {
return // don't do if iface not known
}
Expand Down Expand Up @@ -269,19 +308,6 @@ func (p *PTPEventManager) ExtractMetrics(msg string) {
alias = string(r[:len(r)-1]) + "x"
ptpStats[master].SetAlias(alias)
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
if interfaceName == "nmea_status" || interfaceName == "pps_status" {
if nmeaPpsStatus == UNAVAILABLE {
p.GenPTPEvent(profileName, ptpStats[interfaceType], masterResource, int64(ptpOffset), ptp.FREERUN, ptp.PtpStateChange)
} else {
UpdatePTPOffsetMetrics(offsetSource, processName, alias, ptpOffset)
}
if interfaceName == "nmea_status" {
UpdateNmeaStatusMetrics(processName, alias, nmeaPpsStatus)
} else {
UpdatePpsStatusMetrics(processName, alias, nmeaPpsStatus)
}
return
}
p.GenPTPEvent(profileName, ptpStats[interfaceType], masterResource, int64(ptpOffset), syncState, ptp.PtpStateChange)
UpdateSyncStateMetrics(processName, alias, ptpStats[interfaceType].LastSyncState())
UpdatePTPMetrics(offsetSource, processName, alias, ptpOffset, float64(ptpStats[interfaceType].MaxAbs()),
Expand Down

0 comments on commit da640e4

Please sign in to comment.