Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions plugins/ptp_operator/alias/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package alias

import "sync"

type store struct {
sync.RWMutex
aliases map[string]string
}

var storeInstance = store{aliases: make(map[string]string)}

// GetAlias returns the alias for the given interface name, or the
// interface name itself when no alias has been registered.
func GetAlias(ifname string) string {
storeInstance.RLock()
defer storeInstance.RUnlock()
if v, ok := storeInstance.aliases[ifname]; ok {
return v
}
return ifname
}

// SetAlias records an alias for the given interface name.
func SetAlias(ifname, alias string) {
storeInstance.Lock()
defer storeInstance.Unlock()
storeInstance.aliases[ifname] = alias
}

// Debug logs every registered alias through the supplied logger function.
func Debug(logF func(string, ...any)) {
storeInstance.RLock()
defer storeInstance.RUnlock()
for ifName, a := range storeInstance.aliases {
logF("DEBUG: ifname: '%s' alias: '%s'\n", ifName, a)
}
}
18 changes: 9 additions & 9 deletions plugins/ptp_operator/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/utils"
"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/alias"
"github.com/redhat-cne/sdk-go/pkg/event/ptp"
log "github.com/sirupsen/logrus"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -125,11 +125,11 @@ func (p *PTPEventState) UpdateCurrentEventState(c ClockState, metrics map[string
}
if clockState.IFace != nil {
iface := *clockState.IFace
alias := utils.GetAlias(iface)
aliasValue := alias.GetAlias(iface)
for k, v := range c.Value {
if clockState.Metric[k].metricGauge != nil {
clockState.Metric[k].metricGauge.With(map[string]string{"from": clockState.Process, "process": clockState.Process,
"node": clockState.NodeName, "iface": alias}).Set(float64(v))
"node": clockState.NodeName, "iface": aliasValue}).Set(float64(v))
} else {
log.Infof("metric object was not found for %s=%s", iface, k)
}
Expand Down Expand Up @@ -190,9 +190,9 @@ func (p *PTPEventState) UpdateCurrentEventState(c ClockState, metrics map[string
if clockState.IFace != nil {
iface = *clockState.IFace
}
alias := utils.GetAlias(iface)
aliasValue := alias.GetAlias(iface)
metrics[k].metricGauge.With(map[string]string{"from": clockState.Process, "process": clockState.Process,
"node": clockState.NodeName, "iface": alias}).Set(float64(v))
"node": clockState.NodeName, "iface": aliasValue}).Set(float64(v))
}
clockState.Metric = metrics
p.DependsOn[clockState.Process] = []*ClockState{clockState}
Expand Down Expand Up @@ -269,21 +269,21 @@ func (p *PTPEventState) DeleteAllMetrics(m []*prometheus.GaugeVec) {
if dd.IFace == nil {
continue
}
alias := utils.GetAlias(*dd.IFace)
aliasValue := alias.GetAlias(*dd.IFace)
if dd.Metric != nil {
// unregister metric
for _, v := range dd.Metric {
if v.metricGauge != nil && dd.IFace != nil {
v.metricGauge.Delete(prometheus.Labels{"process": dd.Process, "iface": alias, "node": dd.NodeName})
v.metricGauge.Delete(prometheus.Labels{"process": dd.Process, "iface": aliasValue, "node": dd.NodeName})
prometheus.Unregister(v.metricGauge)
}
}
for _, mm := range m {
mm.Delete(prometheus.Labels{
"process": dd.Process, "from": dd.Process, "node": dd.NodeName, "iface": alias})
"process": dd.Process, "from": dd.Process, "node": dd.NodeName, "iface": aliasValue})
// find metrics without from - click clock state
mm.Delete(prometheus.Labels{
"process": dd.Process, "node": dd.NodeName, "iface": alias})
"process": dd.Process, "node": dd.NodeName, "iface": aliasValue})
}
}
delete(p.DependsOn, dd.Process)
Expand Down
33 changes: 33 additions & 0 deletions plugins/ptp_operator/metrics/alias_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package metrics

import (
"encoding/json"
"net/http"
"time"

"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/alias"
log "github.com/sirupsen/logrus"
)

const portAliasesEndpoint = "http://localhost:8081/port-aliases"

