-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
channels.go
174 lines (146 loc) · 4.49 KB
/
channels.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package beater
import (
"sync"
"github.com/elastic/beats/v7/filebeat/input/file"
"github.com/elastic/beats/v7/filebeat/registrar"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/publisher/pipetool"
"github.com/elastic/elastic-agent-libs/monitoring"
)
type registrarLogger struct {
done chan struct{}
ch chan<- []file.State
}
type finishedLogger struct {
wg *eventCounter
}
type eventCounter struct {
added *monitoring.Uint
done *monitoring.Uint
count *monitoring.Int
wg sync.WaitGroup
}
// countingClient adds and subtracts from a counter when events have been
// published, dropped or ACKed. The countingClient can be used to keep track of
// inflight events for a beat.Client instance. The counter is updated after the
// client has been disconnected from the publisher pipeline via 'Closed'.
type countingClient struct {
counter *eventCounter
client beat.Client
}
type countingClientListener struct {
wgEvents *eventCounter
}
type combinedClientListener struct {
a, b beat.ClientListener
}
func newRegistrarLogger(reg *registrar.Registrar) *registrarLogger {
return ®istrarLogger{
done: make(chan struct{}),
ch: reg.Channel,
}
}
func (l *registrarLogger) Close() { close(l.done) }
func (l *registrarLogger) Published(states []file.State) {
select {
case <-l.done:
// set ch to nil, so no more events will be send after channel close signal
// has been processed the first time.
// Note: nil channels will block, so only done channel will be actively
// report 'closed'.
l.ch = nil
case l.ch <- states:
}
}
func newFinishedLogger(wg *eventCounter) *finishedLogger {
return &finishedLogger{wg}
}
func (l *finishedLogger) Published(n int) bool {
for i := 0; i < n; i++ {
l.wg.Done()
}
return true
}
func (c *eventCounter) Add(delta int) {
c.count.Add(int64(delta))
c.added.Add(uint64(delta))
c.wg.Add(delta)
}
func (c *eventCounter) Done() {
c.count.Dec()
c.done.Inc()
c.wg.Done()
}
func (c *eventCounter) Wait() {
c.wg.Wait()
}
// withPipelineEventCounter adds a counter to the pipeline that keeps track of
// all events published, dropped and ACKed by any active client.
// The type accepted by counter is compatible with sync.WaitGroup.
func withPipelineEventCounter(pipeline beat.PipelineConnector, counter *eventCounter) beat.PipelineConnector {
counterListener := &countingClientListener{counter}
pipeline = pipetool.WithClientConfigEdit(pipeline, func(config beat.ClientConfig) (beat.ClientConfig, error) {
if evts := config.ClientListener; evts != nil {
config.ClientListener = &combinedClientListener{evts, counterListener}
} else {
config.ClientListener = counterListener
}
return config, nil
})
pipeline = pipetool.WithClientWrapper(pipeline, func(client beat.Client) beat.Client {
return &countingClient{
counter: counter,
client: client,
}
})
return pipeline
}
func (c *countingClient) Publish(event beat.Event) {
c.counter.Add(1)
c.client.Publish(event)
}
func (c *countingClient) PublishAll(events []beat.Event) {
c.counter.Add(len(events))
c.client.PublishAll(events)
}
func (c *countingClient) Close() error {
return c.client.Close()
}
func (*countingClientListener) Closing() {}
func (*countingClientListener) Closed() {}
func (*countingClientListener) Published() {}
func (c *countingClientListener) DroppedOnPublish(_ beat.Event) {
c.wgEvents.Done()
}
func (c *combinedClientListener) Closing() {
c.a.Closing()
c.b.Closing()
}
func (c *combinedClientListener) Closed() {
c.a.Closed()
c.b.Closed()
}
func (c *combinedClientListener) Published() {
c.a.Published()
c.b.Published()
}
func (c *combinedClientListener) DroppedOnPublish(event beat.Event) {
c.a.DroppedOnPublish(event)
c.b.DroppedOnPublish(event)
}