Add AFTN protocol validation and serial reader health monitoring to enhance aviation telegram processing reliability and observability

Implement comprehensive AFTN/ICAO protocol compliance validation with configurable enforcement, enabling early detection of malformed telegrams and reducing downstream processing errors. Add real-time serial reader health monitoring to automatically detect message flow interruptions and sequence gaps, ensuring operational visibility into the telegram ingestion pipeline.

Key enhancements:
- AFTN validator validates priority indicators (FF/GG/QU/DD/SS/KK), ICAO addresses (4-char alphanumeric), and datetime formats (DDHHMM) with detailed error categorization
- Invalid telegrams automatically routed to DLQ with full context for offline review and correction
- Serial reader health monitoring tracks message gaps and sequence numbers to detect stalled readers or missing messages within configurable threshold (default: 2 minutes)
- Four new Prometheus metrics expose validation errors by type, message gaps, sequence gaps, and health status for operational alerting
- Pre-configured Prometheus alert rules for critical conditions (stalled reader, high error rates, consumer lag)
- Grafana dashboard provides real-time visibility into AFTN compliance and serial reader health
- Validation disabled by default for safe rollout with zero breaking changes to existing functionality

Implementation maintains clean architecture with validator in adapter layer, extends processor and consumer with health tracking, and ensures thread-safe concurrent access to tracking state. All changes fully tested with 48 validator tests, 10 processor tests, and 21 consumer tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
windyboy
2025-12-24 14:21:59 +08:00
co-authored by Claude Sonnet 4.5
parent 6eff35b56f
commit 7b6f6383ad
17 changed files with 2071 additions and 12 deletions
+91
View File
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/nats-io/nats.go"
@@ -39,6 +40,11 @@ type Consumer struct {
// State
consecutiveProcessErrors int
// Message tracking for health monitoring
lastMessageTime time.Time
lastMessageSequence uint64
messageGapMutex sync.RWMutex
}
// consumerConfig holds normalized consumer configuration values.
@@ -352,6 +358,11 @@ func (c *Consumer) startJetStream(ctx context.Context) error {
fetchErrorStreak = 0
}
// Update message tracking for health monitoring (track each message)
for _, msg := range msgs {
c.updateMessageTracking(msg)
}
// Process batch
c.batchProcessor.ProcessBatch(ctx, msgs)
}
@@ -391,6 +402,22 @@ func (c *Consumer) emitConsumerStats(ctx context.Context) {
zap.Uint64("pending", info.NumPending),
)
obsmetrics.RecordNATSConsumerPending(c.config.streamName, c.config.consumerName, info.NumPending)
// Record AFTN health metrics
gapSeconds := c.getMessageGapSeconds()
healthy := c.isSerialReaderHealthy()
obsmetrics.RecordMessageGap(c.config.streamName, c.config.consumerName, gapSeconds)
obsmetrics.RecordSerialReaderHealth(c.config.streamName, c.config.consumerName, healthy)
if !healthy {
c.logger.Warn("Serial reader appears stalled - no messages received recently",
zap.String("stream", c.config.streamName),
zap.String("consumer", c.config.consumerName),
zap.Float64("gap_seconds", gapSeconds),
zap.Duration("threshold", c.cfg.AFTN.MessageGapThreshold),
)
}
}
}
}
@@ -423,3 +450,67 @@ func (c *Consumer) Shutdown(ctx context.Context) error {
return fmt.Errorf("nats drain timeout: %w", closeCtx.Err())
}
}
// updateMessageTracking updates the last message time and sequence number for health monitoring.
// This should be called for every message received to track message flow and detect gaps.
func (c *Consumer) updateMessageTracking(msg *nats.Msg) {
if msg == nil {
return
}
c.messageGapMutex.Lock()
defer c.messageGapMutex.Unlock()
now := time.Now()
c.lastMessageTime = now
// Extract sequence number from message metadata
if meta, err := msg.Metadata(); err == nil {
currentSeq := meta.Sequence.Stream
// Detect sequence gaps if we have a previous sequence
if c.lastMessageSequence > 0 && c.cfg.AFTN.EnableSequenceGapDetection {
if currentSeq > c.lastMessageSequence+1 {
gapSize := currentSeq - c.lastMessageSequence - 1
c.logger.Warn("Message sequence gap detected",
zap.String("stream", c.config.streamName),
zap.String("consumer", c.config.consumerName),
zap.Uint64("last_sequence", c.lastMessageSequence),
zap.Uint64("current_sequence", currentSeq),
zap.Uint64("gap_size", gapSize),
)
obsmetrics.RecordSequenceGap(c.config.streamName, c.config.consumerName, gapSize)
}
}
c.lastMessageSequence = currentSeq
}
}
// getMessageGapSeconds returns the number of seconds since the last message was received.
// Returns 0 if no message has been received yet.
func (c *Consumer) getMessageGapSeconds() float64 {
c.messageGapMutex.RLock()
defer c.messageGapMutex.RUnlock()
if c.lastMessageTime.IsZero() {
return 0
}
return time.Since(c.lastMessageTime).Seconds()
}
// isSerialReaderHealthy returns true if messages are being received within the threshold.
// Returns false if the gap exceeds the configured message gap threshold.
func (c *Consumer) isSerialReaderHealthy() bool {
c.messageGapMutex.RLock()
defer c.messageGapMutex.RUnlock()
// If we haven't received any messages yet, consider it healthy (initial state)
if c.lastMessageTime.IsZero() {
return true
}
gap := time.Since(c.lastMessageTime)
return gap < c.cfg.AFTN.MessageGapThreshold
}