Skip to content

Commit b42a9f8

Browse files
committed
Remove namedMetric pointer and improve agent log messages for access & error log parsing
1 parent 755ca06 commit b42a9f8

File tree

6 files changed

+80
-56
lines changed

6 files changed

+80
-56
lines changed

src/core/metrics/sources/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ type namedMetric struct {
3434
}
3535

3636
func (n *namedMetric) label(name string) string {
37-
log.Tracef("Creating label for namespace %s, group %s, name %s", n.namespace, n.group, name)
3837
if name == "" {
3938
return ""
4039
}
40+
4141
switch {
4242
case n.namespace != "" && n.group != "":
4343
return strings.Join([]string{n.namespace, n.group, name}, ".")
@@ -46,6 +46,7 @@ func (n *namedMetric) label(name string) string {
4646
case n.group != "":
4747
return strings.Join([]string{n.group, name}, ".")
4848
}
49+
4950
return name
5051
}
5152

src/core/metrics/sources/nginx_access_log.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ var logVarRegex = regexp.MustCompile(`\$([a-zA-Z]+[_[a-zA-Z]+]*)`)
6262
// This metrics source is used to tail the NGINX access logs to retrieve metrics.
6363

6464
type NginxAccessLog struct {
65-
baseDimensions *metrics.CommonDim
66-
*namedMetric
65+
baseDimensions *metrics.CommonDim
6766
mu *sync.Mutex
6867
logFormats map[string]string
6968
logs map[string]context.CancelFunc
7069
binary core.NginxBinary
7170
nginxType string
71+
namespace string
7272
collectionInterval time.Duration
7373
buf []*metrics.StatsEntityWrapper
7474
logger *MetricSourceLogger
@@ -85,12 +85,12 @@ func NewNginxAccessLog(
8585

8686
nginxAccessLog := &NginxAccessLog{
8787
baseDimensions,
88-
&namedMetric{namespace: namespace},
8988
&sync.Mutex{},
9089
make(map[string]string),
9190
make(map[string]context.CancelFunc),
9291
binary,
9392
nginxType,
93+
namespace,
9494
collectionInterval,
9595
[]*metrics.StatsEntityWrapper{},
9696
NewMetricSourceLogger(),
@@ -203,8 +203,8 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
203203

204204
logPattern = replaceCustomLogVars(logPattern)
205205

206-
log.Debugf("Collecting from: %s using format: %s", logFile, logFormat)
207-
log.Debugf("Pattern used for tailing logs: %s", logPattern)
206+
log.Debugf("Collecting from %s using format %s", logFile, logFormat)
207+
log.Debugf("Pattern used for tailing logs, %s", logPattern)
208208

209209
httpCounters, upstreamCounters, upstreamCacheCounters := map[string]float64{}, map[string]float64{}, map[string]float64{}
210210
gzipRatios, requestLengths, requestTimes, upstreamResponseLength, upstreamResponseTimes, upstreamConnectTimes, upstreamHeaderTimes := []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}
@@ -214,19 +214,21 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
214214
if logPattern == "ltsv" {
215215
t, err := tailer.NewLTSVTailer(logFile)
216216
if err != nil {
217-
log.Errorf("unable to tail %q: %v", logFile, err)
217+
log.Errorf("Unable to tail %q: %v", logFile, err)
218218
return
219219
}
220220
go t.Tail(ctx, data)
221221
} else {
222222
t, err := tailer.NewPatternTailer(logFile, map[string]string{"DEFAULT": logPattern})
223223
if err != nil {
224-
log.Errorf("unable to tail %q: %v", logFile, err)
224+
log.Errorf("Unable to tail %q: %v", logFile, err)
225225
return
226226
}
227227
go t.Tail(ctx, data)
228228
}
229229

230+
accessLogNamedMetric := namedMetric{namespace: c.namespace}
231+
230232
tick := time.NewTicker(c.collectionInterval)
231233
defer tick.Stop()
232234
for {
@@ -295,7 +297,7 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
295297
upstreamCounters[n] = 1
296298
}
297299
} else {
298-
log.Debugf("Error getting upstream status value from access logs, %v", err)
300+
log.Debugf("Error getting upstream status value from access log %s, %v", logFile, err)
299301
}
300302
}
301303

@@ -330,6 +332,8 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
330332
case <-tick.C:
331333
mu.Lock()
332334

335+
log.Tracef("Collecting metrics from access log: %s", logFile)
336+
333337
c.baseDimensions.NginxType = c.nginxType
334338
c.baseDimensions.PublishedAPI = logFile
335339

@@ -361,24 +365,23 @@ func (c *NginxAccessLog) logStats(ctx context.Context, logFile, logFormat string
361365
upstreamCounters["upstream.response.length"] = getAverageMetricValue(upstreamResponseLength)
362366
}
363367

364-
log.Tracef("%s log file: Converting httpCounters: %v", logFile, httpCounters)
365-
366-
c.group = "http"
367-
simpleMetrics := c.convertSamplesToSimpleMetrics(httpCounters)
368-
369-
c.group = ""
370-
simpleMetrics = append(simpleMetrics, c.convertSamplesToSimpleMetrics(upstreamCounters)...)
368+
accessLogNamedMetric.group = "http"
369+
log.Tracef("Converting httpCounters for %s access log file, httpCounters=%v", logFile, httpCounters)
370+
simpleMetrics := accessLogNamedMetric.convertSamplesToSimpleMetrics(httpCounters)
371371

372-
c.group = ""
373-
simpleMetrics = append(simpleMetrics, c.convertSamplesToSimpleMetrics(upstreamCacheCounters)...)
372+
accessLogNamedMetric.group = ""
373+
log.Tracef("Converting upstreamCounters for %s access log file, upstreamCounters=%v", logFile, upstreamCounters)
374+
simpleMetrics = append(simpleMetrics, accessLogNamedMetric.convertSamplesToSimpleMetrics(upstreamCounters)...)
375+
log.Tracef("Converting upstreamCacheCounters for %s access log file, upstreamCacheCounters=%v", logFile, upstreamCacheCounters)
376+
simpleMetrics = append(simpleMetrics, accessLogNamedMetric.convertSamplesToSimpleMetrics(upstreamCacheCounters)...)
374377

375-
log.Tracef("Access log metrics collected: %v", simpleMetrics)
378+
log.Tracef("Access log %s metrics collected: %v", logFile, simpleMetrics)
376379

377380
// reset the counters
378381
httpCounters, upstreamCounters, upstreamCacheCounters = map[string]float64{}, map[string]float64{}, map[string]float64{}
379382
gzipRatios, requestLengths, requestTimes, upstreamResponseLength, upstreamResponseTimes, upstreamConnectTimes, upstreamHeaderTimes = []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}, []float64{}
380383

