...

Source file src/edge-infra.dev/pkg/edge/api/middleware/metrics_test.go

Documentation: edge-infra.dev/pkg/edge/api/middleware

     1  package middleware
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/99designs/gqlgen/graphql"
    12  	"github.com/gin-gonic/gin"
    13  	"github.com/penglongli/gin-metrics/ginmetrics"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/vektah/gqlparser/v2/ast"
    16  )
    17  
    18  func TestUseMetrics(t *testing.T) {
    19  	assert := assert.New(t)
    20  	UseMetrics(gin.Default())
    21  
    22  	m := ginmetrics.GetMonitor().GetMetric(graphQLRequests)
    23  	assert.NotNil(m, "%s not defined", graphQLRequests)
    24  
    25  	assert.Equal(&ginmetrics.Metric{
    26  		Type:        ginmetrics.Counter,
    27  		Name:        graphQLRequests,
    28  		Description: "count all graphql requests.",
    29  		Labels:      nil,
    30  	}, &ginmetrics.Metric{
    31  		Type:        m.Type,
    32  		Name:        m.Name,
    33  		Description: m.Description,
    34  		Labels:      m.Labels,
    35  	})
    36  
    37  	m = ginmetrics.GetMonitor().GetMetric(graphQlOperations)
    38  	assert.NotNil(m, "%s not defined", graphQlOperations)
    39  
    40  	assert.Equal(&ginmetrics.Metric{
    41  		Type:        ginmetrics.Counter,
    42  		Name:        graphQlOperations,
    43  		Description: "count all graphql requests by name, operation and status.",
    44  		Labels:      []string{"name", "operation", "status"},
    45  	}, &ginmetrics.Metric{
    46  		Type:        m.Type,
    47  		Name:        m.Name,
    48  		Description: m.Description,
    49  		Labels:      m.Labels,
    50  	})
    51  
    52  	m = ginmetrics.GetMonitor().GetMetric(graphQLSlowReQuests)
    53  	assert.NotNil(m, "%s not defined", graphQLSlowReQuests)
    54  
    55  	assert.Equal(&ginmetrics.Metric{
    56  		Type:        ginmetrics.Counter,
    57  		Name:        graphQLSlowReQuests,
    58  		Description: fmt.Sprintf("count all graphql slow requests by name, operation and status. max_lentency=%d sec.", maxLatencySeconds),
    59  		Labels:      []string{"name", "operation", "status"},
    60  	}, &ginmetrics.Metric{
    61  		Type:        m.Type,
    62  		Name:        m.Name,
    63  		Description: m.Description,
    64  		Labels:      m.Labels,
    65  	})
    66  
    67  	m = ginmetrics.GetMonitor().GetMetric(graphQLResponseTime)
    68  	assert.NotNil(m, "%s not defined", graphQLResponseTime)
    69  
    70  	assert.Equal(&ginmetrics.Metric{
    71  		Type:        ginmetrics.Histogram,
    72  		Name:        graphQLResponseTime,
    73  		Description: "the time graphql took handle the request by name.",
    74  		Labels:      []string{"name"},
    75  		Buckets:     []float64{0.1, 0.3, 1.2, 5, 10},
    76  	}, &ginmetrics.Metric{
    77  		Type:        m.Type,
    78  		Name:        m.Name,
    79  		Description: m.Description,
    80  		Labels:      m.Labels,
    81  		Buckets:     m.Buckets,
    82  	})
    83  }
    84  
    85  func TestHandleGraphQlMetrics(t *testing.T) {
    86  	assert := assert.New(t)
    87  	r := gin.Default()
    88  	UseMetrics(r)
    89  	r.POST("/graphql", func(c *gin.Context) {
    90  		ctx := &graphql.OperationContext{
    91  			Operation: &ast.OperationDefinition{
    92  				Operation: "query",
    93  			},
    94  		}
    95  		resp := &graphql.Response{}
    96  		now := time.Now()
    97  		slowTime := now.Add(-6 * time.Second) // fake latency
    98  		HandleGraphQlMetrics(ctx, resp, slowTime)
    99  		c.String(http.StatusOK, http.StatusText(http.StatusOK))
   100  	})
   101  	ts := httptest.NewServer(r)
   102  	defer ts.Close()
   103  	req, err := http.NewRequest(http.MethodPost, ts.URL+"/graphql", http.NoBody)
   104  	assert.NoError(err)
   105  	resp, err := http.DefaultClient.Do(req)
   106  	assert.NoError(err)
   107  
   108  	respBody, err := io.ReadAll(resp.Body)
   109  	assert.NoError(err)
   110  	assert.Equal(http.StatusOK, resp.StatusCode)
   111  	assert.Equal(http.StatusText(http.StatusOK), string(respBody))
   112  
   113  	req, err = http.NewRequest(http.MethodGet, ts.URL+"/metrics", http.NoBody)
   114  	assert.NoError(err)
   115  	resp, err = http.DefaultClient.Do(req)
   116  	assert.NoError(err)
   117  	respBody, err = io.ReadAll(resp.Body)
   118  	assert.NoError(err)
   119  	body := string(respBody)
   120  	for _, name := range []string{graphQLRequests, graphQlOperations, graphQLSlowReQuests, graphQLResponseTime} {
   121  		assert.Contains(body, name, "metric: %s not found in call to /metrics", name)
   122  	}
   123  }
   124  

View as plain text