Skip to content

Commit e4d6fb6

Browse files
committed
Rename log type to source type where possible
1 parent 56b93f2 commit e4d6fb6

9 files changed

Lines changed: 132 additions & 131 deletions

src/pkg/egress/syslog/filtering_drain_writer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (w *FilteringDrainWriter) Write(env *loggregator_v2.Envelope) error {
7070
}
7171

7272
// shouldIncludeLog determines if a log with the given sourceTypeTag should be forwarded
73-
func shouldIncludeLog(logFilter *LogTypeSet, sourceTypeTag string) bool {
73+
func shouldIncludeLog(logFilter *SourceTypeSet, sourceTypeTag string) bool {
7474
// Empty filter or missing source type means no filtering
7575
if logFilter == nil || sourceTypeTag == "" {
7676
return true
@@ -84,16 +84,16 @@ func shouldIncludeLog(logFilter *LogTypeSet, sourceTypeTag string) bool {
8484
}
8585

8686
// Prefer map lookup over switch for performance
87-
logType := LogType(prefix)
87+
logType := SourceType(prefix)
8888
if !logType.IsValid() {
89-
// Unknown log type, default to not filtering
89+
// Unknown source type, default to not filtering
9090
return true
9191
}
9292

9393
return logFilter.Contains(logType)
9494
}
9595

96-
func sendsLogs(drainData DrainData, logFilter *LogTypeSet, sourceTypeTag string) bool {
96+
func sendsLogs(drainData DrainData, logFilter *SourceTypeSet, sourceTypeTag string) bool {
9797
if drainData != LOGS && drainData != LOGS_AND_METRICS && drainData != LOGS_NO_EVENTS {
9898
return false
9999
}

src/pkg/egress/syslog/filtering_drain_writer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var _ = Describe("Filtering Drain Writer", func() {
8080
})
8181

8282
It("filters logs based on include filter - includes only APP logs", func() {
83-
appFilter := syslog.LogTypeSet{syslog.LOG_APP: struct{}{}}
83+
appFilter := syslog.SourceTypeSet{syslog.SOURCE_APP: struct{}{}}
8484
binding := syslog.Binding{
8585
DrainData: syslog.LOGS,
8686
LogFilter: &appFilter,
@@ -121,9 +121,9 @@ var _ = Describe("Filtering Drain Writer", func() {
121121

122122
It("filters logs based on exclude filter - excludes RTR logs", func() {
123123
// Include APP and STG, effectively excluding RTR
124-
includeFilter := syslog.LogTypeSet{
125-
syslog.LOG_APP: struct{}{},
126-
syslog.LOG_STG: struct{}{},
124+
includeFilter := syslog.SourceTypeSet{
125+
syslog.SOURCE_APP: struct{}{},
126+
syslog.SOURCE_STG: struct{}{},
127127
}
128128
binding := syslog.Binding{
129129
DrainData: syslog.LOGS,
@@ -164,7 +164,7 @@ var _ = Describe("Filtering Drain Writer", func() {
164164
})
165165

166166
It("sends logs with unknown source_type prefix when filter is set", func() {
167-
appFilter := syslog.LogTypeSet{syslog.LOG_APP: struct{}{}}
167+
appFilter := syslog.SourceTypeSet{syslog.SOURCE_APP: struct{}{}}
168168
binding := syslog.Binding{
169169
DrainData: syslog.LOGS,
170170
LogFilter: &appFilter,

src/pkg/egress/syslog/log.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/pkg/egress/syslog/source.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package syslog
2+
3+
import "strings"
4+
5+
// SourceType defines the source types used within Cloud Foundry
6+
// Their order in the code is as documented in https://docs.cloudfoundry.org/devguide/deploy-apps/streaming-logs.html#format
7+
type SourceType string
8+
9+
const (
10+
SOURCE_API SourceType = "API"
11+
SOURCE_STG SourceType = "STG"
12+
SOURCE_RTR SourceType = "RTR"
13+
SOURCE_LGR SourceType = "LGR"
14+
SOURCE_APP SourceType = "APP"
15+
SOURCE_SSH SourceType = "SSH"
16+
SOURCE_CELL SourceType = "CELL"
17+
// TODO PROXY missing. Anything else as well? Also I guess there will be new ones in the future?
18+
)
19+
20+
// validSourceTypes contains SourceType prefixes for efficient lookup
21+
var validSourceTypes = map[SourceType]struct{}{
22+
SOURCE_API: {},
23+
SOURCE_STG: {},
24+
SOURCE_RTR: {},
25+
SOURCE_LGR: {},
26+
SOURCE_APP: {},
27+
SOURCE_SSH: {},
28+
SOURCE_CELL: {},
29+
}
30+
31+
// IsValid checks if the provided SourceType is valid
32+
func (lt SourceType) IsValid() bool {
33+
_, ok := validSourceTypes[lt]
34+
return ok
35+
}
36+
37+
// AllSourceTypes returns all valid source types
38+
func AllSourceTypes() []SourceType {
39+
types := make([]SourceType, 0, len(validSourceTypes))
40+
for t := range validSourceTypes {
41+
types = append(types, t)
42+
}
43+
return types
44+
}
45+
46+
// SourceTypeSet is a set of SourceTypes for efficient membership checking
47+
type SourceTypeSet map[SourceType]struct{}
48+
49+
// Add adds a SourceType to the set
50+
func (s SourceTypeSet) Add(lt SourceType) {
51+
s[lt] = struct{}{}
52+
}
53+
54+
// Contains checks if the set contains a SourceType
55+
func (s SourceTypeSet) Contains(lt SourceType) bool {
56+
_, exists := s[lt]
57+
return exists
58+
}
59+
60+
// ParseSourceType parses a string into a SourceType value
61+
func ParseSourceType(s string) (SourceType, bool) {
62+
lt := SourceType(strings.ToUpper(s))
63+
return lt, lt.IsValid()
64+
}

src/pkg/egress/syslog/syslog_connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Binding struct {
2020
DrainData DrainData `json:"type,omitempty"`
2121
OmitMetadata bool
2222
InternalTls bool
23-
LogFilter *LogTypeSet
23+
LogFilter *SourceTypeSet
2424
}
2525

2626
type Drain struct {

src/pkg/ingress/bindings/binding_config.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,39 @@ func getBindingType(u *url.URL) syslog.DrainData {
8686
return drainData
8787
}
8888

89-
func (d *DrainParamParser) getLogFilter(u *url.URL) *syslog.LogTypeSet {
90-
includeLogTypes := u.Query().Get("include-log-types")
91-
excludeLogTypes := u.Query().Get("exclude-log-types")
92-
93-
if excludeLogTypes != "" {
94-
return d.NewLogTypeSet(excludeLogTypes, true)
95-
} else if includeLogTypes != "" {
96-
return d.NewLogTypeSet(includeLogTypes, false)
89+
func (d *DrainParamParser) getLogFilter(u *url.URL) *syslog.SourceTypeSet {
90+
includeSourceTypes := u.Query().Get("include-source-types")
91+
excludeSourceTypes := u.Query().Get("exclude-source-types")
92+
93+
if excludeSourceTypes != "" {
94+
return d.NewSourceTypeSet(excludeSourceTypes, true)
95+
} else if includeSourceTypes != "" {
96+
return d.NewSourceTypeSet(includeSourceTypes, false)
9797
}
9898
return nil
9999
}
100100

101-
// NewLogTypeSet parses a URL query parameter into a Set of LogTypes.
102-
// logTypeList is assumed to be a comma-separated list of valid log types.
103-
func (d *DrainParamParser) NewLogTypeSet(logTypeList string, isExclude bool) *syslog.LogTypeSet {
101+
// NewSourceTypeSet parses a URL query parameter into a Set of SourceTypes.
102+
// logTypeList is assumed to be a comma-separated list of valid source types.
103+
func (d *DrainParamParser) NewSourceTypeSet(logTypeList string, isExclude bool) *syslog.SourceTypeSet {
104104
if logTypeList == "" {
105105
return nil
106106
}
107107

108108
logTypes := strings.Split(logTypeList, ",")
109-
set := make(syslog.LogTypeSet, len(logTypes))
109+
set := make(syslog.SourceTypeSet, len(logTypes))
110110

111111
for _, logType := range logTypes {
112112
logType = strings.TrimSpace(logType)
113-
t, _ := syslog.ParseLogType(logType)
113+
t, _ := syslog.ParseSourceType(logType)
114114
set.Add(t)
115115
}
116116

117117
if isExclude {
118118
// Invert the set - include all types except those in the set
119-
fullSet := make(syslog.LogTypeSet)
119+
fullSet := make(syslog.SourceTypeSet)
120120

121-
for _, t := range syslog.AllLogTypes() {
121+
for _, t := range syslog.AllSourceTypes() {
122122
fullSet.Add(t)
123123
}
124124

src/pkg/ingress/bindings/binding_config_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,32 @@ var _ = Describe("Drain Param Config", func() {
119119
testCases := []struct {
120120
name string
121121
url string
122-
expected *syslog.LogTypeSet
122+
expected *syslog.SourceTypeSet
123123
}{
124124
{
125125
name: "empty drain URL defaults to all types",
126126
url: "https://test.org/drain",
127-
expected: NewLogTypeSet(),
127+
expected: NewSourceTypeSet(),
128128
},
129129
{
130-
name: "include-log-types=app",
131-
url: "https://test.org/drain?include-log-types=app",
132-
expected: NewLogTypeSet(syslog.LOG_APP),
130+
name: "include-source-types=app",
131+
url: "https://test.org/drain?include-source-types=app",
132+
expected: NewSourceTypeSet(syslog.SOURCE_APP),
133133
},
134134
{
135-
name: "include-log-types=app,stg,cell",
136-
url: "https://test.org/drain?include-log-types=app,stg,cell",
137-
expected: NewLogTypeSet(syslog.LOG_APP, syslog.LOG_STG, syslog.LOG_CELL),
135+
name: "include-source-types=app,stg,cell",
136+
url: "https://test.org/drain?include-source-types=app,stg,cell",
137+
expected: NewSourceTypeSet(syslog.SOURCE_APP, syslog.SOURCE_STG, syslog.SOURCE_CELL),
138138
},
139139
{
140-
name: "exclude-log-types=rtr,cell,stg",
141-
url: "https://test.org/drain?exclude-log-types=rtr,cell,stg",
142-
expected: NewLogTypeSet(syslog.LOG_API, syslog.LOG_LGR, syslog.LOG_APP, syslog.LOG_SSH),
140+
name: "exclude-source-types=rtr,cell,stg",
141+
url: "https://test.org/drain?exclude-source-types=rtr,cell,stg",
142+
expected: NewSourceTypeSet(syslog.SOURCE_API, syslog.SOURCE_LGR, syslog.SOURCE_APP, syslog.SOURCE_SSH),
143143
},
144144
{
145-
name: "exclude-log-types=rtr",
146-
url: "https://test.org/drain?exclude-log-types=rtr",
147-
expected: NewLogTypeSet(syslog.LOG_API, syslog.LOG_STG, syslog.LOG_LGR, syslog.LOG_APP, syslog.LOG_SSH, syslog.LOG_CELL),
145+
name: "exclude-source-types=rtr",
146+
url: "https://test.org/drain?exclude-source-types=rtr",
147+
expected: NewSourceTypeSet(syslog.SOURCE_API, syslog.SOURCE_STG, syslog.SOURCE_LGR, syslog.SOURCE_APP, syslog.SOURCE_SSH, syslog.SOURCE_CELL),
148148
},
149149
}
150150

@@ -252,11 +252,11 @@ func (f *stubFetcher) DrainLimit() int {
252252
return -1
253253
}
254254

255-
func NewLogTypeSet(logTypes ...syslog.LogType) *syslog.LogTypeSet {
255+
func NewSourceTypeSet(logTypes ...syslog.SourceType) *syslog.SourceTypeSet {
256256
if len(logTypes) == 0 {
257257
return nil
258258
}
259-
set := make(syslog.LogTypeSet, len(logTypes))
259+
set := make(syslog.SourceTypeSet, len(logTypes))
260260
for _, t := range logTypes {
261261
set[t] = struct{}{}
262262
}

src/pkg/ingress/bindings/filtered_binding_fetcher.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ func (f *FilteredBindingFetcher) FetchBindings() ([]syslog.Binding, error) {
9898

9999
if invalidLogFilter(u) {
100100
invalidDrains += 1
101-
f.printWarning("include-log-types and exclude-log-types cannot be used at the same time in syslog drain url %s for application %s", anonymousUrl.String(), b.AppId)
101+
f.printWarning("include-source-types and exclude-source-types cannot be used at the same time in syslog drain url %s for application %s", anonymousUrl.String(), b.AppId)
102102
continue
103103
}
104104

105-
logTypes := getUnknownLogTypes(u.Query())
105+
logTypes := getUnknownSourceTypes(u.Query())
106106
if logTypes != nil {
107107
invalidDrains += 1
108-
f.printWarning("Unknown log types '%s' in log type filter in syslog drain url %s for application %s", strings.Join(logTypes, ", "), anonymousUrl.String(), b.AppId)
108+
f.printWarning("Unknown source types '%s' in source type filter in syslog drain url %s for application %s", strings.Join(logTypes, ", "), anonymousUrl.String(), b.AppId)
109109
continue
110110
}
111111

@@ -140,26 +140,26 @@ func (f *FilteredBindingFetcher) FetchBindings() ([]syslog.Binding, error) {
140140
return newBindings, nil
141141
}
142142

143-
// invalidLogFilter checks if both include-log-types and exclude-log-types
143+
// invalidLogFilter checks if both include-source-types and exclude-source-types
144144
func invalidLogFilter(u *url.URL) bool {
145-
includeLogTypes := u.Query().Get("include-log-types")
146-
excludeLogTypes := u.Query().Get("exclude-log-types")
147-
if excludeLogTypes != "" && includeLogTypes != "" {
145+
includeSourceTypes := u.Query().Get("include-source-types")
146+
excludeSourceTypes := u.Query().Get("exclude-source-types")
147+
if excludeSourceTypes != "" && includeSourceTypes != "" {
148148
return true
149149
}
150150
return false
151151
}
152152

153-
// assumes only one of include-log-types or exclude-log-types is set
154-
func getUnknownLogTypes(u url.Values) []string {
153+
// assumes only one of include-source-types or exclude-source-types is set
154+
func getUnknownSourceTypes(u url.Values) []string {
155155
var logTypeList string
156-
includeLogTypes := u.Get("include-log-types")
157-
excludeLogTypes := u.Get("exclude-log-types")
156+
includeSourceTypes := u.Get("include-source-types")
157+
excludeSourceTypes := u.Get("exclude-source-types")
158158

159-
if includeLogTypes != "" {
160-
logTypeList = includeLogTypes
161-
} else if excludeLogTypes != "" {
162-
logTypeList = excludeLogTypes
159+
if includeSourceTypes != "" {
160+
logTypeList = includeSourceTypes
161+
} else if excludeSourceTypes != "" {
162+
logTypeList = excludeSourceTypes
163163
} else {
164164
return nil
165165
}
@@ -169,7 +169,7 @@ func getUnknownLogTypes(u url.Values) []string {
169169

170170
for _, logType := range logTypes {
171171
logType = strings.TrimSpace(logType)
172-
_, ok := syslog.ParseLogType(logType)
172+
_, ok := syslog.ParseSourceType(logType)
173173
if !ok {
174174
unknownTypes = append(unknownTypes, logType)
175175
continue

0 commit comments

Comments
 (0)