Skip to content

Commit a034d5b

Browse files
authored
Add attributes to nginx.request.count (#1263)
1 parent 2d6e23b commit a034d5b

File tree

8 files changed

+341
-191
lines changed

8 files changed

+341
-191
lines changed

internal/collector/nginxplusreceiver/documentation.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ The total number of client requests received, since the last collection interval
144144
| ---- | ----------- | ---------- |
145145
| requests | Gauge | Int |
146146
147+
#### Attributes
148+
149+
| Name | Description | Values |
150+
| ---- | ----------- | ------ |
151+
| nginx.zone.name | The name of the shared memory zone. | Any Str |
152+
| nginx.zone.type | The type of shared memory zone, depending on what block it was defined in the NGINX configuration. | Str: ``SERVER``, ``LOCATION`` |
153+
147154
### nginx.http.request.discarded
148155
149156
The total number of requests completed without sending a response.

internal/collector/nginxplusreceiver/internal/metadata/generated_metrics.go

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

internal/collector/nginxplusreceiver/internal/metadata/generated_metrics_test.go

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

internal/collector/nginxplusreceiver/metadata.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ metrics:
173173
gauge:
174174
value_type: int
175175
unit: "requests"
176+
attributes:
177+
- nginx.zone.name
178+
- nginx.zone.type
176179
nginx.cache.bytes_read:
177180
enabled: true
178181
description: The total number of bytes read from the cache or proxied server.

internal/collector/nginxplusreceiver/scraper.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
type NginxPlusScraper struct {
4343
previousServerZoneResponses map[string]ResponseStatuses
4444
previousLocationZoneResponses map[string]ResponseStatuses
45+
previousServerZoneRequests map[string]int64
46+
previousLocationZoneRequests map[string]int64
4547
plusClient *plusapi.NginxClient
4648
cfg *Config
4749
mb *metadata.MetricsBuilder
@@ -137,6 +139,8 @@ func (nps *NginxPlusScraper) Scrape(ctx context.Context) (pmetric.Metrics, error
137139
nps.previousHTTPRequestsTotal = stats.HTTPRequests.Total
138140
nps.createPreviousServerZoneResponses(stats)
139141
nps.createPreviousLocationZoneResponses(stats)
142+
nps.createPreviousServerZoneRequests(stats)
143+
nps.createPreviousLocationZoneRequests(stats)
140144
})
141145

142146
stats, err := nps.plusClient.GetStats(ctx)
@@ -175,6 +179,22 @@ func (nps *NginxPlusScraper) createPreviousLocationZoneResponses(stats *plusapi.
175179
nps.previousLocationZoneResponses = previousLocationZoneResponses
176180
}
177181

182+
func (nps *NginxPlusScraper) createPreviousServerZoneRequests(stats *plusapi.Stats) {
183+
previousServerZoneRequests := make(map[string]int64)
184+
for szName, sz := range stats.ServerZones {
185+
previousServerZoneRequests[szName] = int64(sz.Requests)
186+
}
187+
nps.previousServerZoneRequests = previousServerZoneRequests
188+
}
189+
190+
func (nps *NginxPlusScraper) createPreviousLocationZoneRequests(stats *plusapi.Stats) {
191+
previousLocationZoneRequests := make(map[string]int64)
192+
for lzName, lz := range stats.LocationZones {
193+
previousLocationZoneRequests[lzName] = lz.Requests
194+
}
195+
nps.previousLocationZoneRequests = previousLocationZoneRequests
196+
}
197+
178198
func (nps *NginxPlusScraper) createPreviousServerZoneResponses(stats *plusapi.Stats) {
179199
previousServerZoneResponses := make(map[string]ResponseStatuses)
180200
for szName, sz := range stats.ServerZones {
@@ -224,7 +244,7 @@ func (nps *NginxPlusScraper) recordMetrics(stats *plusapi.Stats) {
224244
nps.mb.RecordNginxHTTPRequestsDataPoint(now, int64(stats.HTTPRequests.Total), "", 0)
225245

226246
requestsDiff := int64(stats.HTTPRequests.Total) - int64(nps.previousHTTPRequestsTotal)
227-
nps.mb.RecordNginxHTTPRequestCountDataPoint(now, requestsDiff)
247+
nps.mb.RecordNginxHTTPRequestCountDataPoint(now, requestsDiff, "", 0)
228248
nps.previousHTTPRequestsTotal = stats.HTTPRequests.Total
229249

230250
nps.recordCacheMetrics(stats, now)
@@ -866,6 +886,13 @@ func (nps *NginxPlusScraper) recordServerZoneMetrics(stats *plusapi.Stats, now p
866886

867887
nps.mb.RecordNginxHTTPRequestsDataPoint(now, int64(sz.Requests), szName, metadata.AttributeNginxZoneTypeSERVER)
868888

889+
nps.mb.RecordNginxHTTPRequestCountDataPoint(now,
890+
int64(sz.Requests)-nps.previousServerZoneRequests[szName],
891+
szName,
892+
metadata.AttributeNginxZoneTypeSERVER,
893+
)
894+
nps.previousServerZoneRequests[szName] = int64(sz.Requests)
895+
869896
nps.recordServerZoneHTTPMetrics(sz, szName, now)
870897

871898
nps.mb.RecordNginxHTTPRequestDiscardedDataPoint(now, int64(sz.Discarded),
@@ -975,6 +1002,14 @@ func (nps *NginxPlusScraper) recordLocationZoneMetrics(stats *plusapi.Stats, now
9751002
metadata.AttributeNginxZoneTypeLOCATION,
9761003
)
9771004

1005+
nps.mb.RecordNginxHTTPRequestCountDataPoint(now,
1006+
lz.Requests-nps.previousLocationZoneRequests[lzName],
1007+
lzName,
1008+
metadata.AttributeNginxZoneTypeLOCATION,
1009+
)
1010+
1011+
nps.previousLocationZoneRequests[lzName] = lz.Requests
1012+
9781013
nps.recordLocationZoneHTTPMetrics(lz, lzName, now)
9791014

9801015
nps.mb.RecordNginxHTTPRequestDiscardedDataPoint(now, lz.Discarded,

internal/collector/nginxplusreceiver/scraper_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ func TestScraper(t *testing.T) {
5858
},
5959
}
6060

61+
scraper.previousLocationZoneRequests = map[string]int64{
62+
"location_test": 30, // 5
63+
}
64+
65+
scraper.previousServerZoneRequests = map[string]int64{
66+
"test": 29, // 3
67+
}
68+
6169
scraper.previousHTTPRequestsTotal = 3
6270

6371
actualMetrics, err := scraper.Scrape(context.Background())

internal/collector/nginxplusreceiver/testdata/expected.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,31 @@ resourceMetrics:
297297
dataPoints:
298298
- asInt: "44"
299299
timeUnixNano: "1000000"
300+
attributes:
301+
- key: nginx.zone.name
302+
value:
303+
stringValue: ""
304+
- key: nginx.zone.type
305+
value:
306+
stringValue: ""
307+
- asInt: "3"
308+
attributes:
309+
- key: nginx.zone.name
310+
value:
311+
stringValue: test
312+
- key: nginx.zone.type
313+
value:
314+
stringValue: SERVER
315+
timeUnixNano: "1000000"
316+
- asInt: "4"
317+
attributes:
318+
- key: nginx.zone.name
319+
value:
320+
stringValue: location_test
321+
- key: nginx.zone.type
322+
value:
323+
stringValue: LOCATION
324+
timeUnixNano: "1000000"
300325
isMonotonic: true
301326
unit: requests
302327
- description: The total number of bytes read from the cache or proxied server.

0 commit comments

Comments
 (0)