-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathnginx_log_scraper.go
More file actions
278 lines (233 loc) · 7.47 KB
/
nginx_log_scraper.go
File metadata and controls
278 lines (233 loc) · 7.47 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package accesslog
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/pipeline"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension/xextension/storage"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/otel"
metricSdk "go.opentelemetry.io/otel/sdk/metric"
"go.uber.org/zap"
"github.com/nginx/agent/v3/internal/collector/nginxossreceiver/internal/config"
"github.com/nginx/agent/v3/internal/collector/nginxossreceiver/internal/metadata"
"github.com/nginx/agent/v3/internal/collector/nginxossreceiver/internal/model"
"github.com/nginx/agent/v3/internal/collector/nginxossreceiver/internal/scraper/accesslog/operator/input/file"
)
const Percentage = 100
type (
NginxLogScraper struct {
outChan <-chan []*entry.Entry
cfg *config.Config
settings receiver.Settings
logger *zap.Logger
mb *metadata.MetricsBuilder
rb *metadata.ResourceBuilder
pipes []*pipeline.DirectedPipeline
wg *sync.WaitGroup
cancel context.CancelFunc
entries []*entry.Entry
operators []operator.Config
mut sync.Mutex
}
NginxMetrics struct {
responseStatuses ResponseStatuses
}
ResponseStatuses struct {
oneHundredStatusRange int64
twoHundredStatusRange int64
threeHundredStatusRange int64
fourHundredStatusRange int64
fiveHundredStatusRange int64
}
)
func NewScraper(
settings receiver.Settings,
cfg *config.Config,
) *NginxLogScraper {
logger := settings.Logger
logger.Info("Creating NGINX access log scraper")
mb := metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings)
rb := mb.NewResourceBuilder()
operators := make([]operator.Config, 0)
for _, accessLog := range cfg.AccessLogs {
logger.Info("Adding access log file operator", zap.String("file_path", accessLog.FilePath))
fileInputConfig := file.NewConfig()
fileInputConfig.AccessLogFormat = accessLog.LogFormat
fileInputConfig.Include = append(fileInputConfig.Include, accessLog.FilePath)
inputCfg := operator.NewConfig(fileInputConfig)
operators = append(operators, inputCfg)
}
nls := &NginxLogScraper{
cfg: cfg,
logger: logger,
settings: settings,
mb: mb,
rb: rb,
mut: sync.Mutex{},
wg: &sync.WaitGroup{},
operators: operators,
}
return nls
}
func (nls *NginxLogScraper) ID() component.ID {
return component.NewID(metadata.Type)
}
// nolint: unparam
func (nls *NginxLogScraper) Start(parentCtx context.Context, _ component.Host) error {
nls.logger.Info("NGINX access log scraper started")
ctx, cancel := context.WithCancel(parentCtx)
nls.cancel = cancel
for _, op := range nls.operators {
nls.logger.Info("Initializing NGINX access log scraper pipeline", zap.Any("operator_id", op.ID()))
pipe, err := nls.initStanzaPipeline([]operator.Config{op}, nls.logger)
if err != nil {
nls.logger.Error("Error initializing pipeline", zap.Any("operator_id", op.ID()), zap.Any("error", err))
continue
}
nls.pipes = append(nls.pipes, pipe)
}
for _, pipe := range nls.pipes {
startError := pipe.Start(storage.NewNopClient())
if startError != nil {
nls.logger.Error("Error starting pipeline", zap.Any("error", startError))
}
}
nls.wg.Add(1)
go nls.runConsumer(ctx)
return nil
}
func (nls *NginxLogScraper) Scrape(_ context.Context) (pmetric.Metrics, error) {
nls.mut.Lock()
defer nls.mut.Unlock()
nginxMetrics := NginxMetrics{}
for _, ent := range nls.entries {
nls.logger.Debug("Scraping NGINX access log", zap.Any("entity", ent))
item, ok := ent.Body.(*model.NginxAccessItem)
if !ok {
nls.logger.Warn("Failed to cast log entry to *model.NginxAccessItem", zap.Any("entry", ent.Body))
continue
}
if v, err := strconv.Atoi(item.Status); err == nil {
codeRange := fmt.Sprintf("%dxx", v/Percentage)
switch codeRange {
case "1xx":
nginxMetrics.responseStatuses.oneHundredStatusRange++
case "2xx":
nginxMetrics.responseStatuses.twoHundredStatusRange++
case "3xx":
nginxMetrics.responseStatuses.threeHundredStatusRange++
case "4xx":
nginxMetrics.responseStatuses.fourHundredStatusRange++
case "5xx":
nginxMetrics.responseStatuses.fiveHundredStatusRange++
default:
nls.logger.Error("Unknown status range", zap.String("codeRange", codeRange))
continue
}
}
}
nls.entries = make([]*entry.Entry, 0)
timeNow := pcommon.NewTimestampFromTime(time.Now())
nls.rb.SetInstanceID(nls.settings.ID.Name())
nls.rb.SetInstanceType("nginx")
nls.logger.Debug("NGINX OSS access log resource info", zap.Any("resource", nls.rb))
nls.mb.RecordNginxHTTPResponseCountDataPoint(
timeNow,
nginxMetrics.responseStatuses.oneHundredStatusRange,
metadata.AttributeNginxStatusRange1xx,
)
nls.mb.RecordNginxHTTPResponseCountDataPoint(
timeNow,
nginxMetrics.responseStatuses.twoHundredStatusRange,
metadata.AttributeNginxStatusRange2xx,
)
nls.mb.RecordNginxHTTPResponseCountDataPoint(
timeNow,
nginxMetrics.responseStatuses.threeHundredStatusRange,
metadata.AttributeNginxStatusRange3xx,
)
nls.mb.RecordNginxHTTPResponseCountDataPoint(
timeNow,
nginxMetrics.responseStatuses.fourHundredStatusRange,
metadata.AttributeNginxStatusRange4xx,
)
nls.mb.RecordNginxHTTPResponseCountDataPoint(
timeNow,
nginxMetrics.responseStatuses.fiveHundredStatusRange,
metadata.AttributeNginxStatusRange5xx,
)
return nls.mb.Emit(metadata.WithResource(nls.rb.Emit())), nil
}
func (nls *NginxLogScraper) Shutdown(_ context.Context) error {
nls.logger.Info("Shutting down NGINX access log scraper")
if nls.cancel != nil {
nls.cancel()
}
nls.wg.Wait()
var err error
for _, pipe := range nls.pipes {
if stopErr := pipe.Stop(); stopErr != nil {
err = errors.Join(err, stopErr)
}
}
return err
}
func (nls *NginxLogScraper) ConsumerCallback(_ context.Context, entries []*entry.Entry) {
nls.mut.Lock()
nls.entries = append(nls.entries, entries...)
nls.mut.Unlock()
}
func (nls *NginxLogScraper) initStanzaPipeline(
operators []operator.Config,
logger *zap.Logger,
) (*pipeline.DirectedPipeline, error) {
mp := otel.GetMeterProvider()
if mp == nil {
mp = metricSdk.NewMeterProvider()
otel.SetMeterProvider(mp)
}
settings := component.TelemetrySettings{
Logger: logger,
MeterProvider: mp,
}
emitter := helper.NewSynchronousLogEmitter(settings, nls.ConsumerCallback)
pipe, err := pipeline.Config{
Operators: operators,
DefaultOutput: emitter,
}.Build(settings)
return pipe, err
}
func (nls *NginxLogScraper) runConsumer(ctx context.Context) {
nls.logger.Info("Starting NGINX access log receiver's consumer")
defer nls.wg.Done()
entryChan := nls.outChan
for {
select {
case <-ctx.Done():
nls.logger.Info("Closing NGINX access log receiver consumer")
return
case entries, ok := <-entryChan:
if !ok {
nls.logger.Info("Emitter channel closed, shutting down NGINX access log consumer")
return
}
nls.mut.Lock()
nls.entries = append(nls.entries, entries...)
nls.mut.Unlock()
}
}
}