Brijesh's Git Server — argus-core @ 590f666c334fbab570964e0ef9a06c3161efb729

Logging service

internal/logs/validation.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
package logs

import (
	"fmt"
	"time"

	pb "argus-core/rpc/logs"
)

const (
	MaxBatchSize     = 1000
	MaxMessageLength = 10000
)

func validateSendLogsRequest(req *pb.SendLogsRequest) error {
	if req.ApiKey == "" {
		return fmt.Errorf("%w: API key is required", ErrInvalidInput)
	}

	if len(req.Logs) == 0 {
		return fmt.Errorf("%w: no logs provided", ErrInvalidInput)
	}

	if len(req.Logs) > MaxBatchSize {
		return fmt.Errorf("%w: maximum batch size is %d", ErrTooManyLogs, MaxBatchSize)
	}

	for i, log := range req.Logs {
		if log.Message == "" {
			return fmt.Errorf("%w: empty message in log entry %d", ErrInvalidInput, i)
		}
		if len(log.Message) > MaxMessageLength {
			return fmt.Errorf("%w: message too long in log entry %d", ErrInvalidInput, i)
		}
		if log.Level == pb.LogLevel_UNKNOWN {
			return fmt.Errorf("%w: invalid log level in entry %d", ErrInvalidLogLevel, i)
		}
	}

	return nil
}

func validateGetLogsRequest(req *pb.GetLogsRequest) error {
	if req.Token == "" {
		return fmt.Errorf("%w: token is required", ErrInvalidInput)
	}

	if req.ApplicationId == "" {
		return fmt.Errorf("%w: application ID is required", ErrInvalidInput)
	}

	if req.StartTime != "" {
		if _, err := time.Parse(time.RFC3339, req.StartTime); err != nil {
			return fmt.Errorf("%w: invalid start time format, use RFC3339", ErrInvalidInput)
		}
	}

	return nil
}