Skip to content

Commit

Permalink
test: supplementary metrics test
Browse files Browse the repository at this point in the history
Signed-off-by: ShyunnY <[email protected]>
  • Loading branch information
ShyunnY committed Mar 27, 2024
1 parent a11aa70 commit 0f5c0e8
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 27 deletions.
1 change: 1 addition & 0 deletions 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
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
86 changes: 78 additions & 8 deletions internal/metrics/sample_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,89 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"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) {
stdExp, err := stdoutmetric.New(
stdoutmetric.WithPrettyPrint(),
stdoutmetric.WithWriter(writer),
)
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
}
81 changes: 69 additions & 12 deletions internal/metrics/sample_gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,80 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"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) {
stdExp, err := stdoutmetric.New(
stdoutmetric.WithPrettyPrint(),
stdoutmetric.WithWriter(writer),
)
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
}
73 changes: 66 additions & 7 deletions internal/metrics/sample_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,78 @@

package metrics_test

import "github.com/envoyproxy/gateway/internal/metrics"
import (
"context"
"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) {
stdExp, err := stdoutmetric.New(
stdoutmetric.WithPrettyPrint(),
stdoutmetric.WithWriter(writer),
)
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
}
79 changes: 79 additions & 0 deletions internal/metrics/testdata/counter_metric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"Resource": [

Check failure on line 2 in internal/metrics/testdata/counter_metric.yaml

View workflow job for this annotation

GitHub Actions / lint

2:1 syntax error: found character '\t' that cannot start any token (syntax)

Check failure on line 2 in internal/metrics/testdata/counter_metric.yaml

View workflow job for this annotation

GitHub Actions / lint

2:1 syntax error: found character '\t' that cannot start any token (syntax)
{
"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-27T16:02:06.6440075+08:00",
"Time": "2024-03-27T16:02:06.6440075+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-27T16:02:06.6440075+08:00",
"Time": "2024-03-27T16:02:06.6440075+08:00",
"Value": 7
}
],
"Temporality": "CumulativeTemporality",
"IsMonotonic": true
}
}
]
}
]
}
Loading

0 comments on commit 0f5c0e8

Please sign in to comment.