// SyncAliasesFromDaemon fetches aliases from linuxptp-daemon and populates the local alias store
func SyncAliasesFromDaemon() error {
client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Get(portAliasesEndpoint)
if err != nil {
return err
}
defer resp.Body.Close()

var aliases map[string]string
if err = json.NewDecoder(resp.Body).Decode(&aliases); err != nil {
return err
}

for ifName, aliasValue := range aliases {
alias.SetAlias(ifName, aliasValue)
}
log.Infof("Synced %d port aliases from linuxptp-daemon", len(aliases))
return nil
}
65 changes: 32 additions & 33 deletions plugins/ptp_operator/metrics/logparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"strconv"
"strings"

"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/alias"
"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/event"
"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/stats"
"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/utils"
"k8s.io/utils/pointer"

"github.com/redhat-cne/cloud-event-proxy/plugins/ptp_operator/types"
"github.com/redhat-cne/sdk-go/pkg/event/ptp"
log "github.com/sirupsen/logrus"
"k8s.io/utils/pointer"
)

var (
Expand Down Expand Up @@ -385,17 +384,17 @@ func (p *PTPEventManager) ParseGMLogs(processName, configName, output string, fi
Metric: nil,
NodeName: ptpNodeName,
}
alias := ptpStats[masterType].Alias()
if alias == "" {
alias = utils.GetAlias(iface)
ptpStats[masterType].SetAlias(alias)
aliasValue := ptpStats[masterType].Alias()
if aliasValue == "" {
aliasValue = alias.GetAlias(iface)
ptpStats[masterType].SetAlias(aliasValue)
}
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": alias}).Set(GetSyncStateID(syncState))
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": aliasValue}).Set(GetSyncStateID(syncState))
// status metrics
ptpStats[masterType].SetPtpDependentEventState(clockState, ptpStats.HasMetrics(processName), ptpStats.HasMetricHelp(processName))

// If GM is locked/Freerun/Holdover then ptp state change event
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
masterResource := fmt.Sprintf("%s/%s", aliasValue, MasterClockType)
lastClockState := ptpStats[masterType].LastSyncState()

// When GM is enabled, there is only one event happening at the GM level for now, so it is not being sent to the state decision routine.
Expand All @@ -413,8 +412,8 @@ func (p *PTPEventManager) ParseGMLogs(processName, configName, output string, fi
log.Infof("%s sync state %s, last ptp state is : %s", masterResource, clockState.State, lastClockState)
ptpStats[masterType].SetLastSyncState(clockState.State)
p.PublishEvent(clockState.State, lastOffset, masterResource, ptp.PtpStateChange)
UpdateSyncStateMetrics(processName, alias, ptpStats[masterType].LastSyncState())
UpdatePTPOffsetMetrics(processName, processName, alias, float64(lastOffset))
UpdateSyncStateMetrics(processName, aliasValue, ptpStats[masterType].LastSyncState())
UpdatePTPOffsetMetrics(processName, processName, aliasValue, float64(lastOffset))
}
}

Expand Down Expand Up @@ -459,10 +458,10 @@ func (p *PTPEventManager) ParseTBCLogs(processName, configName, output string, f

tbcClockNameType := types.IFace(stats.TBCMainClockName)

alias := ptpStats[tbcClockNameType].Alias()
if alias == "" {
alias = utils.GetAlias(iface)
ptpStats[tbcClockNameType].SetAlias(alias)
aliasValue := ptpStats[tbcClockNameType].Alias()
if aliasValue == "" {
aliasValue = alias.GetAlias(iface)
ptpStats[tbcClockNameType].SetAlias(aliasValue)
}

clockState := event.ClockState{
Expand All @@ -475,31 +474,31 @@ func (p *PTPEventManager) ParseTBCLogs(processName, configName, output string, f
NodeName: ptpNodeName,
}

SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": alias}).Set(GetSyncStateID(syncState))
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": aliasValue}).Set(GetSyncStateID(syncState))
// status metrics
ptpStats[tbcClockNameType].SetPtpDependentEventState(clockState, ptpStats.HasMetrics(processName), ptpStats.HasMetricHelp(processName))

// If GM is locked/Freerun/Holdover then ptp state change event
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
masterResource := fmt.Sprintf("%s/%s", aliasValue, MasterClockType)
lastClockState := ptpStats[tbcClockNameType].LastSyncState()
ptpStats[tbcClockNameType].SetLastOffset(offs)
lastOffset := ptpStats[tbcClockNameType].LastOffset()

// Update the T-BC offset metric on every status report
UpdatePTPOffsetMetrics(processName, processName, alias, float64(lastOffset))
UpdatePTPOffsetMetrics(processName, processName, aliasValue, float64(lastOffset))

if clockState.State != lastClockState && clockState.State != "" { // publish directly here
log.Infof("%s sync state %s, last ptp state is : %s", masterResource, clockState.State, lastClockState)
ptpStats[tbcClockNameType].SetLastSyncState(clockState.State)
p.PublishEvent(clockState.State, lastOffset, masterResource, ptp.PtpStateChange)
}

UpdateSyncStateMetrics(processName, alias, ptpSyncState)
UpdateSyncStateMetrics(processName, aliasValue, ptpSyncState)

// Impose T-BC state onto the ts2phc process state for the upstream interface
// This is needed because ts2phc doesn't update the upstream interface
// when ptp4l updates it in the T-BC mode
UpdateSyncStateMetrics(ts2phcProcessName, alias, ptpSyncState)
UpdateSyncStateMetrics(ts2phcProcessName, aliasValue, ptpSyncState)

// if there is phc2sys ooptions enabled then when the clock is FREERUN annouce OSCLOCK as FREERUN
if clockState.State == ptp.FREERUN {
Expand Down Expand Up @@ -574,10 +573,10 @@ logStatusLoop:
}

if err == nil {
alias := ptpStats[ifaceType].Alias()
if alias == "" {
alias = utils.GetAlias(*iface)
ptpStats[ifaceType].SetAlias(alias)
aliasValue := ptpStats[ifaceType].Alias()
if aliasValue == "" {
aliasValue = alias.GetAlias(*iface)
ptpStats[ifaceType].SetAlias(aliasValue)
}
ptpStats[ifaceType].SetPtpDependentEventState(event.ClockState{
State: GetSyncState(syncState),
Expand All @@ -594,8 +593,8 @@ logStatusLoop:
ppsStatus: "0=UNAVAILABLE, 1=AVAILABLE",
},
}, ptpStats.HasMetrics(processName), ptpStats.HasMetricHelp(processName))
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": alias}).Set(GetSyncStateID(syncState))
UpdatePTPOffsetMetrics(processName, processName, alias, dpllOffset)
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": aliasValue}).Set(GetSyncStateID(syncState))
UpdatePTPOffsetMetrics(processName, processName, aliasValue, dpllOffset)
} else {
log.Errorf("error parsing dpll %s", err.Error())
}
Expand Down Expand Up @@ -635,17 +634,17 @@ func (p *PTPEventManager) ParseGNSSLogs(processName, configName, output string,

//openshift_ptp_offset_ns{from="gnss",iface="ens2f1",node="cnfde21.ptp.lab.eng.bos.redhat.com",process="gnss"} 0
if err == nil {
alias := ptpStats[ifaceType].Alias()
if alias == "" {
alias = utils.GetAlias(*iface)
ptpStats[ifaceType].SetAlias(alias)
aliasValue := ptpStats[ifaceType].Alias()
if aliasValue == "" {
aliasValue = alias.GetAlias(*iface)
ptpStats[ifaceType].SetAlias(aliasValue)
}
// last state of GNSS
lastState, errState := ptpStats[ifaceType].GetStateState(processName, iface)
pLabels := map[string]string{"from": processName, "node": ptpNodeName,
"process": processName, "iface": alias}
"process": processName, "iface": aliasValue}
PtpOffset.With(pLabels).Set(gnssOffset)
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": alias}).Set(GetSyncStateID(syncState))
SyncState.With(map[string]string{"process": processName, "node": ptpNodeName, "iface": aliasValue}).Set(GetSyncStateID(syncState))
ptpStats[ifaceType].SetPtpDependentEventState(event.ClockState{
State: GetSyncState(syncState),
Offset: pointer.Float64(gnssOffset),
Expand All @@ -659,7 +658,7 @@ func (p *PTPEventManager) ParseGNSSLogs(processName, configName, output string,
// reduce noise ; if state changed then send events
if lastState != GetSyncState(syncState) || errState != nil {
log.Infof("%s last state %s and current state %s", processName, lastState, GetSyncState(syncState))
masterResource := fmt.Sprintf("%s/%s", alias, MasterClockType)
masterResource := fmt.Sprintf("%s/%s", aliasValue, MasterClockType)
p.publishGNSSEvent(gnssState, gnssOffset, GetSyncState(syncState), masterResource, ptp.GnssStateChange)
}
}
Expand Down
Loading
Loading