381-
log.Debugf("access log stats count: %d", len(simpleMetrics))
384+
log.Debugf("Access log %s stats count: %d", logFile, len(simpleMetrics))
382385

383386
c.buf = append(c.buf, metrics.NewStatsEntityWrapper(c.baseDimensions.ToDimensions(), simpleMetrics, proto.MetricsReport_INSTANCE))
384387

src/core/metrics/sources/nginx_error_log.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ var regularExpressionErrorMap = map[string][]*re.Regexp{
5757
}
5858

5959
type NginxErrorLog struct {
60-
baseDimensions *metrics.CommonDim
61-
*namedMetric
60+
baseDimensions *metrics.CommonDim
6261
mu *sync.Mutex
6362
logFormats map[string]string
6463
logs map[string]context.CancelFunc
6564
binary core.NginxBinary
6665
nginxType string
66+
namespace string
6767
collectionInterval time.Duration
6868
buf []*metrics.StatsEntityWrapper
6969
}
@@ -78,12 +78,12 @@ func NewNginxErrorLog(
7878
log.Trace("Creating NewNginxErrorLog")
7979
nginxErrorLog := &NginxErrorLog{
8080
baseDimensions,
81-
&namedMetric{namespace: namespace},
8281
&sync.Mutex{},
8382
make(map[string]string),
8483
make(map[string]context.CancelFunc),
8584
binary,
8685
nginxType,
86+
namespace,
8787
collectionInterval,
8888
[]*metrics.StatsEntityWrapper{},
8989
}
@@ -111,7 +111,7 @@ func (c *NginxErrorLog) Stop() {
111111
fn()
112112
delete(c.logs, f)
113113
}
114-
log.Debugf("Stopping NginxErrorLog source for nginx id: %v", c.baseDimensions.NginxId)
114+
log.Debugf("Stopping NginxErrorLog source for NGINX ID: %v", c.baseDimensions.NginxId)
115115
}
116116

117117
func (c *NginxErrorLog) Update(dimensions *metrics.CommonDim, collectorConf *metrics.NginxCollectorConfig) {
@@ -189,14 +189,14 @@ func (c *NginxErrorLog) collectLogStats(_ context.Context, m chan<- *metrics.Sta
189189
}
190190

191191
func (c *NginxErrorLog) logStats(ctx context.Context, logFile string) {
192-
log.Debugf("Collecting from error log: %s", logFile)
192+
log.Debugf("Collecting from error log %s", logFile)
193193

194194
counters := map[string]float64{}
195195
mu := sync.Mutex{}
196196

197197
t, err := tailer.NewTailer(logFile)
198198
if err != nil {
199-
log.Errorf("Unable to tail %q: %v", logFile, err)
199+
log.Errorf("Unable to tail %s, %v", logFile, err)
200200
return
201201
}
202202
data := make(chan string, 1024)
@@ -205,6 +205,8 @@ func (c *NginxErrorLog) logStats(ctx context.Context, logFile string) {
205205
tick := time.NewTicker(c.collectionInterval)
206206
defer tick.Stop()
207207

208+
errorLogNamedMetric := namedMetric{namespace: c.namespace}
209+
208210
for {
209211
select {
210212
case d := <-data:
@@ -228,15 +230,21 @@ func (c *NginxErrorLog) logStats(ctx context.Context, logFile string) {
228230

229231
case <-tick.C:
230232
mu.Lock()
233+
234+
log.Tracef("Collecting metrics from error log: %s", logFile)
235+
231236
c.baseDimensions.NginxType = c.nginxType
232237
c.baseDimensions.PublishedAPI = logFile
233238

234-
simpleMetrics := c.convertSamplesToSimpleMetrics(counters)
235-
log.Tracef("Error log metrics collected: %v", simpleMetrics)
239+
log.Tracef("Converting counters for %s error log file, counters=%v", logFile, counters)
240+
simpleMetrics := errorLogNamedMetric.convertSamplesToSimpleMetrics(counters)
241+
log.Tracef("Error log %s metrics collected: %v", logFile, simpleMetrics)
236242

237243
// reset the counters
238244
counters = map[string]float64{}
239245

246+
log.Debugf("Error log %s stats count: %d", logFile, len(simpleMetrics))
247+
240248
c.buf = append(c.buf, metrics.NewStatsEntityWrapper(c.baseDimensions.ToDimensions(), simpleMetrics, proto.MetricsReport_INSTANCE))
241249

242250
mu.Unlock()

test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/common.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/performance/vendor/github.com/nginx/agent/v2/src/core/metrics/sources/nginx_access_log.go

Lines changed: 22 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)