Skip to content
Snippets Groups Projects
Select Git revision
  • ec11ca51e564f75f074e18c8590708de1f2d786d
  • master default protected
  • refactor/PRXS-3053-Files
  • feature/PRXS-3143-3235-ReferenceOptions
  • feature/PRXS-3421-ImplementNewRefAPI
  • feature/PRXS-3143-LimitReferenceFields
  • feature/PRXS-3234-FeaturePruneIdents
  • feature/3149-LocaleCodeAsID-Feature
  • feature/PRXS-3383-CollectionsRankSortAPI
  • PRXS-3421-RecursiveReferences
  • feature/PRXS-3383-CollectionsSort
  • feature/3109-SerializeFeature
  • release/0.33
  • feature/3109-RecoverySchema
  • feature/3109-feature
  • fix/PRXS-3369-ValidateFields
  • refactor/PRXS-3306-MovePkgGroup1
  • refactor/6-pkg-refactor-expr
  • fix/PRXS-3360-TemplateBuilderPatch
  • feature/3293-MongoV2
  • feature/3272-GoVersionUp
  • v0.33.1
  • v0.32.0
  • v0.31.1
  • v0.31.0
  • v0.30.0
  • v0.29.0
  • v0.28.0
  • v0.27.0-alpha.1+16
  • v0.27.0-alpha.1+15
  • v0.27.0-alpha.1+14
  • v0.27.0-alpha.1+13
  • v0.27.0-alpha.1+12
  • v0.27.0-alpha.1+11
  • v0.27.0-alpha.1+10
  • v0.27.0-alpha.1+9
  • v0.27.0-alpha.1+8
  • v0.27.0-alpha.1+7
  • v0.27.0-alpha.1+6
  • v0.27.0-alpha.1+5
  • v0.27.0-alpha.1+4
41 results

setup_test.go

Blame
  • metrics_test.go 2.28 KiB
    package metrics
    
    import (
    	"context"
    	"io"
    	"net/http"
    	"testing"
    
    	"git.perx.ru/perxis/perxis-go/pkg/id"
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"github.com/stretchr/testify/assert"
    	"github.com/stretchr/testify/require"
    )
    
    func serveMetrics(t *testing.T) *http.Server {
    	mux := http.NewServeMux()
    	mux.Handle("/metrics", promhttp.Handler())
    	srv := &http.Server{Addr: "localhost:30301", Handler: mux}
    	go func() { srv.ListenAndServe() }()
    	return srv
    }
    
    func TestMetrics_Example(t *testing.T) {
    	ctx := context.Background()
    	t.Run("Metrics do not allow dynamically assigned labels", func(t *testing.T) {
    		m := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_counter_" + id.GenerateNewID()}, nil)
    		prometheus.MustRegister(m)
    		assert.Panics(t, func() {
    			m.With(prometheus.Labels{"a": "v_a1", "b": "v_b1"}).Inc()
    		})
    	})
    
    	t.Run("Metrics allow dynamically assigned label values", func(t *testing.T) {
    		srv := serveMetrics(t)
    		defer srv.Shutdown(ctx)
    
    		name := "test_counter_" + id.GenerateNewID()
    		m := prometheus.NewCounterVec(prometheus.CounterOpts{Name: name}, []string{"a", "b"})
    		prometheus.MustRegister(m)
    		m.With(prometheus.Labels{"a": "v_a1", "b": "v_b1"}).Inc()
    
    		resp, err := http.Get("http://localhost:30301/metrics")
    		require.NoError(t, err)
    		defer resp.Body.Close()
    		b, err := io.ReadAll(resp.Body)
    		require.NoError(t, err)
    		// fmt.Println(string(b))
    		assert.Contains(t, string(b), name+"{a=\"v_a1\",b=\"v_b1\"} 1")
    	})
    
    	t.Run("Metrics with constant label values", func(t *testing.T) {
    		srv := serveMetrics(t)
    		defer srv.Shutdown(ctx)
    
    		name := "test_counter_" + id.GenerateNewID()
    		m := prometheus.NewCounterVec(prometheus.CounterOpts{
    			Namespace:   "test_namespace",
    			Subsystem:   "test_subsystem",
    			Name:        name,
    			Help:        "Test Help",
    			ConstLabels: prometheus.Labels{"const_1": "val_1"},
    		}, []string{"a", "b"})
    		prometheus.MustRegister(m)
    		m.With(prometheus.Labels{"a": "v_a1", "b": "v_b1"}).Inc()
    
    		resp, err := http.Get("http://localhost:30301/metrics")
    		require.NoError(t, err)
    		defer resp.Body.Close()
    		b, err := io.ReadAll(resp.Body)
    		require.NoError(t, err)
    		// fmt.Println(string(b))
    		assert.Contains(t, string(b), name+"{a=\"v_a1\",b=\"v_b1\",const_1=\"val_1\"} 1")
    	})
    }