github.com/msales/pkg/v3@v3.24.0/stats/prometheus_internal_test.go (about)

     1  package stats
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPrometheus_formatFQN(t *testing.T) {
    11  	tests := []struct {
    12  		in   string
    13  		want string
    14  	}{
    15  		{
    16  			in:   "test",
    17  			want: "test",
    18  		},
    19  		{
    20  			in:   "test",
    21  			want: "test",
    22  		},
    23  		{
    24  			in:   "test-test2",
    25  			want: "test_test2",
    26  		},
    27  		{
    28  			in:   "test.test.asdf.test",
    29  			want: "test_test_asdf_test",
    30  		},
    31  		{
    32  			in:   "test.test.asdf-test",
    33  			want: "test_test_asdf_test",
    34  		},
    35  		{
    36  			in:   "test.test.asdf.test.test.test.asdf.test.test.test.asdf.test.test.test.asdf..test",
    37  			want: "test_test_asdf_test_test_test_asdf_test_test_test_asdf_test_test_test_asdf__test",
    38  		},
    39  		{
    40  			in:   "test-test.asdf-test.test-test.asdf-test.test-test.asdf-test.test-test.asdf--test",
    41  			want: "test_test_asdf_test_test_test_asdf_test_test_test_asdf_test_test_test_asdf__test",
    42  		},
    43  	}
    44  	prometheus := NewPrometheus("test.test")
    45  
    46  	for _, tt := range tests {
    47  		res := prometheus.formatFQN(tt.in)
    48  		assert.Equal(t, tt.want, res)
    49  	}
    50  }
    51  
    52  func TestFormatPrometheusTags(t *testing.T) {
    53  	tags := []interface{}{"string", "test", "bool", true, "float", 1.1, "int", 2}
    54  	names, labels := formatPrometheusTags(tags)
    55  
    56  	assert.Equal(t, []string{"string", "bool", "float", "int"}, names)
    57  	assert.Equal(t, prometheus.Labels{
    58  		"string": "test",
    59  		"bool":   "true",
    60  		"float":  "1.1",
    61  		"int":    "2",
    62  	}, labels)
    63  }
    64  
    65  func BenchmarkPrometheus_FormatFQN(b *testing.B) {
    66  	testData := "test-test.asdf-test.test-test.asdf-test.test-test.asdf-test.test-test.asdf--test"
    67  
    68  	prometheus := NewPrometheus("test")
    69  	for i := 0; i < b.N; i++ {
    70  		res := prometheus.formatFQN(testData)
    71  
    72  		assert.NotNil(b, res)
    73  	}
    74  }
    75  
    76  func BenchmarkFormatPrometheusTags(b *testing.B) {
    77  	tags := []interface{}{"string", "test", "bool", true, "float", 1.0, "int", 2}
    78  
    79  	b.ReportAllocs()
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		formatPrometheusTags(tags)
    83  	}
    84  }