Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse

import (
"crypto/tls"
"time"

"github.com/ClickHouse/clickhouse-go/v2"
chdriver "github.com/ClickHouse/clickhouse-go/v2"
Expand Down Expand Up @@ -29,6 +30,8 @@ func New(config *ClickHouseConfig) (DB, error) {
Username: config.Username,
Password: config.Password,
},
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,

// Debug: true,
// Debugf: func(format string, v ...any) {
Expand Down
33 changes: 28 additions & 5 deletions internal/logmq/batchprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,30 @@ func (bp *BatchProcessor) processBatch(_ string, msgs []*mqs.Message) {
// Validate that both Event and Attempt are present.
// The logstore requires both for data consistency.
if entry.Event == nil || entry.Attempt == nil {
logger.Error("invalid log entry: both event and attempt are required",
fields := []zap.Field{
zap.Bool("has_event", entry.Event != nil),
zap.Bool("has_attempt", entry.Attempt != nil),
zap.String("message_id", msg.LoggableID))
zap.String("message_id", msg.LoggableID),
}
if entry.Event != nil {
fields = append(fields, zap.String("event_id", entry.Event.ID))
fields = append(fields, zap.String("tenant_id", entry.Event.TenantID))
}
if entry.Attempt != nil {
fields = append(fields, zap.String("attempt_id", entry.Attempt.ID))
fields = append(fields, zap.String("tenant_id", entry.Attempt.TenantID))
}
logger.Error("invalid log entry: both event and attempt are required", fields...)
msg.Nack()
continue
}

logger.Info("added to batch",
zap.String("message_id", msg.LoggableID),
zap.String("event_id", entry.Event.ID),
zap.String("attempt_id", entry.Attempt.ID),
zap.String("tenant_id", entry.Event.TenantID))

entries = append(entries, entry)
validMsgs = append(validMsgs, msg)
}
Expand All @@ -107,17 +123,24 @@ func (bp *BatchProcessor) processBatch(_ string, msgs []*mqs.Message) {
return
}

if err := bp.logStore.InsertMany(bp.ctx, entries); err != nil {
insertCtx, cancel := context.WithTimeout(bp.ctx, 30*time.Second)
defer cancel()

insertStart := time.Now()
if err := bp.logStore.InsertMany(insertCtx, entries); err != nil {
logger.Error("failed to insert log entries",
zap.Error(err),
zap.Int("entry_count", len(entries)))
zap.Int("entry_count", len(entries)),
zap.Int64("insert_duration_ms", time.Since(insertStart).Milliseconds()))
for _, msg := range validMsgs {
msg.Nack()
}
return
}

logger.Info("batch processed successfully", zap.Int("count", len(validMsgs)))
logger.Info("batch processed successfully",
zap.Int("count", len(validMsgs)),
zap.Int64("insert_duration_ms", time.Since(insertStart).Milliseconds()))

for _, msg := range validMsgs {
msg.Ack()
Expand Down
4 changes: 3 additions & 1 deletion internal/logmq/messagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hookdeck/outpost/internal/consumer"
"github.com/hookdeck/outpost/internal/logging"
"github.com/hookdeck/outpost/internal/mqs"
"go.uber.org/zap"
)

// BatchAdder is the interface for adding messages to a batch processor.
Expand All @@ -29,7 +30,8 @@ func NewMessageHandler(logger *logging.Logger, batchAdder BatchAdder) consumer.M

func (h *messageHandler) Handle(ctx context.Context, msg *mqs.Message) error {
logger := h.logger.Ctx(ctx)
logger.Info("logmq handler")
logger.Info("logmq handler",
zap.String("message_id", msg.LoggableID))
h.batchAdder.Add(ctx, msg)
return nil
}
Loading