Skip to content

Commit

Permalink
feat: fixed tests and rebased with nginx/agent main
Browse files Browse the repository at this point in the history
  • Loading branch information
karlsassenberg committed Feb 23, 2023
1 parent 6c04ae3 commit 099413b
Show file tree
Hide file tree
Showing 33 changed files with 347 additions and 679 deletions.
46 changes: 12 additions & 34 deletions docs/proto/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,8 @@ Dimension defines a dimensional attribute used when classifying and categorizing

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| name | [string](#string) | | Dimension name |
| value | [string](#string) | | Dimension value |
| name | [string](#string) | | |
| value | [string](#string) | | |



Expand All @@ -1106,14 +1106,14 @@ Dimension defines a dimensional attribute used when classifying and categorizing
<a name="f5-nginx-agent-sdk-MetricsReport"></a>

### MetricsReport
Represents a metric report



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| meta | [Metadata](#f5-nginx-agent-sdk-Metadata) | | Provides meta information about the metrics |
| type | [MetricsReport.Type](#f5-nginx-agent-sdk-MetricsReport-Type) | | Type of metrics |
| data | [StatsEntity](#f5-nginx-agent-sdk-StatsEntity) | repeated | List of stats entities |
| meta | [Metadata](#f5-nginx-agent-sdk-Metadata) | | |
| type | [MetricsReport.Type](#f5-nginx-agent-sdk-MetricsReport-Type) | | |
| data | [StatsEntity](#f5-nginx-agent-sdk-StatsEntity) | repeated | |



Expand All @@ -1128,8 +1128,8 @@ Represents a metric report

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| name | [string](#string) | | Metric name |
| value | [double](#double) | | Metric value |
| name | [string](#string) | | |
| value | [double](#double) | | |



Expand All @@ -1139,22 +1139,14 @@ Represents a metric report
<a name="f5-nginx-agent-sdk-StatsEntity"></a>

### StatsEntity
<<<<<<< HEAD
<<<<<<< HEAD
StatsEntity defines a stats entity which is a timestamped entry for dimensions and metrics
=======
StatsEntity a timestamped entry for Dimensions and Metrics
>>>>>>> Create dedicated cache and upstream metrics reports
=======
StatsEntity defines a stats entity which is a timestamped entry for dimensions and metrics
>>>>>>> adds updated generated files from protobuf


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| timestamp | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | Timestamp defines the time of stats entity creation |
| dimensions | [Dimension](#f5-nginx-agent-sdk-Dimension) | repeated | List of dimensions |
| simplemetrics | [SimpleMetric](#f5-nginx-agent-sdk-SimpleMetric) | repeated | List of metrics |
| timestamp | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
| dimensions | [Dimension](#f5-nginx-agent-sdk-Dimension) | repeated | |
| simplemetrics | [SimpleMetric](#f5-nginx-agent-sdk-SimpleMetric) | repeated | |



Expand All @@ -1166,29 +1158,15 @@ StatsEntity defines a stats entity which is a timestamped entry for dimensions a
<a name="f5-nginx-agent-sdk-MetricsReport-Type"></a>

### MetricsReport.Type
Metric type enum


| Name | Number | Description |
| ---- | ------ | ----------- |
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> adds updated generated files from protobuf
| SYSTEM | 0 | System metric type |
| INSTANCE | 1 | NGINX instance metric type |
| AGENT | 2 | Agent metric type |
| CACHE_ZONE | 3 | Cache zone metric type |
| UPSTREAMS | 4 | Upstreams metric type |
<<<<<<< HEAD
=======
| SYSTEM | 0 | |
| INSTANCE | 1 | |
| AGENT | 2 | |
| CACHE_ZONE | 3 | |
| UPSTREAMS | 4 | |
>>>>>>> Create dedicated cache and upstream metrics reports
=======
>>>>>>> adds updated generated files from protobuf



Expand Down
70 changes: 14 additions & 56 deletions sdk/proto/metrics.pb.go

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

8 changes: 4 additions & 4 deletions src/core/metrics/collectors/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ func TestContainerCollector_Collect(t *testing.T) {
mockSource1,
mockSource2,
},
buf: make(chan *proto.StatsEntity),
buf: make(chan *metrics.StatsEntityWrapper),
dim: &metrics.CommonDim{},
}

ctx := context.TODO()
wg := &sync.WaitGroup{}
wg.Add(1)

channel := make(chan *proto.StatsEntity)
channel := make(chan *metrics.StatsEntityWrapper)
go containerCollector.Collect(ctx, wg, channel)

containerCollector.buf <- &proto.StatsEntity{Dimensions: []*proto.Dimension{{Name: "new_dim", Value: "123"}}}
containerCollector.buf <- &metrics.StatsEntityWrapper{Type: proto.MetricsReport_SYSTEM, Data: &proto.StatsEntity{Dimensions: []*proto.Dimension{{Name: "new_dim", Value: "123"}}}}
actual := <-channel

mockSource1.AssertExpectations(t)
Expand All @@ -83,7 +83,7 @@ func TestContainerCollector_Collect(t *testing.T) {
{Name: "nginx_id", Value: ""},
{Name: "new_dim", Value: "123"},
}
assert.Equal(t, expectedDimensions, actual.Dimensions)
assert.Equal(t, expectedDimensions, actual.Data.Dimensions)
}

func TestContainerCollector_UpdateConfig(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/metrics/collectors/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestNginxCollector_Collect(t *testing.T) {
ctx := context.TODO()
wg := &sync.WaitGroup{}
wg.Add(1)
go nginxCollector.Collect(ctx, wg, make(chan<- *proto.StatsEntity))
go nginxCollector.Collect(ctx, wg, make(chan<- *metrics.StatsEntityWrapper))

time.Sleep(10 * time.Millisecond)
mockNginxSource1.AssertExpectations(t)
Expand Down
5 changes: 2 additions & 3 deletions src/core/metrics/collectors/source_mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"context"
"sync"

"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core/metrics"
"github.com/stretchr/testify/mock"
)
Expand All @@ -26,7 +25,7 @@ type NginxSourceMock struct {
mock.Mock
}

func (m *NginxSourceMock) Collect(ctx context.Context, wg *sync.WaitGroup, statsChannel chan<- *proto.StatsEntity) {
func (m *NginxSourceMock) Collect(ctx context.Context, wg *sync.WaitGroup, statsChannel chan<- *metrics.StatsEntityWrapper) {
m.Called(ctx, wg, statsChannel)
wg.Done()
}
Expand All @@ -43,7 +42,7 @@ type SourceMock struct {
mock.Mock
}

func (m *SourceMock) Collect(ctx context.Context, wg *sync.WaitGroup, statsChannel chan<- *proto.StatsEntity) {
func (m *SourceMock) Collect(ctx context.Context, wg *sync.WaitGroup, statsChannel chan<- *metrics.StatsEntityWrapper) {
m.Called(ctx, wg, statsChannel)
wg.Done()
}
8 changes: 4 additions & 4 deletions src/core/metrics/collectors/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ func TestSystemCollector_Collect(t *testing.T) {
mockSource1,
mockSource2,
},
buf: make(chan *proto.StatsEntity),
buf: make(chan *metrics.StatsEntityWrapper),
dim: &metrics.CommonDim{},
}

ctx := context.TODO()
wg := &sync.WaitGroup{}
wg.Add(1)

channel := make(chan *proto.StatsEntity)
channel := make(chan *metrics.StatsEntityWrapper)
go systemCollector.Collect(ctx, wg, channel)

systemCollector.buf <- &proto.StatsEntity{Dimensions: []*proto.Dimension{{Name: "new_dim", Value: "123"}}}
systemCollector.buf <- &metrics.StatsEntityWrapper{Type: proto.MetricsReport_SYSTEM, Data: &proto.StatsEntity{Dimensions: []*proto.Dimension{{Name: "new_dim", Value: "123"}}}}
actual := <-channel

mockSource1.AssertExpectations(t)
Expand All @@ -115,7 +115,7 @@ func TestSystemCollector_Collect(t *testing.T) {
{Name: "nginx_id", Value: ""},
{Name: "new_dim", Value: "123"},
}
assert.Equal(t, expectedDimensions, actual.Dimensions)
assert.Equal(t, expectedDimensions, actual.Data.Dimensions)
}

func TestSystemCollector_UpdateConfig(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions src/core/metrics/sources/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sources

import (
"context"
"github.com/nginx/agent/v2/src/core/metrics"
"sort"
"testing"

Expand Down Expand Up @@ -180,10 +181,10 @@ func TestCommonDelta(t *testing.T) {
}

func TestCommonSendNginxDownStatus(t *testing.T) {
m := make(chan *proto.StatsEntity, 1)
m := make(chan *metrics.StatsEntityWrapper, 1)
expected := &proto.SimpleMetric{Name: "nginx.status", Value: 0.0}
SendNginxDownStatus(context.TODO(), []*proto.Dimension{}, m)
actual := <-m
assert.Equal(t, 1, len(actual.Simplemetrics))
assert.Equal(t, expected, actual.Simplemetrics[0])
assert.Equal(t, 1, len(actual.Data.Simplemetrics))
assert.Equal(t, expected, actual.Data.Simplemetrics[0])
}
9 changes: 5 additions & 4 deletions src/core/metrics/sources/container_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sources

import (
"context"
"github.com/nginx/agent/v2/src/core/metrics"
"path"
"runtime"
"sort"
Expand Down Expand Up @@ -116,7 +117,7 @@ func TestContainerCPUSource(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
actual := make(chan *proto.StatsEntity, 1)
actual := make(chan *metrics.StatsEntityWrapper, 1)
ctx := context.TODO()
wg := &sync.WaitGroup{}
wg.Add(1)
Expand All @@ -130,10 +131,10 @@ func TestContainerCPUSource(t *testing.T) {
sort.SliceStable(test.stats.Simplemetrics, func(i, j int) bool {
return test.stats.Simplemetrics[i].Name < test.stats.Simplemetrics[j].Name
})
sort.SliceStable(result.Simplemetrics, func(i, j int) bool {
return result.Simplemetrics[i].Name < result.Simplemetrics[j].Name
sort.SliceStable(result.Data.Simplemetrics, func(i, j int) bool {
return result.Data.Simplemetrics[i].Name < result.Data.Simplemetrics[j].Name
})
assert.Equal(tt, test.stats.Simplemetrics, result.Simplemetrics)
assert.Equal(tt, test.stats.Simplemetrics, result.Data.Simplemetrics)
case <-time.After(10 * time.Millisecond):
assert.Nil(tt, test.stats)
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/metrics/sources/container_mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sources

import (
"context"
"github.com/nginx/agent/v2/src/core/metrics"
"path"
"runtime"
"sort"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestContainerMemorySource(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
actual := make(chan *proto.StatsEntity, 1)
actual := make(chan *metrics.StatsEntityWrapper, 1)
ctx := context.TODO()
wg := &sync.WaitGroup{}
wg.Add(1)
Expand All @@ -74,10 +75,10 @@ func TestContainerMemorySource(t *testing.T) {
sort.SliceStable(test.stats.Simplemetrics, func(i, j int) bool {
return test.stats.Simplemetrics[i].Name < test.stats.Simplemetrics[j].Name
})
sort.SliceStable(result.Simplemetrics, func(i, j int) bool {
return result.Simplemetrics[i].Name < result.Simplemetrics[j].Name
sort.SliceStable(result.Data.Simplemetrics, func(i, j int) bool {
return result.Data.Simplemetrics[i].Name < result.Data.Simplemetrics[j].Name
})
assert.Equal(tt, test.stats.Simplemetrics, result.Simplemetrics)
assert.Equal(tt, test.stats.Simplemetrics, result.Data.Simplemetrics)
case <-time.After(10 * time.Millisecond):
assert.Nil(tt, test.stats)
}
Expand Down
Loading

0 comments on commit 099413b

Please sign in to comment.