This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sprint.go
69 lines (62 loc) · 1.65 KB
/
sprint.go
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
package mntr
import (
"fmt"
"github.com/caos/orbos/internal/ingestion"
"github.com/golang/protobuf/ptypes"
structpb "github.com/golang/protobuf/ptypes/struct"
"strings"
)
func EventRecord(namespace, evt string, fields map[string]string) *ingestion.EventRequest {
return &ingestion.EventRequest{
CreationDate: ptypes.TimestampNow(),
Data: &structpb.Struct{
Fields: protoStruct(fields),
},
Type: strings.ReplaceAll(strings.ToLower(fmt.Sprintf("%s.%s", namespace, evt)), " ", "."),
}
}
func protoStruct(fields map[string]string) map[string]*structpb.Value {
pstruct := make(map[string]*structpb.Value)
for key, value := range fields {
if key == "ts" {
continue
}
pstruct[key] = &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: value}}
}
return pstruct
}
func CommitRecord(fields []*Field) string {
stringFields := make([]string, len(fields))
for _, field := range fields {
if field.Key == "evt" {
stringFields = append(stringFields, fmt.Sprint(field.Value))
continue
}
if field.Key == "file" {
continue
}
if field.Key == "err" {
stringFields = append(stringFields, "An error occurred")
}
stringFields = append(stringFields, fmt.Sprintf("%s: %s", field.Key, field.Value))
}
return strings.Join(stringFields, "\n")
}
func LogRecord(fields []*Field) string {
logLine := ""
for _, field := range fields {
var color string
switch field.Key {
case "msg":
fallthrough
case "evt":
color = "1;35"
case "err":
color = "1;31"
default:
color = "0;33"
}
logLine = fmt.Sprintf("%s %s=\x1b[%sm\"%v\"\x1b[0m", logLine, field.Key, color, field.Value)
}
return strings.TrimSpace(logLine) + "\n"
}