Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nginx metrics and tests #3420

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions receiver/nginxreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nginxreceiver/internal/metadata"
)

type NginxIntegrationSuite struct {
Expand Down Expand Up @@ -92,24 +93,46 @@ func (suite *NginxIntegrationSuite) TestNginxScraperHappyPath() {
ilm := ilms.At(0)
ms := ilm.Metrics()

require.Equal(t, 7, ms.Len())

metricValues := make(map[string]int64, 7)
require.Equal(t, 4, ms.Len())

for i := 0; i < ms.Len(); i++ {
m := ms.At(i)

var dps pdata.IntDataPointSlice

switch m.DataType() {
case pdata.MetricDataTypeIntGauge:
dps = m.IntGauge().DataPoints()
case pdata.MetricDataTypeIntSum:
dps = m.IntSum().DataPoints()
switch m.Name() {
case metadata.M.NginxRequests.Name():
require.Equal(t, 1, m.IntSum().DataPoints().Len())
require.True(t, m.IntSum().IsMonotonic())
case metadata.M.NginxConnectionsAccepted.Name():
require.Equal(t, 1, m.IntSum().DataPoints().Len())
require.True(t, m.IntSum().IsMonotonic())
case metadata.M.NginxConnectionsHandled.Name():
require.Equal(t, 1, m.IntSum().DataPoints().Len())
require.True(t, m.IntSum().IsMonotonic())
case metadata.M.NginxConnectionsCurrent.Name():
dps := m.IntGauge().DataPoints()
require.Equal(t, 4, dps.Len())
present := map[string]bool{}
for j := 0; j < dps.Len(); j++ {
dp := dps.At(j)
state, _ := dp.LabelsMap().Get("state")
switch state {
case "active":
present[state] = true
case "reading":
present[state] = true
case "writing":
present[state] = true
case "waiting":
present[state] = true
default:
require.Nil(t, state, fmt.Sprintf("connections with state %s not expected", state))
}
}
// Ensure all 4 expected states were present
require.Equal(t, 4, len(present))
default:
require.Nil(t, m.Name(), fmt.Sprintf("metric %s not expected", m.Name()))
}

require.Equal(t, 1, dps.Len())

metricValues[m.Name()] = dps.At(0).Value()
}
}
59 changes: 12 additions & 47 deletions receiver/nginxreceiver/internal/metadata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 5 additions & 21 deletions receiver/nginxreceiver/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: nginxreceiver

labels:
state:
description: The state of a connection

djaglowski marked this conversation as resolved.
Show resolved Hide resolved
metrics:
nginx.requests:
Expand All @@ -11,12 +13,6 @@ metrics:
monotonic: true
aggregation: cumulative
labels: []
nginx.connections_active:
description: The current number of open connections
unit: connections
data:
type: int gauge
labels: []
nginx.connections_accepted:
description: The total number of accepted client connections
unit: connections
Expand All @@ -33,21 +29,9 @@ metrics:
monotonic: true
aggregation: cumulative
labels: []
nginx.connections_reading:
description: The current number of connections where nginx is reading the request headerhe current number of open connections
nginx.connections_current:
description: The current number of nginx connections by state
unit: connections
data:
type: int gauge
labels: []
nginx.connections_writing:
description: The current number of connections where nginx is writing the response back to the client.
unit: connections
data:
type: int gauge
labels: []
nginx.connections_waiting:
description: The current number of idle client connections waiting for a request.
unit: connections
data:
type: int gauge
labels: []
labels: [state]
9 changes: 5 additions & 4 deletions receiver/nginxreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ func (r *nginxScraper) scrape(ctx context.Context) (pdata.ResourceMetricsSlice,
}

metrics.AddSumDataPoint(metadata.M.NginxRequests.Name(), stats.Requests)
metrics.AddGaugeDataPoint(metadata.M.NginxConnectionsActive.Name(), stats.Connections.Active)
metrics.AddSumDataPoint(metadata.M.NginxConnectionsAccepted.Name(), stats.Connections.Accepted)
metrics.AddSumDataPoint(metadata.M.NginxConnectionsHandled.Name(), stats.Connections.Handled)
metrics.AddGaugeDataPoint(metadata.M.NginxConnectionsReading.Name(), stats.Connections.Reading)
metrics.AddGaugeDataPoint(metadata.M.NginxConnectionsWriting.Name(), stats.Connections.Writing)
metrics.AddGaugeDataPoint(metadata.M.NginxConnectionsWaiting.Name(), stats.Connections.Waiting)

metrics.WithLabels(map[string]string{metadata.L.State: "active"}).AddGaugeDataPoint(metadata.M.NginxConnectionsCurrent.Name(), stats.Connections.Active)
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
metrics.WithLabels(map[string]string{metadata.L.State: "reading"}).AddGaugeDataPoint(metadata.M.NginxConnectionsCurrent.Name(), stats.Connections.Reading)
metrics.WithLabels(map[string]string{metadata.L.State: "writing"}).AddGaugeDataPoint(metadata.M.NginxConnectionsCurrent.Name(), stats.Connections.Writing)
metrics.WithLabels(map[string]string{metadata.L.State: "waiting"}).AddGaugeDataPoint(metadata.M.NginxConnectionsCurrent.Name(), stats.Connections.Waiting)

return metrics.Metrics.ResourceMetrics(), nil
}
32 changes: 20 additions & 12 deletions receiver/nginxreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package nginxreceiver
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -25,6 +26,8 @@ import (
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer/pdata"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nginxreceiver/internal/metadata"
)

func TestScraper(t *testing.T) {
Expand Down Expand Up @@ -57,7 +60,7 @@ Reading: 6 Writing: 179 Waiting: 106
ilm := ilms.At(0)
ms := ilm.Metrics()

require.Equal(t, 7, ms.Len())
require.Equal(t, 4, ms.Len())

metricValues := make(map[string]int64, 7)

Expand All @@ -69,23 +72,28 @@ Reading: 6 Writing: 179 Waiting: 106
switch m.DataType() {
case pdata.MetricDataTypeIntGauge:
dps = m.IntGauge().DataPoints()
require.Equal(t, 4, dps.Len())
for j := 0; j < dps.Len(); j++ {
dp := dps.At(j)
state, _ := dp.LabelsMap().Get(metadata.L.State)
label := fmt.Sprintf("%s state:%s", m.Name(), state)
metricValues[label] = dp.Value()
}
case pdata.MetricDataTypeIntSum:
dps = m.IntSum().DataPoints()
require.Equal(t, 1, dps.Len())
metricValues[m.Name()] = dps.At(0).Value()
}

require.Equal(t, 1, dps.Len())

metricValues[m.Name()] = dps.At(0).Value()
}

require.Equal(t, map[string]int64{
"nginx.connections_accepted": 16630948,
"nginx.connections_handled": 16630948,
"nginx.requests": 31070465,
"nginx.connections_active": 291,
"nginx.connections_reading": 6,
"nginx.connections_writing": 179,
"nginx.connections_waiting": 106,
"nginx.connections_accepted": 16630948,
"nginx.connections_handled": 16630948,
"nginx.requests": 31070465,
"nginx.connections_current state:active": 291,
"nginx.connections_current state:reading": 6,
"nginx.connections_current state:writing": 179,
"nginx.connections_current state:waiting": 106,
}, metricValues)
}

Expand Down