Skip to content

Commit

Permalink
feat(lokicompliance): emit more fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 27, 2024
1 parent 511b242 commit 9b5755a
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions internal/lokicompliance/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"context"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"net/netip"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -168,12 +170,17 @@ func sendLogs(ctx context.Context, client ht.Client, target string, body push.Pu
type LogEntry struct {
Timestamp time.Time
Level plog.SeverityNumber
Method string
Status int
Took time.Duration
Size uint64
SpanID pcommon.SpanID
TraceID pcommon.TraceID

// HTTP attributes.
Protocol string
Method string
ClientAddr netip.AddrPort
Status int
Took time.Duration
Size uint64

SpanID pcommon.SpanID
TraceID pcommon.TraceID
}

// NewLogEntry generates new [LogEntry].
Expand All @@ -188,6 +195,11 @@ func NewLogEntry(r *rand.Rand, ts time.Time) LogEntry {
plog.SeverityNumberError,
plog.SeverityNumberFatal,
}),
Protocol: randomElement(r, []string{
semconv.HTTPFlavorHTTP10.Value.Emit(),
semconv.HTTPFlavorHTTP11.Value.Emit(),
semconv.HTTPFlavorHTTP20.Value.Emit(),
}),
Method: randomElement(r, []string{
http.MethodGet,
http.MethodHead,
Expand All @@ -196,6 +208,7 @@ func NewLogEntry(r *rand.Rand, ts time.Time) LogEntry {
http.MethodPatch,
http.MethodDelete,
}),
ClientAddr: randomAddr(r),
Status: randomElement(r, []int{
http.StatusOK,
http.StatusCreated,
Expand Down Expand Up @@ -229,6 +242,12 @@ func randomTraceID(r *rand.Rand) (s pcommon.TraceID) {
return s
}

func randomAddr(r *rand.Rand) netip.AddrPort {
var buf [4]byte
_, _ = r.Read(buf[:])
return netip.AddrPortFrom(netip.AddrFrom4(buf), uint16(r.Intn(math.MaxUint16)))
}

func randomElement[S ~[]T, T any](r *rand.Rand, s S) (zero T) {
if len(s) == 0 {
return zero
Expand All @@ -254,9 +273,15 @@ func (e LogEntry) EncodeJSON(enc *jx.Encoder) {
enc.Field("level", func(enc *jx.Encoder) {
enc.Str(e.Level.String())
})
enc.Field("protocol", func(enc *jx.Encoder) {
enc.Str(e.Protocol)
})
enc.Field("method", func(enc *jx.Encoder) {
enc.Str(e.Method)
})
enc.Field("client_ip", func(enc *jx.Encoder) {
enc.Str(e.ClientAddr.String())
})
enc.Field("status", func(enc *jx.Encoder) {
enc.Int(e.Status)
})
Expand Down

0 comments on commit 9b5755a

Please sign in to comment.