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

test: supplementary metrics test #3037

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.24.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.24.0
go.opentelemetry.io/otel/exporters/prometheus v0.46.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0
go.opentelemetry.io/otel/metric v1.24.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
go.opentelemetry.io/proto/otlp v1.1.0
Expand Down Expand Up @@ -165,7 +166,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tsaarni/x500dn v1.0.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.24.0 h1:mM8
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.24.0/go.mod h1:0PrIIzDteLSmNyxqcGYRL4mDIo8OTuBAOI/Bn1URxac=
go.opentelemetry.io/otel/exporters/prometheus v0.46.0 h1:I8WIFXR351FoLJYuloU4EgXbtNX2URfU/85pUPheIEQ=
go.opentelemetry.io/otel/exporters/prometheus v0.46.0/go.mod h1:ztwVUHe5DTR/1v7PeuGRnU5Bbd4QKYwApWmuutKsJSs=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0 h1:JYE2HM7pZbOt5Jhk8ndWZTUWYOVift2cHjXVMkPdmdc=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0/go.mod h1:yMb/8c6hVsnma0RpsBMNo0fEiQKeclawtgaIaOp2MLY=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
Expand Down
89 changes: 81 additions & 8 deletions internal/metrics/sample_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,92 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"encoding/json"
"io"
"os"
"testing"

var (
irUpdates = metrics.NewCounter(
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"

"github.com/envoyproxy/gateway/internal/metrics"
)

func TestCounter(t *testing.T) {

writer := os.Stdout
if true {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShyunnY why always true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the metric carries a timestamp, it needs to be written before each test is executed. (So explicitly make it true)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if always true, you shuld just remove this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, these file will be updated every time you run the test, so you'd better improve it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions, I will improve this soon, thank you again!

// nolint:gosec
f, err := os.OpenFile("testdata/counter_metric.yaml", os.O_CREATE|os.O_RDWR, 0644)
require.NoError(t, err)
writer = f
}

p, err := initCounterProvider(writer)
require.NoError(t, err)

// simulate a function that builds an indicator and changes its value
metricsFunc := []func(){
xdsIRCounter,
watchableSubCounter,
}
for _, f := range metricsFunc {
f()
}

// Ensure that metrics data can be flushed by closing the provider
err = p.Shutdown(context.TODO())
require.NoError(t, err)
}

func xdsIRCounter() {
irCounter := metrics.NewCounter(
"ir_updates_total",
"Number of IR updates, by ir type",
)
)

func NewCounter() {
// increment on every xds ir update
irUpdates.With(irType.Value("xds")).Increment()
irCounter.With(metrics.NewLabel("ir-type").Value("xds")).Increment()
// xds ir updates double
irCounter.With(metrics.NewLabel("ir-type").Value("xds")).Add(2)
}

func watchableSubCounter() {
subCounter := metrics.NewCounter(
"watchable_subscribed_total",
"Total number of subscribed watchable.",
)
// increment on every xds ir subscribed
subCounter.With(metrics.NewLabel("ir-type").Value("xds")).Add(2)
// xds ir updates double
irUpdates.With(irType.Value("xds")).Add(2)
subCounter.With(metrics.NewLabel("ir-type").Value("xds")).Add(5)
}

func initCounterProvider(writer io.Writer) (*metric.MeterProvider, error) {
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")

stdExp, err := stdoutmetric.New(
stdoutmetric.WithEncoder(enc),
)
if err != nil {
return nil, err
}

p := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(stdExp)),
metric.WithResource(resource.NewSchemaless(
semconv.ServiceName("test"),
attribute.String("metric-type", "Counter"),
)),
)
otel.SetMeterProvider(p)

return p, nil
}
84 changes: 72 additions & 12 deletions internal/metrics/sample_gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,83 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"encoding/json"
"io"
"os"
"testing"

var (
irType = metrics.NewLabel("ir-type")
currentIRsNum = metrics.NewGauge(
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"

"github.com/envoyproxy/gateway/internal/metrics"
)

func TestGauge(t *testing.T) {
writer := os.Stdout
if true {
// nolint:gosec
f, err := os.OpenFile("testdata/gauge_metric.yaml", os.O_CREATE|os.O_RDWR, 0644)
require.NoError(t, err)
writer = f
}
p, err := initGaugeProvider(writer)
require.NoError(t, err)

// simulate a function that builds an indicator and changes its value
metricsFunc := []func(){
xdsIRGauge,
}
for _, f := range metricsFunc {
f()
}

// Ensure that metrics data can be flushed by closing the provider
err = p.Shutdown(context.TODO())
require.NoError(t, err)
}

func xdsIRGauge() {
currentIRsNum := metrics.NewGauge(
"current_irs_queue_num",
"current number of ir in queue, by ir type",
)
)

func NewGauge() {
// only the last recorded value (2) will be exported for this gauge
currentIRsNum.With(irType.Value("xds")).Record(1)
currentIRsNum.With(irType.Value("xds")).Record(3)
currentIRsNum.With(irType.Value("xds")).Record(2)
currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(1)
currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(3)
currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(2)

currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(1)
currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(3)
currentIRsNum.With(metrics.NewLabel("ir-type").Value("xds")).Record(2)
}

func initGaugeProvider(writer io.Writer) (*metric.MeterProvider, error) {
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")

stdExp, err := stdoutmetric.New(
stdoutmetric.WithEncoder(enc),
)
if err != nil {
return nil, err
}

p := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(stdExp)),
metric.WithResource(resource.NewSchemaless(
semconv.ServiceName("test"),
attribute.String("metric-type", "Gauge"),
)),
)
otel.SetMeterProvider(p)

