Brijesh's Git Server — argus_client @ 7b93aa77b6802acd952901f91a3cb618ab2a67cd

client.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
package argus_client

import (
	"context"
	"errors"
	"log"
	"log/slog"
	"net/http"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"

	"github.com/google/uuid"
	"github.com/wbrijesh/argus_client/rpc/logs"
)

type Client struct {
	apiKey string
	client logs.LogsService
}

type ClientConfig struct {
	ApiKey  string
	BaseUrl string
}

func NewClient(config ClientConfig) *Client {
	return &Client{
		apiKey: config.ApiKey,
		client: logs.NewLogsServiceProtobufClient(config.BaseUrl, &http.Client{}),
	}
}

type LogLevel logs.LogLevel

const (
	LevelDebug LogLevel = LogLevel(logs.LogLevel_DEBUG)
	LevelInfo  LogLevel = LogLevel(logs.LogLevel_INFO)
	LevelWarn  LogLevel = LogLevel(logs.LogLevel_WARN)
	LevelError LogLevel = LogLevel(logs.LogLevel_ERROR)
	LevelFatal LogLevel = LogLevel(logs.LogLevel_FATAL)
)

type LogEntry struct {
	Level   LogLevel
	Message string
}

func (c *Client) SendLogs(entries []LogEntry) error {
	if len(entries) == 0 {
		return nil
	} else if len(entries) > 30 {
		return errors.New("cannot send more than 30 logs at a time")
	}

	ctx := context.Background()

	pbLogs := make([]*logs.LogEntry, len(entries))

	for i, entry := range entries {
		pbLogs[i] = &logs.LogEntry{
			LogId:     uuid.New().String(),
			Timestamp: time.Now().Format(time.RFC3339),
			Level:     logs.LogLevel(entry.Level),
			Message:   entry.Message,
		}
	}

	_, err := c.client.SendLogs(ctx, &logs.SendLogsRequest{
		ApiKey: c.apiKey,
		Logs:   pbLogs,
	})

	return err
}

// Custom slog.Handler implementation with batching and graceful shutdown
type ArgusHandler struct {
	client      *Client
	logBuffer   []LogEntry
	bufferMutex sync.Mutex
	flushTicker *time.Ticker
	stopChan    chan struct{}
}

func NewArgusHandler(client *Client) *ArgusHandler {
	handler := &ArgusHandler{
		client:      client,
		logBuffer:   make([]LogEntry, 0, 30),
		flushTicker: time.NewTicker(5 * time.Second), // Adjust the interval as needed
		stopChan:    make(chan struct{}),
	}

	go handler.startBatching()
	handler.setupSignalHandler()

	return handler
}

func (h *ArgusHandler) startBatching() {
	for {
		select {
		case <-h.flushTicker.C:
			h.flushLogs()
		case <-h.stopChan:
			h.flushLogs()
			return
		}
	}
}

func (h *ArgusHandler) flushLogs() {
	h.bufferMutex.Lock()
	defer h.bufferMutex.Unlock()

	if len(h.logBuffer) == 0 {
		return
	}

	err := h.client.SendLogs(h.logBuffer)
	if err != nil {
		log.Println("Failed to send logs: ", err)
	}

	h.logBuffer = h.logBuffer[:0] // Reset the buffer
}

func (h *ArgusHandler) Handle(ctx context.Context, record slog.Record) error {
	level := map[slog.Level]LogLevel{
		slog.LevelDebug: LevelDebug,
		slog.LevelInfo:  LevelInfo,
		slog.LevelWarn:  LevelWarn,
		slog.LevelError: LevelError,
	}[record.Level]

	entry := LogEntry{
		Level:   level,
		Message: record.Message,
	}

	h.bufferMutex.Lock()
	h.logBuffer = append(h.logBuffer, entry)
	if len(h.logBuffer) >= 30 {
		go h.flushLogs()
	}
	h.bufferMutex.Unlock()

	return nil
}

func (h *ArgusHandler) Enabled(ctx context.Context, level slog.Level) bool {
	return true
}

func (h *ArgusHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	return h
}

func (h *ArgusHandler) WithGroup(name string) slog.Handler {
	return h
}

func (h *ArgusHandler) setupSignalHandler() {
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		<-signalChan
		h.flushTicker.Stop()
		close(h.stopChan)
	}()
}

func (h *ArgusHandler) Flush() {
	h.flushTicker.Stop()
	h.flushLogs()
}