This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
/
syslog_agent.go
148 lines (130 loc) · 3.82 KB
/
syslog_agent.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
package app
import (
"code.cloudfoundry.org/loggregator-agent/pkg/metrics"
"fmt"
"log"
"time"
"net/http"
_ "net/http/pprof"
gendiodes "code.cloudfoundry.org/go-diodes"
"code.cloudfoundry.org/loggregator-agent/pkg/binding"
"code.cloudfoundry.org/loggregator-agent/pkg/cache"
"code.cloudfoundry.org/loggregator-agent/pkg/diodes"
"code.cloudfoundry.org/loggregator-agent/pkg/egress"
"code.cloudfoundry.org/loggregator-agent/pkg/egress/syslog"
"code.cloudfoundry.org/loggregator-agent/pkg/ingress/cups"
"code.cloudfoundry.org/loggregator-agent/pkg/ingress/v2"
"code.cloudfoundry.org/loggregator-agent/pkg/plumbing"
"code.cloudfoundry.org/loggregator-agent/pkg/timeoutwaitgroup"
"google.golang.org/grpc"
)
// SyslogAgent manages starting the syslog agent service.
type SyslogAgent struct {
pprofPort uint16
metrics Metrics
bindingManager BindingManager
grpc GRPC
log *log.Logger
cache Cache
bindingsPerAppLimit int
drainSkipCertVerify bool
}
type Metrics interface {
NewGauge(name string, options ...metrics.MetricOption) metrics.Gauge
NewCounter(name string, options ...metrics.MetricOption) metrics.Counter
}
type BindingManager interface {
Run()
GetDrains(string) []egress.Writer
}
// NewSyslogAgent intializes and returns a new syslog agent.
func NewSyslogAgent(
cfg Config,
m Metrics,
l *log.Logger,
) *SyslogAgent {
connector := syslog.NewSyslogConnector(
syslog.NetworkTimeoutConfig{
Keepalive: 10 * time.Second,
DialTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
},
cfg.DrainSkipCertVerify,
timeoutwaitgroup.New(time.Minute),
syslog.NewWriterFactory(m),
m,
)
tlsClient := plumbing.NewTLSHTTPClient(
cfg.Cache.CertFile,
cfg.Cache.KeyFile,
cfg.Cache.CAFile,
cfg.Cache.CommonName,
)
cacheClient := cache.NewClient(cfg.Cache.URL, tlsClient)
fetcher := cups.NewFilteredBindingFetcher(
&cfg.Cache.Blacklist,
cups.NewBindingFetcher(cfg.BindingsPerAppLimit, cacheClient, m),
m,
l,
)
bindingManager := binding.NewManager(
fetcher,
connector,
m,
cfg.Cache.PollingInterval,
cfg.IdleDrainTimeout,
l,
)
return &SyslogAgent{
pprofPort: cfg.DebugPort,
grpc: cfg.GRPC,
metrics: m,
log: l,
bindingsPerAppLimit: cfg.BindingsPerAppLimit,
drainSkipCertVerify: cfg.DrainSkipCertVerify,
bindingManager: bindingManager,
}
}
func (s *SyslogAgent) Run() {
go http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", s.pprofPort), nil)
ingressDropped := s.metrics.NewCounter("dropped", metrics.WithMetricTags(map[string]string{"direction": "ingress"}))
diode := diodes.NewManyToOneEnvelopeV2(10000, gendiodes.AlertFunc(func(missed int) {
ingressDropped.Add(float64(missed))
}))
drainIngress := s.metrics.NewCounter("ingress", metrics.WithMetricTags(map[string]string{"scope": "all_drains"}))
go s.bindingManager.Run()
go func() {
for {
e := diode.Next()
drainWriters := s.bindingManager.GetDrains(e.SourceId)
for _, w := range drainWriters {
drainIngress.Add(1)
// Ignore this because we typically wrap everything in a diode
// writer which doesn't return an error
_ = w.Write(e)
}
}
}()
var opts []plumbing.ConfigOption
if len(s.grpc.CipherSuites) > 0 {
opts = append(opts, plumbing.WithCipherSuites(s.grpc.CipherSuites))
}
serverCreds, err := plumbing.NewServerCredentials(
s.grpc.CertFile,
s.grpc.KeyFile,
s.grpc.CAFile,
opts...,
)
if err != nil {
s.log.Fatalf("failed to configure server TLS: %s", err)
}
im := s.metrics.NewCounter("ingress", metrics.WithMetricTags(map[string]string{"scope": "agent"}))
omm := s.metrics.NewCounter("origin_mappings")
rx := v2.NewReceiver(diode, im, omm)
srv := v2.NewServer(
fmt.Sprintf("127.0.0.1:%d", s.grpc.Port),
rx,
grpc.Creds(serverCreds),
)
srv.Start()
}