8000 Fix how we handle log tailing when the line is nil (#1283) · nginx/agent@60046f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60046f8

Browse files
authored
Fix how we handle log tailing when the line is nil (#1283)
1 parent 7aa8c9f commit 60046f8

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

internal/datasource/nginx/log_tailer.go

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,73 +111,95 @@ func NewLTSVTailer(file string) (*LTSVTailer, error) {
111111
return &LTSVTailer{t}, nil
112112
}
113113

114+
//nolint:revive // Can't extract anymore functions to reduce complexity
114115
func (t *Tailer) Tail(ctx context.Context, data chan<- string) {
115116
for {
116117
select {
117118
case line := <-t.handle.Lines:
118-
lineContent := t.parseLine(line)
119+
if line == nil {
120+
return
121+
}
122+
123+
if line.Err != nil {
124+
continue
125+
}
126+
127+
lineContent := line.Text
119128
if lineContent != "" {
120129
data <- lineContent
121130
}
122131
case <-ctx.Done():
123132
handleContextDone(ctx)
124133

134+
err := t.handle.Stop()
135+
if err != nil {
136+
slog.ErrorContext(ctx, "Error stopping tailer", "error", err)
137+
}
138+
125139
return
126140
}
127141
}
128142
}
129143

130-
func (t *Tailer) parseLine(line *tail.Line) string {
131-
if line == nil {
132-
return ""
133-
}
134-
135-
if line.Err != nil {
136-
return ""
137-
}
138-
139-
return line.Text
140-
}
141-
144+
//nolint:revive // Can't extract anymore functions to reduce complexity
142145
func (t *PatternTailer) Tail(ctx context.Context, data chan<- map[string]string) {
143146
for {
144147
select {
145148
case line := <-t.handle.Lines:
146-
lineContent := t.parseLine(line)
149+
if line == nil {
150+
return
151+
}
152+
153+
if line.Err != nil {
154+
continue
155+
}
156+
157+
lineContent := t.gc.ParseString(line.Text)
147158
if lineContent != nil {
148159
data <- lineContent
149160
}
150161
case <-ctx.Done():
151162
handleContextDone(ctx)
152163

153-
return
154-
}
155-
}
156-
}
164+
err := t.handle.Stop()
165+
if err != nil {
166+
slog.ErrorContext(ctx, "Error stopping tailer", "error", err)
167+
}
157168

158-
func (t *PatternTailer) parseLine(line *tail.Line) map[string]string {
159-
if line == nil {
160-
return nil
161-
}
169+
slog.DebugContext(ctx, "Tailer is done")
162170

163-
if line.Err != nil {
164-
return nil
171+
return
172+
}
165173
}
166-
167-
return t.gc.ParseString(line.Text)
168174
}
169175

176+
//nolint:revive // Can't extract anymore functions to reduce complexity
170177
func (t *LTSVTailer) Tail(ctx context.Context, data chan<- map[string]string) {
171178
for {
172179
select {
173180
case line := <-t.handle.Lines:
174-
lineText := t.parseLine(line)
181+
if line == nil {
182+
return
183+
}
184+
185+
if line.Err != nil {
186+
continue
187+
}
188+
189+
lineText := t.parse(line.Text)
175190
if lineText != nil {
176191
data <- lineText
177192
}
178193
case <-ctx.Done():
179194
handleContextDone(ctx)
180195

196+
err := t.handle.Stop()
197+
if err != nil {
198+
slog.ErrorContext(ctx, "Error stopping tailer", "error", err)
199+
}
200+
201+
slog.DebugContext(ctx, "Tailer is done")
202+
181203
return
182204
}
183205
}
@@ -199,18 +221,6 @@ func (t *LTSVTailer) parse(line string) map[string]string {
199221
return lineMap
200222
}
201223

202-
func (t *LTSVTailer) parseLine(line *tail.Line) map[string]string {
203-
if line == nil {
204-
return nil
205-
}
206-
207-
if line.Err != nil {
208-
return nil
209-
}
210-
211-
return t.parse(line.Text)
212-
}
213-
214224
func handleContextDone(ctx context.Context) {
215225
ctxErr := ctx.Err()
216226
switch ctxErr {
@@ -219,5 +229,4 @@ func handleContextDone(ctx context.Context) {
219229
case context.Canceled:
220230
slog.DebugContext(ctx, "Tailer forcibly canceled", "error", ctxErr)
221231
}
222-
slog.DebugContext(ctx, "Tailer is done")
223232
}

0 commit comments

Comments
 (0)
0