Skip to content

Commit 920b036

Browse files
LogType bitfield (#861)
* add Service to FirelensMessage * add LogType
1 parent 8077983 commit 920b036

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed

src/pkg/logs/fluent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ type FirelensMessage struct {
1919
EcsCluster string `json:"ecs_cluster,omitempty"` // ECS metadata
2020
Etag string `json:"etag,omitempty"` // added by us
2121
Host string `json:"host,omitempty"` // added by us
22+
Service string `json:"service,omitempty"` // added by us
2223
}

src/pkg/logs/log_type.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package logs
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type LogType uint32
9+
10+
type InvalidLogTypeError struct {
11+
Value string
12+
}
13+
14+
func (e InvalidLogTypeError) Error() string {
15+
return fmt.Sprintf("invalid log type: %q, must be one of %v", e.Value, AllLogTypes)
16+
}
17+
18+
const (
19+
LogTypeUnspecified LogType = 0
20+
LogTypeRun LogType = 1 << iota
21+
LogTypeBuild
22+
23+
LogTypeAll LogType = 0xFFFFFFFF
24+
)
25+
26+
var AllLogTypes = []LogType{
27+
LogTypeRun,
28+
LogTypeBuild,
29+
}
30+
31+
var (
32+
LogType_name = map[LogType]string{
33+
LogTypeUnspecified: "UNSPECIFIED",
34+
LogTypeRun: "RUN",
35+
LogTypeBuild: "BUILD",
36+
LogTypeAll: "ALL",
37+
}
38+
LogType_value = map[string]LogType{
39+
"UNSPECIFIED": LogTypeUnspecified,
40+
"RUN": LogTypeRun,
41+
"BUILD": LogTypeBuild,
42+
"ALL": LogTypeAll,
43+
}
44+
)
45+
46+
func (c *LogType) Set(value string) error {
47+
value = strings.TrimSpace(strings.ToUpper(value))
48+
49+
if value == "" {
50+
*c = LogTypeUnspecified
51+
return nil
52+
}
53+
54+
if value == "ALL" {
55+
*c = LogTypeAll
56+
return nil
57+
}
58+
59+
parts := strings.Split(value, ",")
60+
for _, part := range parts {
61+
logType, ok := LogType_value[part]
62+
if !ok {
63+
return InvalidLogTypeError{Value: value}
64+
}
65+
66+
*c |= logType
67+
}
68+
69+
return nil
70+
}
71+
72+
func (c LogType) Has(logType LogType) bool {
73+
return c&logType != 0
74+
}
75+
76+
func (c LogType) Type() string {
77+
return "log-type"
78+
}
79+
80+
func (c LogType) Value() string {
81+
return c.String()
82+
}
83+
84+
func ParseLogType(value string) (LogType, error) {
85+
var logType LogType
86+
err := logType.Set(value)
87+
return logType, err
88+
}
89+
90+
func (c LogType) String() string {
91+
// convert the bitfield into a comma-separated list of log types
92+
var logTypes []string
93+
for _, logType := range AllLogTypes {
94+
if c&logType != 0 {
95+
logTypes = append(logTypes, LogType_name[logType])
96+
}
97+
}
98+
99+
if len(logTypes) == 0 {
100+
return LogType_name[LogTypeUnspecified]
101+
}
102+
103+
return strings.Join(logTypes, ",")
104+
}

src/pkg/logs/log_type_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package logs
2+
3+
import "testing"
4+
5+
func TestParseLogType(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
value string
9+
want LogType
10+
wantErr bool
11+
}{
12+
{"empty", "", LogTypeUnspecified, false},
13+
{"unspecified", "unspecified", LogTypeUnspecified, true},
14+
{"run", "run", LogTypeRun, false},
15+
{"build", "build", LogTypeBuild, false},
16+
{"all", "all", LogTypeAll, false},
17+
{"invalid", "invalid", LogTypeUnspecified, true},
18+
}
19+
for _, tt := range tests {
20+
t.Run(tt.name, func(t *testing.T) {
21+
got, err := ParseLogType(tt.value)
22+
if err != nil && !tt.wantErr {
23+
t.Errorf("ParseLogType() error = %v, wantErr %v", err, tt.wantErr)
24+
}
25+
if got != tt.want {
26+
t.Errorf("ParseLogType() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}
31+
32+
func TestLogTypeString(t *testing.T) {
33+
tests := []struct {
34+
name string
35+
value LogType
36+
want string
37+
}{
38+
{"unspecified", LogTypeUnspecified, "UNSPECIFIED"},
39+
{"run", LogTypeRun, "RUN"},
40+
{"build", LogTypeBuild, "BUILD"},
41+
{"all", LogTypeAll, "RUN,BUILD"},
42+
}
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
if got := tt.value.String(); got != tt.want {
46+
t.Errorf("LogType.String() = %v, want %v", got, tt.want)
47+
}
48+
})
49+
}
50+
}
51+
52+
func TestLogTypeSet(t *testing.T) {
53+
tests := []struct {
54+
name string
55+
init string
56+
value string
57+
want LogType
58+
wantErr bool
59+
}{
60+
{"empty from unspecified", "", "", LogTypeUnspecified, true},
61+
{"run from unspecified", "", "run", LogTypeRun, false},
62+
{"build from unspecified", "", "build", LogTypeBuild, false},
63+
{"all from unspecified", "", "all", LogTypeAll, false},
64+
{"invalid from unspecified", "", "invalid", LogTypeUnspecified, true},
65+
{"empty from run", "", "", LogTypeRun, true},
66+
{"run from run", "", "run", LogTypeRun, false},
67+
{"build from run", "", "build", LogTypeAll, false},
68+
{"all from run", "", "all", LogTypeAll, false},
69+
{"invalid from run", "", "invalid", LogTypeRun, true},
70+
{"empty from build", "", "", LogTypeBuild, true},
71+
{"run from build", "", "run", LogTypeAll, false},
72+
{"build from build", "", "build", LogTypeBuild, false},
73+
{"all from build", "", "all", LogTypeAll, false},
74+
{"invalid from build", "", "invalid", LogTypeRun, true},
75+
{"empty from all", "", "", LogTypeAll, true},
76+
{"run from all", "", "run", LogTypeAll, false},
77+
{"build from all", "", "build", LogTypeAll, false},
78+
{"all from all", "", "all", LogTypeAll, false},
79+
{"invalid from all", "", "invalid", LogTypeAll, true},
80+
}
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
logType, err := ParseLogType(tt.init)
84+
if err != nil {
85+
t.Errorf("ParseLogType() error = %v", err)
86+
return
87+
}
88+
err = logType.Set(tt.value)
89+
if err != nil && !tt.wantErr {
90+
t.Errorf("LogType.Set() error = %v, wantErr %v", err, tt.wantErr)
91+
}
92+
})
93+
}
94+
}
95+
96+
func TestLogTypeHas(t *testing.T) {
97+
tests := []struct {
98+
name string
99+
value LogType
100+
arg LogType
101+
want bool
102+
}{
103+
{"unspecified has unspecified", LogTypeUnspecified, LogTypeUnspecified, false},
104+
{"unspecified has run", LogTypeUnspecified, LogTypeRun, false},
105+
{"unspecified has build", LogTypeUnspecified, LogTypeBuild, false},
106+
{"unspecified has all", LogTypeUnspecified, LogTypeAll, false},
107+
{"run has unspecified", LogTypeRun, LogTypeUnspecified, false},
108+
{"run has run", LogTypeRun, LogTypeRun, true},
109+
{"run has build", LogTypeRun, LogTypeBuild, false},
110+
{"run has all", LogTypeRun, LogTypeAll, true},
111+
{"build has unspecified", LogTypeBuild, LogTypeUnspecified, false},
112+
{"build has run", LogTypeBuild, LogTypeRun, false},
113+
{"build has build", LogTypeBuild, LogTypeBuild, true},
114+
{"build has all", LogTypeBuild, LogTypeAll, true},
115+
{"all has unspecified", LogTypeAll, LogTypeUnspecified, false},
116+
{"all has run", LogTypeAll, LogTypeRun, true},
117+
{"all has build", LogTypeAll, LogTypeBuild, true},
118+
{"all has all", LogTypeAll, LogTypeAll, true},
119+
}
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
if got := tt.value.Has(tt.arg); got != tt.want {
123+
t.Errorf("LogType.Has() = %v, want %v", got, tt.want)
124+
}
125+
})
126+
}
127+
}

0 commit comments

Comments
 (0)