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
+70 -4
View File
@@ -1,6 +1,7 @@
package metrics
import (
"context"
"math"
"net/http"
"strings"
@@ -25,10 +26,14 @@ const (
MetricJSAPICallsTotal = "caatsm_js_api_calls_total"
MetricDBQueriesTotal = "caatsm_db_queries_total"
MetricDBQueryLatencySeconds = "caatsm_db_query_latency_seconds"
MetricDLQMessagesTotal = "caatsm_dlq_messages_total"
MetricDLQPublishFailures = "caatsm_dlq_publish_failures_total"
MetricPublishFailuresTotal = "caatsm_publish_failures_total"
MetricNATSConsumerPending = "caatsm_nats_consumer_pending_messages"
MetricDLQMessagesTotal = "caatsm_dlq_messages_total"
MetricDLQPublishFailures = "caatsm_dlq_publish_failures_total"
MetricPublishFailuresTotal = "caatsm_publish_failures_total"
MetricNATSConsumerPending = "caatsm_nats_consumer_pending_messages"
MetricAFTNValidationErrorsTotal = "caatsm_aftn_validation_errors_total"
MetricMessageGapSeconds = "caatsm_message_gap_seconds"
MetricMessageSequenceGapTotal = "caatsm_message_sequence_gap_total"
MetricSerialReaderHealthy = "caatsm_serial_reader_healthy"
// Common label keys.
LabelStatus = "status"
@@ -39,6 +44,7 @@ const (
LabelResult = "result"
LabelReason = "reason"
LabelOperation = "operation"
LabelErrorType = "error_type"
// Standard result label values for caatsm_messages_total.
ResultOK = "ok"
@@ -78,6 +84,12 @@ var (
// NATS consumer lag metrics.
natsConsumerPending *prometheus.GaugeVec
// AFTN validation and health metrics.
aftnValidationErrorsTotal *prometheus.CounterVec
messageGapSeconds *prometheus.GaugeVec
messageSequenceGapTotal *prometheus.CounterVec
serialReaderHealthy *prometheus.GaugeVec
)
func initCollectors() {
@@ -154,6 +166,27 @@ func initCollectors() {
Help: "Approximate number of pending messages for a JetStream consumer, labelled by stream and consumer.",
}, []string{LabelStream, LabelConsumer})
// AFTN validation and health metrics.
aftnValidationErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: MetricAFTNValidationErrorsTotal,
Help: "Total number of AFTN protocol validation errors, labelled by error type.",
}, []string{LabelErrorType})
messageGapSeconds = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: MetricMessageGapSeconds,
Help: "Time in seconds since the last message was received from the serial reader.",
}, []string{LabelStream, LabelConsumer})
messageSequenceGapTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: MetricMessageSequenceGapTotal,
Help: "Total number of message sequence gaps detected (missing sequence numbers).",
}, []string{LabelStream, LabelConsumer})
serialReaderHealthy = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: MetricSerialReaderHealthy,
Help: "Serial reader health status: 1 = healthy (messages flowing), 0 = stalled (no messages).",
}, []string{LabelStream, LabelConsumer})
registry.MustRegister(
processedCounter,
failureCounter,
@@ -168,6 +201,10 @@ func initCollectors() {
dbQueriesTotal,
dbQueryLatency,
natsConsumerPending,
aftnValidationErrorsTotal,
messageGapSeconds,
messageSequenceGapTotal,
serialReaderHealthy,
)
}
@@ -272,6 +309,35 @@ func RecordNATSConsumerPending(stream, consumer string, pending uint64) {
natsConsumerPending.WithLabelValues(streamLabel, consumerLabel).Set(float64(pending))
}
// RecordAFTNValidationError increments the AFTN validation error counter for the given error type.
func RecordAFTNValidationError(ctx context.Context, errorType string) {
ensureCollectors()
aftnValidationErrorsTotal.WithLabelValues(labelValue(errorType)).Inc()
}
// RecordMessageGap records the time gap (in seconds) since the last message was received.
func RecordMessageGap(stream, consumer string, gapSeconds float64) {
ensureCollectors()
messageGapSeconds.WithLabelValues(labelValue(stream), labelValue(consumer)).Set(gapSeconds)
}
// RecordSequenceGap increments the sequence gap counter when missing sequence numbers are detected.
func RecordSequenceGap(stream, consumer string, gapSize uint64) {
ensureCollectors()
messageSequenceGapTotal.WithLabelValues(labelValue(stream), labelValue(consumer)).Add(float64(gapSize))
}
// RecordSerialReaderHealth sets the serial reader health status.
// healthy=1 means messages are flowing normally, healthy=0 means the reader has stalled.
func RecordSerialReaderHealth(stream, consumer string, healthy bool) {
ensureCollectors()
value := 0.0
if healthy {
value = 1.0
}
serialReaderHealthy.WithLabelValues(labelValue(stream), labelValue(consumer)).Set(value)
}
func labelValue(value string) string {
value = strings.TrimSpace(value)
if value == "" {