-
Notifications
You must be signed in to change notification settings - Fork 4
feat(p987): report why frames fail to decode #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/gaucho-racing/mapache/p987/pkg/logger" | ||
| ) | ||
|
|
||
| // A frame that produces no signals never reaches the live path — it is | ||
| // stored with a status in its metadata and dropped. That is correct | ||
| // behaviour but it made "frames arrive, nothing shows up live" impossible | ||
| // to diagnose without querying ClickHouse, so decode outcomes are counted | ||
| // here and reported. | ||
| // | ||
| // Frames arrive at a few hundred per second, so a line per failure would | ||
| // bury everything else and out-write the disk. Each distinct | ||
| // (bus, can id, reason) logs once when first seen — a new unknown id is | ||
| // visible immediately — and repeats are rolled up on an interval. | ||
|
|
||
| const decodeReportInterval = 30 * time.Second | ||
|
|
||
| // maxReportedKinds bounds the summary line. A bus carrying dozens of | ||
| // undecodable ids should not produce an unbounded log entry. | ||
| const maxReportedKinds = 12 | ||
|
|
||
| type decodeKey struct { | ||
| bus string | ||
| canID int | ||
| status string | ||
| } | ||
|
|
||
| type decodeStat struct { | ||
| total int64 | ||
| sinceReport int64 | ||
| } | ||
|
|
||
| var ( | ||
| decodeMu sync.Mutex | ||
| decodeStats = map[decodeKey]*decodeStat{} | ||
| decodedOK int64 | ||
| decodedFailed int64 | ||
| ) | ||
|
|
||
| // NoteDecodeOutcome records one frame's decode result. | ||
| func NoteDecodeOutcome(bus string, canID int, status, note string) { | ||
| decodeMu.Lock() | ||
| if status == statusOK { | ||
| decodedOK++ | ||
| decodeMu.Unlock() | ||
| return | ||
| } | ||
| decodedFailed++ | ||
|
|
||
| key := decodeKey{bus: bus, canID: canID, status: status} | ||
| stat, ok := decodeStats[key] | ||
| if !ok { | ||
| stat = &decodeStat{} | ||
| decodeStats[key] = stat | ||
| } | ||
| first := stat.total == 0 | ||
| stat.total++ | ||
| stat.sinceReport++ | ||
| decodeMu.Unlock() | ||
|
|
||
| if first { | ||
| logger.SugarLogger.Warnf("[DECODE] %s 0x%X %s: %s", bus, canID, status, note) | ||
| } | ||
| } | ||
|
|
||
| // InitDecodeReporting starts the rollup. Runs for the life of the process. | ||
| func InitDecodeReporting() { | ||
| go func() { | ||
| ticker := time.NewTicker(decodeReportInterval) | ||
| defer ticker.Stop() | ||
| for range ticker.C { | ||
| reportDecodeOutcomes() | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func reportDecodeOutcomes() { | ||
| decodeMu.Lock() | ||
| ok, failed := decodedOK, decodedFailed | ||
| decodedOK, decodedFailed = 0, 0 | ||
|
|
||
| type entry struct { | ||
| key decodeKey | ||
| count int64 | ||
| } | ||
| entries := make([]entry, 0, len(decodeStats)) | ||
| for k, s := range decodeStats { | ||
| if s.sinceReport > 0 { | ||
| entries = append(entries, entry{k, s.sinceReport}) | ||
| s.sinceReport = 0 | ||
| } | ||
| } | ||
| decodeMu.Unlock() | ||
|
|
||
| if ok == 0 && failed == 0 { | ||
| return | ||
| } | ||
|
|
||
| // Loudest offenders first — that is what you act on. | ||
| sort.Slice(entries, func(i, j int) bool { return entries[i].count > entries[j].count }) | ||
|
|
||
| var b strings.Builder | ||
| fmt.Fprintf(&b, "[DECODE] %s: %d decoded, %d undecodable", decodeReportInterval, ok, failed) | ||
| if len(entries) > 0 { | ||
| b.WriteString(" —") | ||
| for i, e := range entries { | ||
| if i == maxReportedKinds { | ||
| fmt.Fprintf(&b, " (+%d more kinds)", len(entries)-i) | ||
| break | ||
| } | ||
| fmt.Fprintf(&b, " %s/0x%X %s ×%d", e.key.bus, e.key.canID, e.key.status, e.count) | ||
| } | ||
| } | ||
|
|
||
| if failed > 0 { | ||
| logger.SugarLogger.Warnln(b.String()) | ||
| return | ||
| } | ||
| logger.SugarLogger.Infoln(b.String()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When multiple p987 vehicles publish through this service, this key and the global success/failure counters merge their outcomes because
vehicleIDis discarded before reporting. A healthy vehicle's decoded frames can therefore mask that another vehicle has zero successful decodes, while the first warning cannot identify which vehicle failed. Since the subscribed topic and upload-key validation are both vehicle-specific, include the vehicle ID in the report key, totals, and log output.Useful? React with 👍 / 👎.