Skip to content

Commit

Permalink
test(promutil): add promutil simulator test (pingcap#401)
Browse files Browse the repository at this point in the history
* test(promutil): add promutil simulator test
  • Loading branch information
maxshuang authored May 20, 2022
1 parent 65d5bf4 commit eb3f21a
Show file tree
Hide file tree
Showing 12 changed files with 1,665 additions and 51 deletions.
7 changes: 6 additions & 1 deletion pkg/promutil/implement.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ func wrapHistogramOpts(prefix string, constLabels prometheus.Labels, opts *prome
}

func wrapOptsCommon(prefix string, constLabels prometheus.Labels, namespace *string, cls prometheus.Labels) {
// namespace SHOULD NOT be nil
if prefix != "" {
*namespace = prefix + "_" + *namespace
if *namespace != "" {
*namespace = prefix + "_" + *namespace
} else {
*namespace = prefix
}
}
for name, value := range constLabels {
if _, exists := cls[name]; exists {
Expand Down
121 changes: 111 additions & 10 deletions pkg/promutil/implement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestNewCounter(t *testing.T) {
projectKey := constLabelProjectKey
tenantKey := constLabelTenantKey

factory := NewFactory4JobMaster(
factory := NewFactory4JobMasterImpl(
reg,
tenant,
jobType,
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestNewCounter(t *testing.T) {

// different jobID of the same project, but with same metric
jobID = "job1"
factory = NewFactory4JobMaster(
factory = NewFactory4JobMasterImpl(
reg,
tenant,
jobType,
Expand All @@ -173,7 +173,7 @@ func TestNewCounter(t *testing.T) {

// different project but with same metric
tenant.ProjectID = "project1"
factory = NewFactory4JobMaster(
factory = NewFactory4JobMasterImpl(
reg,
tenant,
jobType,
Expand All @@ -191,7 +191,7 @@ func TestNewCounter(t *testing.T) {
// JobMaster and Worker of the same job type can't has same
// metric name
workerID := "worker0"
factory = NewFactory4Worker(
factory = NewFactory4WorkerImpl(
reg,
tenant,
jobType,
Expand All @@ -209,7 +209,7 @@ func TestNewCounter(t *testing.T) {

// different workerID of the same job, but with same metric
workerID = "worker1"
factory = NewFactory4Worker(
factory = NewFactory4WorkerImpl(
reg,
tenant,
jobType,
Expand All @@ -226,7 +226,7 @@ func TestNewCounter(t *testing.T) {
})

// framework with same metric
factory = NewFactory4Framework(
factory = NewFactory4FrameworkImpl(
reg,
)
counter = factory.NewCounter(prometheus.CounterOpts{
Expand All @@ -239,13 +239,114 @@ func TestNewCounter(t *testing.T) {
})
}

func TestNewCounterFail(t *testing.T) {
// [TODO]: const label conflict with inner const labels
// const label conflict with inner const labels
func TestNewCounterFailConstLabelConflict(t *testing.T) {
t.Parallel()

defer func() {
err := recover()
require.NotNil(t, err)
require.Regexp(t, "duplicate label name", err.(string))
}()

// [TODO]: metric duplicate
reg := NewRegistry()
require.NotNil(t, reg)

factory := NewFactory4JobMasterImpl(
reg,
tenant.ProjectInfo{
TenantID: "user0",
ProjectID: "proj0",
},
"DM",
"job0",
)
_ = factory.NewCounter(prometheus.CounterOpts{
Namespace: "dm",
Subsystem: "worker",
Name: "http_request",
ConstLabels: prometheus.Labels{
constLabelJobKey: "job0", // conflict with inner const labels
},
})
}

// TODO: add more uts to solidate the api behavior
func TestNewCounterVec(t *testing.T) {
t.Parallel()

reg := NewRegistry()
require.NotNil(t, reg)

factory := NewFactory4JobMasterImpl(
reg,
tenant.ProjectInfo{
TenantID: "user0",
ProjectID: "proj0",
},
"DM",
"job0",
)
counterVec := factory.NewCounterVec(prometheus.CounterOpts{
Namespace: "dm",
Subsystem: "worker",
Name: "http_request",
ConstLabels: prometheus.Labels{
"k1": "v1",
},
},
[]string{"k2", "k3", "k4"},
)

counter, err := counterVec.GetMetricWithLabelValues([]string{"v1", "v2", "v3"}...)
require.NoError(t, err)
counter.Inc()

// unmatch label values count
_, err = counterVec.GetMetricWithLabelValues([]string{"v1", "v2"}...)
require.Error(t, err)

counter, err = counterVec.GetMetricWith(prometheus.Labels{
"k2": "v2", "k3": "v3", "k4": "v4",
})
require.NoError(t, err)
counter.Inc()

// unmatch label values count
counter, err = counterVec.GetMetricWith(prometheus.Labels{
"k3": "v3", "k4": "v4",
})
require.Error(t, err)

require.True(t, counterVec.DeleteLabelValues([]string{"v1", "v2", "v3"}...))
require.False(t, counterVec.DeleteLabelValues([]string{"v1", "v2"}...))
require.False(t, counterVec.DeleteLabelValues([]string{"v1", "v2", "v4"}...))

require.True(t, counterVec.Delete(prometheus.Labels{
"k2": "v2", "k3": "v3", "k4": "v4",
}))
require.False(t, counterVec.Delete(prometheus.Labels{
"k3": "v3", "k4": "v4",
}))
require.False(t, counterVec.Delete(prometheus.Labels{
"k2": "v3", "k3": "v3", "k4": "v4",
}))

curryCounterVec, err := counterVec.CurryWith(prometheus.Labels{
"k2": "v2",
})
require.NoError(t, err)
counter, err = curryCounterVec.GetMetricWith(prometheus.Labels{
"k3": "v3", "k4": "v4",
})
require.NoError(t, err)
counter.Add(1)

// unmatch label values count after curry
_, err = curryCounterVec.GetMetricWith(prometheus.Labels{
"k2": "v2", "k3": "v3", "k4": "v4",
})
require.Error(t, err)
}

func compareMetric(t *testing.T, expected *dto.Metric, actual *dto.Metric) {
// compare label pairs
Expand Down
2 changes: 2 additions & 0 deletions pkg/promutil/test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
GO111MODULE=on go build -o metric_main ./
74 changes: 74 additions & 0 deletions pkg/promutil/test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module github.com/hanfei1991/microcosm/pkg/promutil/example

go 1.18

require (
github.com/hanfei1991/microcosm v0.0.0-20220519065215-9a7a74c24c30
github.com/prometheus/client_golang v1.12.2
)

require (
github.com/Shopify/sarama v1.29.0 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eapache/go-resiliency v1.2.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/edwingeng/deque v0.0.0-20191220032131-8596380dee17 // indirect
github.com/gavv/monotime v0.0.0-20190418164738-30dba4353424 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c // indirect
github.com/pingcap/failpoint v0.0.0-20220303073211-00fea37feb66 // indirect
github.com/pingcap/kvproto v0.0.0-20220328072018-6e75c12dbd73 // indirect
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee // indirect
github.com/pingcap/tidb v1.1.0-beta.0.20220412180037-d07b66ea638c // indirect
github.com/pingcap/tidb-tools v6.0.0-alpha.0.20220317013353-dfc5146f4746+incompatible // indirect
github.com/pingcap/tiflow v0.0.0-20220418100802-8c4f693f6456 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/tikv/pd/client v0.0.0-20220307081149-841fa61e9710 // indirect
github.com/xdg/scram v1.0.3 // indirect
github.com/xdg/stringprep v1.0.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
google.golang.org/genproto v0.0.0-20220216160803-4663080d8bc8 // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gorm.io/gorm v1.23.4 // indirect
)
Loading

0 comments on commit eb3f21a

Please sign in to comment.