|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/aws/aws-sdk-go-v2/config" |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/firehose" |
| 16 | + "github.com/aws/aws-sdk-go-v2/service/firehose/types" |
| 17 | + "github.com/prometheus/client_golang/prometheus" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + defaultFirehoseQueueSize = 1000 |
| 22 | + firehosePublishTimeout = 5 * time.Second |
| 23 | +) |
| 24 | + |
| 25 | +var firehoseRecordsTotal = prometheus.NewCounterVec( |
| 26 | + prometheus.CounterOpts{ |
| 27 | + Name: "ingestion_firehose_records_total", |
| 28 | + Help: "Inference telemetry records handled by the optional Amazon Data Firehose mirror.", |
| 29 | + }, |
| 30 | + []string{"status"}, |
| 31 | +) |
| 32 | + |
| 33 | +func init() { |
| 34 | + prometheus.MustRegister(firehoseRecordsTotal) |
| 35 | +} |
| 36 | + |
| 37 | +type firehoseAPI interface { |
| 38 | + PutRecord(context.Context, *firehose.PutRecordInput, ...func(*firehose.Options)) (*firehose.PutRecordOutput, error) |
| 39 | +} |
| 40 | + |
| 41 | +type firehosePublisher struct { |
| 42 | + client firehoseAPI |
| 43 | + streamName string |
| 44 | +} |
| 45 | + |
| 46 | +type firehoseDispatcher struct { |
| 47 | + publisher *firehosePublisher |
| 48 | + queue chan InferenceLog |
| 49 | +} |
| 50 | + |
| 51 | +func newFirehoseDispatcher() (*firehoseDispatcher, error) { |
| 52 | + if !strings.EqualFold(strings.TrimSpace(os.Getenv("FIREHOSE_ENABLED")), "true") { |
| 53 | + return nil, nil |
| 54 | + } |
| 55 | + |
| 56 | + streamName := strings.TrimSpace(os.Getenv("FIREHOSE_DELIVERY_STREAM")) |
| 57 | + if streamName == "" { |
| 58 | + return nil, errors.New("FIREHOSE_DELIVERY_STREAM is required when FIREHOSE_ENABLED=true") |
| 59 | + } |
| 60 | + |
| 61 | + region := strings.TrimSpace(os.Getenv("AWS_REGION")) |
| 62 | + if region == "" { |
| 63 | + region = "us-east-1" |
| 64 | + } |
| 65 | + |
| 66 | + queueSize := defaultFirehoseQueueSize |
| 67 | + if raw := strings.TrimSpace(os.Getenv("FIREHOSE_QUEUE_SIZE")); raw != "" { |
| 68 | + parsed, err := strconv.Atoi(raw) |
| 69 | + if err != nil || parsed < 1 { |
| 70 | + return nil, fmt.Errorf("FIREHOSE_QUEUE_SIZE must be a positive integer: %q", raw) |
| 71 | + } |
| 72 | + queueSize = parsed |
| 73 | + } |
| 74 | + |
| 75 | + cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(region)) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("load AWS configuration: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + dispatcher := &firehoseDispatcher{ |
| 81 | + publisher: &firehosePublisher{ |
| 82 | + client: firehose.NewFromConfig(cfg), |
| 83 | + streamName: streamName, |
| 84 | + }, |
| 85 | + queue: make(chan InferenceLog, queueSize), |
| 86 | + } |
| 87 | + go dispatcher.run() |
| 88 | + |
| 89 | + return dispatcher, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (d *firehoseDispatcher) enqueue(entry InferenceLog) { |
| 93 | + select { |
| 94 | + case d.queue <- entry: |
| 95 | + firehoseRecordsTotal.WithLabelValues("queued").Inc() |
| 96 | + default: |
| 97 | + firehoseRecordsTotal.WithLabelValues("dropped").Inc() |
| 98 | + log.Printf("firehose mirror queue full; dropping telemetry record for model=%s", entry.ModelID) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (d *firehoseDispatcher) run() { |
| 103 | + for entry := range d.queue { |
| 104 | + ctx, cancel := context.WithTimeout(context.Background(), firehosePublishTimeout) |
| 105 | + err := d.publisher.publish(ctx, entry) |
| 106 | + cancel() |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + firehoseRecordsTotal.WithLabelValues("error").Inc() |
| 110 | + log.Printf("firehose PutRecord failed for model=%s: %v", entry.ModelID, err) |
| 111 | + continue |
| 112 | + } |
| 113 | + firehoseRecordsTotal.WithLabelValues("delivered").Inc() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (p *firehosePublisher) publish(ctx context.Context, entry InferenceLog) error { |
| 118 | + payload, err := encodeFirehoseRecord(entry) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + _, err = p.client.PutRecord(ctx, &firehose.PutRecordInput{ |
| 124 | + DeliveryStreamName: &p.streamName, |
| 125 | + Record: &types.Record{ |
| 126 | + Data: payload, |
| 127 | + }, |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("put record: %w", err) |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func encodeFirehoseRecord(entry InferenceLog) ([]byte, error) { |
| 136 | + payload, err := json.Marshal(entry) |
| 137 | + if err != nil { |
| 138 | + return nil, fmt.Errorf("marshal inference log: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + // Firehose concatenates records inside delivered objects. NDJSON keeps each |
| 142 | + // inference event independently parseable after buffering and compression. |
| 143 | + return append(payload, '\n'), nil |
| 144 | +} |
0 commit comments