Skip to content

Commit

Permalink
test: supplementary metrics test (#3037)
Browse files Browse the repository at this point in the history
* test: supplementary metrics test

Signed-off-by: ShyunnY <[email protected]>

* fix: use json enc

Signed-off-by: ShyunnY <[email protected]>

---------

Signed-off-by: ShyunnY <[email protected]>
  • Loading branch information
ShyunnY authored Mar 29, 2024
1 parent 31b4058 commit 767348f
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 28 deletions.
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 {
// 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

0 comments on commit 767348f

Please sign in to comment.