currentIRsNum.With(irType.Value("infra")).Record(1)
currentIRsNum.With(irType.Value("infra")).Record(3)
currentIRsNum.With(irType.Value("infra")).Record(2)
return p, nil
}
76 changes: 69 additions & 7 deletions internal/metrics/sample_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,81 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"encoding/json"
"io"
"os"
"testing"

var (
method = metrics.NewLabel("method")
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"

sentBytes = metrics.NewHistogram(
"github.com/envoyproxy/gateway/internal/metrics"
)

func TestHistogram(t *testing.T) {
writer := os.Stdout
if true {
// nolint:gosec
f, err := os.OpenFile("testdata/histogram_metric.yaml", os.O_CREATE|os.O_RDWR, 0644)
require.NoError(t, err)
writer = f
}
p, err := initHistogramProvider(writer)
require.NoError(t, err)

// simulate a function that builds an indicator and changes its value
metricsFunc := []func(){
methodHistogram,
}
for _, f := range metricsFunc {
f()
}

// Ensure that metrics data can be flushed by closing the provider
err = p.Shutdown(context.TODO())
require.NoError(t, err)
}

func methodHistogram() {
sentBytes := metrics.NewHistogram(
"sent_bytes_total",
"Histogram of sent bytes by method",
[]float64{10, 50, 100, 1000, 10000},
metrics.WithUnit(metrics.Bytes),
)
)

func NewHistogram() {
sentBytes.With(method.Value("/request/path/1")).Record(458)
// This will hit Bounds of 25,500 and 2500
sentBytes.With(metrics.NewLabel("method").Value("/request/path/1")).Record(20)
sentBytes.With(metrics.NewLabel("method").Value("/request/path/1")).Record(458)
sentBytes.With(metrics.NewLabel("method").Value("/request/path/1")).Record(2000)
}

func initHistogramProvider(writer io.Writer) (*metric.MeterProvider, error) {
enc := json.NewEncoder(writer)
enc.SetIndent("", " ")

stdExp, err := stdoutmetric.New(
stdoutmetric.WithEncoder(enc),
)
if err != nil {
return nil, err
}

p := metric.NewMeterProvider(
metric.WithReader(metric.NewPeriodicReader(stdExp)),
metric.WithResource(resource.NewSchemaless(
semconv.ServiceName("test"),
attribute.String("metric-type", "Histogram"),
)),
)
otel.SetMeterProvider(p)

return p, nil
}
80 changes: 80 additions & 0 deletions internal/metrics/testdata/counter_metric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"Resource": [
{
"Key": "metric-type",
"Value": {
"Type": "STRING",
"Value": "Counter"
}
},
{
"Key": "service.name",
"Value": {
"Type": "STRING",
"Value": "test"
}
}
],
"ScopeMetrics": [
{
"Scope": {
"Name": "envoy-gateway",
"Version": "",
"SchemaURL": ""
},
"Metrics": [
{
"Name": "ir_updates_total",
"Description": "Number of IR updates, by ir type",
"Unit": "1",
"Data": {
"DataPoints": [
{
"Attributes": [
{
"Key": "ir-type",
"Value": {
"Type": "STRING",
"Value": "xds"
}
}
],
"StartTime": "2024-03-27T21:54:18.820255+08:00",
"Time": "2024-03-27T21:54:18.8202957+08:00",
"Value": 3
}
],
"Temporality": "CumulativeTemporality",
"IsMonotonic": true
}
},
{
"Name": "watchable_subscribed_total",
"Description": "Total number of subscribed watchable.",
"Unit": "1",
"Data": {
"DataPoints": [
{
"Attributes": [
{
"Key": "ir-type",
"Value": {
"Type": "STRING",
"Value": "xds"
}
}
],
"StartTime": "2024-03-27T21:54:18.8202957+08:00",
"Time": "2024-03-27T21:54:18.8202957+08:00",
"Value": 7
}
],
"Temporality": "CumulativeTemporality",
"IsMonotonic": true
}
}
]
}
]
}

Loading
Loading