-
Notifications
You must be signed in to change notification settings - Fork 25
/
syslog_connector.go
177 lines (148 loc) · 4.41 KB
/
syslog_connector.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package syslog
import (
"fmt"
"log"
"golang.org/x/net/context"
metrics "code.cloudfoundry.org/go-metric-registry"
"code.cloudfoundry.org/go-diodes"
"code.cloudfoundry.org/go-loggregator/v9"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress"
)
type Binding struct {
AppId string `json:"appId,omitempty"`
Hostname string `json:"hostname,omitempty"`
Drain Drain `json:"drain,omitempty"`
DrainData DrainData `json:"type,omitempty"`
OmitMetadata bool
InternalTls bool
}
type Drain struct {
Url string `json:"url"`
Credentials Credentials `json:"credentials"`
}
type Credentials struct {
Cert string `json:"cert"`
Key string `json:"key"`
CA string `json:"ca"`
}
// LogClient is used to emit logs.
type LogClient interface {
EmitLog(message string, opts ...loggregator.EmitLogOption)
}
// nullLogClient ensures that the LogClient is in fact optional.
type nullLogClient struct{}
// EmitLog drops all messages into /dev/null.
func (nullLogClient) EmitLog(message string, opts ...loggregator.EmitLogOption) {
}
type writerFactory interface {
NewWriter(*URLBinding) (egress.WriteCloser, error)
}
// SyslogConnector creates the various egress syslog writers.
type SyslogConnector struct {
skipCertVerify bool
logClient LogClient
wg egress.WaitGroup
sourceIndex string
writerFactory writerFactory
metricClient metricClient
droppedMetric metrics.Counter
}
// NewSyslogConnector configures and returns a new SyslogConnector.
func NewSyslogConnector(
skipCertVerify bool,
wg egress.WaitGroup,
f writerFactory,
m metricClient,
opts ...ConnectorOption,
) *SyslogConnector {
droppedMetric := m.NewCounter(
"dropped",
"Total number of dropped envelopes.",
metrics.WithMetricLabels(map[string]string{"direction": "egress"}),
)
sc := &SyslogConnector{
skipCertVerify: skipCertVerify,
wg: wg,
logClient: nullLogClient{},
writerFactory: f,
metricClient: m,
droppedMetric: droppedMetric,
}
for _, o := range opts {
o(sc)
}
return sc
}
// ConnectorOption allows a syslog connector to be customized.
type ConnectorOption func(*SyslogConnector)
// WithLogClient returns a ConnectorOption that will set up logging for any
// information about a binding.
func WithLogClient(logClient LogClient, sourceIndex string) ConnectorOption {
return func(sc *SyslogConnector) {
sc.logClient = logClient
sc.sourceIndex = sourceIndex
}
}
// Connect returns an egress writer based on the scheme of the binding drain
// URL.
func (w *SyslogConnector) Connect(ctx context.Context, b Binding) (egress.Writer, error) {
urlBinding, err := buildBinding(ctx, b)
if err != nil {
return nil, err
}
writer, err := w.writerFactory.NewWriter(urlBinding)
if err != nil {
return nil, err
}
anonymousUrl := *urlBinding.URL
anonymousUrl.User = nil
anonymousUrl.RawQuery = ""
drainScope := "app"
if b.AppId == "" {
drainScope = "aggregate"
}
drainDroppedMetric := w.metricClient.NewCounter(
"messages_dropped_per_drain",
"Total number of dropped messages.",
metrics.WithMetricLabels(map[string]string{
"direction": "egress",
"drain_scope": drainScope,
"drain_url": anonymousUrl.String(),
}),
)
dw := egress.NewDiodeWriter(ctx, writer, diodes.AlertFunc(func(missed int) {
w.droppedMetric.Add(float64(missed))
drainDroppedMetric.Add(float64(missed))
w.emitLoggregatorErrorLog(b.AppId, fmt.Sprintf("%d messages lost for application %s in user provided syslog drain with url %s", missed, b.AppId, anonymousUrl.String()))
w.emitStandardOutErrorLog(b.AppId, urlBinding.Scheme(), anonymousUrl.String(), missed)
}), w.wg)
filteredWriter, err := NewFilteringDrainWriter(b, dw)
if err != nil {
log.Printf("failed to create filtered writer: %s", err)
return nil, err
}
return filteredWriter, nil
}
func (w *SyslogConnector) emitLoggregatorErrorLog(appID, message string) {
if appID == "" {
return
}
option := loggregator.WithAppInfo(appID, "LGR", "")
w.logClient.EmitLog(message, option)
option = loggregator.WithAppInfo(
appID,
"SYS",
w.sourceIndex,
)
w.logClient.EmitLog(message, option)
}
func (w *SyslogConnector) emitStandardOutErrorLog(appID, scheme, url string, missed int) {
errorAppOrAggregate := fmt.Sprintf("for %s's app drain", appID)
if appID == "" {
errorAppOrAggregate = "for aggregate drain"
}
log.Printf(
"Dropped %d %s logs %s with url %s",
missed, scheme, errorAppOrAggregate, url,
)
}