✨ 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:
co-authored by
Claude Sonnet 4.5
parent
6eff35b56f
commit
7b6f6383ad
@@ -22,6 +22,7 @@ type Config struct {
|
||||
Telemetry TelemetryConfig `koanf:"telemetry"`
|
||||
Monitoring MonitoringConfig `koanf:"monitoring"`
|
||||
DLQ DLQConfig `koanf:"dlq"`
|
||||
AFTN AFTNConfig `koanf:"aftn"`
|
||||
// Legacy fields for backward compatibility during migration
|
||||
Subscription SubscriptionConfig `koanf:"subscription"`
|
||||
Timeouts TimeoutsConfig `koanf:"timeouts"`
|
||||
@@ -146,6 +147,19 @@ type DLQConfig struct {
|
||||
Subject string `koanf:"subject"`
|
||||
}
|
||||
|
||||
// AFTNConfig defines AFTN protocol validation and monitoring settings
|
||||
type AFTNConfig struct {
|
||||
// ValidationEnabled enables AFTN protocol validation
|
||||
ValidationEnabled bool `koanf:"validation_enabled"`
|
||||
|
||||
// MessageGapThreshold is the duration after which the serial reader
|
||||
// is considered stalled (no messages received). Default: 2 minutes.
|
||||
MessageGapThreshold time.Duration `koanf:"message_gap_threshold"`
|
||||
|
||||
// EnableSequenceGapDetection enables monitoring for missing sequence numbers
|
||||
EnableSequenceGapDetection bool `koanf:"enable_sequence_gap_detection"`
|
||||
}
|
||||
|
||||
// MonitoringConfig controls the lightweight HTTP server that exposes health and metrics endpoints.
|
||||
type MonitoringConfig struct {
|
||||
Disabled bool `koanf:"disabled"`
|
||||
@@ -317,6 +331,11 @@ func LoadConfig() (*Config, error) {
|
||||
cfg.Monitoring.HealthTimeout = 2 * time.Second
|
||||
}
|
||||
|
||||
// Set AFTN defaults
|
||||
if cfg.AFTN.MessageGapThreshold == 0 {
|
||||
cfg.AFTN.MessageGapThreshold = 2 * time.Minute
|
||||
}
|
||||
|
||||
// Validate configuration
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("config validation failed: %w", err)
|
||||
@@ -406,6 +425,10 @@ func (c *Config) Validate() error {
|
||||
if c.Monitoring.HealthTimeout < 0 {
|
||||
return fmt.Errorf("monitoring.health_timeout must be >= 0")
|
||||
}
|
||||
// Validate AFTN configuration
|
||||
if c.AFTN.MessageGapThreshold < 0 {
|
||||
return fmt.Errorf("aftn.message_gap_threshold must be >= 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -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 == "" {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -42,6 +42,9 @@ type Recorder interface {
|
||||
|
||||
// RecordJSAPICall records a JetStream API call.
|
||||
RecordJSAPICall(operation string)
|
||||
|
||||
// RecordAFTNValidationError records an AFTN protocol validation failure.
|
||||
RecordAFTNValidationError(ctx context.Context, errorType string)
|
||||
}
|
||||
|
||||
// ProvideRecorder wires a composite Recorder based on configuration flags.
|
||||
@@ -100,6 +103,9 @@ func (n *noopRecorder) RecordDLQPublishFailure(ctx context.Context, stream, cons
|
||||
func (n *noopRecorder) RecordJSAPICall(operation string) {
|
||||
}
|
||||
|
||||
func (n *noopRecorder) RecordAFTNValidationError(ctx context.Context, errorType string) {
|
||||
}
|
||||
|
||||
// compositeRecorder fans out all calls to a slice of underlying recorders.
|
||||
type compositeRecorder struct {
|
||||
recorders []Recorder
|
||||
@@ -167,6 +173,12 @@ func (c *compositeRecorder) RecordJSAPICall(operation string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compositeRecorder) RecordAFTNValidationError(ctx context.Context, errorType string) {
|
||||
for _, r := range c.recorders {
|
||||
r.RecordAFTNValidationError(ctx, errorType)
|
||||
}
|
||||
}
|
||||
|
||||
// promRecorder delegates to the Prometheus metrics helpers in the
|
||||
// internal/infra/metrics package.
|
||||
type promRecorder struct{}
|
||||
@@ -213,6 +225,10 @@ func (p *promRecorder) RecordJSAPICall(operation string) {
|
||||
obsmetrics.RecordJSAPICall(operation)
|
||||
}
|
||||
|
||||
func (p *promRecorder) RecordAFTNValidationError(ctx context.Context, errorType string) {
|
||||
obsmetrics.RecordAFTNValidationError(ctx, errorType)
|
||||
}
|
||||
|
||||
// otelRecorder creates and records OpenTelemetry metrics for the CAATSM
|
||||
// processor. It intentionally focuses on a small set of high-value metrics to
|
||||
// avoid duplicating the full Prometheus surface.
|
||||
@@ -305,4 +321,9 @@ func (o *otelRecorder) RecordDLQPublishFailure(ctx context.Context, stream, cons
|
||||
func (o *otelRecorder) RecordJSAPICall(operation string) {
|
||||
}
|
||||
|
||||
func (o *otelRecorder) RecordAFTNValidationError(ctx context.Context, errorType string) {
|
||||
// AFTN validation metrics are primarily tracked via Prometheus.
|
||||
// This is a no-op for OTEL recorder.
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user