registry_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package prometheus
  2. import (
  3. "strings"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. "github.com/prometheus/client_golang/prometheus"
  9. dto "github.com/prometheus/client_model/go"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/ydb-platform/ydb/library/go/core/metrics"
  12. "github.com/ydb-platform/ydb/library/go/ptr"
  13. "github.com/ydb-platform/ydb/library/go/test/assertpb"
  14. "google.golang.org/protobuf/testing/protocmp"
  15. )
  16. func TestNewRegistry(t *testing.T) {
  17. expected := &Registry{
  18. rg: prometheus.NewRegistry(),
  19. m: new(sync.Mutex),
  20. subregistries: make(map[string]*Registry),
  21. tags: map[string]string{},
  22. prefix: "",
  23. streamFormat: StreamCompact,
  24. }
  25. r := NewRegistry(nil)
  26. assert.IsType(t, expected, r)
  27. assert.Equal(t, expected, r)
  28. }
  29. func TestRegistry_Subregisters(t *testing.T) {
  30. r := NewRegistry(nil)
  31. sr1 := r.WithPrefix("subregister1").
  32. WithTags(map[string]string{"ololo": "trololo"})
  33. sr2 := sr1.WithPrefix("subregister2").
  34. WithTags(map[string]string{"shimba": "boomba"})
  35. // check global subregistries map
  36. expectedMap := map[string]*Registry{
  37. "\"subregister1\"{}": {
  38. rg: r.rg,
  39. m: r.m,
  40. subregistries: r.subregistries,
  41. prefix: "subregister1",
  42. tags: make(map[string]string),
  43. },
  44. "\"subregister1\"{ololo=trololo}": {
  45. rg: r.rg,
  46. m: r.m,
  47. subregistries: r.subregistries,
  48. tags: map[string]string{"ololo": "trololo"},
  49. prefix: "subregister1",
  50. },
  51. "\"subregister1_subregister2\"{ololo=trololo}": {
  52. rg: r.rg,
  53. m: r.m,
  54. subregistries: r.subregistries,
  55. tags: map[string]string{"ololo": "trololo"},
  56. prefix: "subregister1_subregister2",
  57. },
  58. "\"subregister1_subregister2\"{ololo=trololo,shimba=boomba}": {
  59. rg: r.rg,
  60. m: r.m,
  61. subregistries: r.subregistries,
  62. tags: map[string]string{"ololo": "trololo", "shimba": "boomba"},
  63. prefix: "subregister1_subregister2",
  64. },
  65. }
  66. assert.EqualValues(t, expectedMap, r.subregistries)
  67. // top-register write
  68. rCnt := r.Counter("subregisters_count")
  69. rCnt.Add(2)
  70. // sub-register write
  71. srTm := sr1.Timer("mytimer")
  72. srTm.RecordDuration(42 * time.Second)
  73. // sub-sub-register write
  74. srHm := sr2.Histogram("myhistogram", metrics.NewBuckets(1, 2, 3))
  75. srHm.RecordValue(1.5)
  76. mr, err := r.Gather()
  77. assert.NoError(t, err)
  78. assert.IsType(t, mr, []*dto.MetricFamily{})
  79. expected := []*dto.MetricFamily{
  80. {
  81. Name: ptr.String("subregister1_mytimer"),
  82. Help: ptr.String(""),
  83. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_GAUGE),
  84. Metric: []*dto.Metric{
  85. {
  86. Label: []*dto.LabelPair{
  87. {Name: ptr.String("ololo"), Value: ptr.String("trololo")},
  88. },
  89. Gauge: &dto.Gauge{Value: ptr.Float64(42)},
  90. },
  91. },
  92. },
  93. {
  94. Name: ptr.String("subregister1_subregister2_myhistogram"),
  95. Help: ptr.String(""),
  96. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_HISTOGRAM),
  97. Metric: []*dto.Metric{
  98. {
  99. Label: []*dto.LabelPair{
  100. {Name: ptr.String("ololo"), Value: ptr.String("trololo")},
  101. {Name: ptr.String("shimba"), Value: ptr.String("boomba")},
  102. },
  103. Histogram: &dto.Histogram{
  104. SampleCount: ptr.Uint64(1),
  105. SampleSum: ptr.Float64(1.5),
  106. Bucket: []*dto.Bucket{
  107. {CumulativeCount: ptr.Uint64(0), UpperBound: ptr.Float64(1)},
  108. {CumulativeCount: ptr.Uint64(1), UpperBound: ptr.Float64(2)},
  109. {CumulativeCount: ptr.Uint64(1), UpperBound: ptr.Float64(3)},
  110. },
  111. },
  112. },
  113. },
  114. },
  115. {
  116. Name: ptr.String("subregisters_count"),
  117. Help: ptr.String(""),
  118. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_COUNTER),
  119. Metric: []*dto.Metric{
  120. {
  121. Label: []*dto.LabelPair{},
  122. Counter: &dto.Counter{Value: ptr.Float64(2)},
  123. },
  124. },
  125. },
  126. }
  127. cmpOpts := []cmp.Option{
  128. protocmp.Transform(),
  129. }
  130. assert.True(t, cmp.Equal(expected, mr, cmpOpts...), cmp.Diff(expected, mr, cmpOpts...))
  131. }
  132. func TestRegistry_Counter(t *testing.T) {
  133. r := NewRegistry(nil)
  134. sr := r.WithPrefix("myprefix").
  135. WithTags(map[string]string{"ololo": "trololo"})
  136. // must panic on empty name
  137. assert.Panics(t, func() { r.Counter("") })
  138. srCnt := sr.Counter("mycounter")
  139. srCnt.Add(42)
  140. mr, err := r.Gather()
  141. assert.NoError(t, err)
  142. assert.IsType(t, mr, []*dto.MetricFamily{})
  143. expected := []*dto.MetricFamily{
  144. {
  145. Name: ptr.String("myprefix_mycounter"),
  146. Help: ptr.String(""),
  147. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_COUNTER),
  148. Metric: []*dto.Metric{
  149. {
  150. Label: []*dto.LabelPair{
  151. {Name: ptr.String("ololo"), Value: ptr.String("trololo")},
  152. },
  153. Counter: &dto.Counter{Value: ptr.Float64(42)},
  154. },
  155. },
  156. },
  157. }
  158. cmpOpts := []cmp.Option{
  159. protocmp.Transform(),
  160. }
  161. assert.True(t, cmp.Equal(expected, mr, cmpOpts...), cmp.Diff(expected, mr, cmpOpts...))
  162. }
  163. func TestRegistry_DurationHistogram(t *testing.T) {
  164. r := NewRegistry(nil)
  165. sr := r.WithPrefix("myprefix").
  166. WithTags(map[string]string{"ololo": "trololo"})
  167. // must panic on empty name
  168. assert.Panics(t, func() { r.DurationHistogram("", nil) })
  169. cnt := sr.DurationHistogram("myhistogram", metrics.NewDurationBuckets(
  170. 1*time.Second, 3*time.Second, 5*time.Second,
  171. ))
  172. cnt.RecordDuration(2 * time.Second)
  173. cnt.RecordDuration(4 * time.Second)
  174. mr, err := r.Gather()
  175. assert.NoError(t, err)
  176. assert.IsType(t, mr, []*dto.MetricFamily{})
  177. expected := []*dto.MetricFamily{
  178. {
  179. Name: ptr.String("myprefix_myhistogram"),
  180. Help: ptr.String(""),
  181. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_HISTOGRAM),
  182. Metric: []*dto.Metric{
  183. {
  184. Label: []*dto.LabelPair{{Name: ptr.String("ololo"), Value: ptr.String("trololo")}},
  185. Histogram: &dto.Histogram{
  186. SampleCount: ptr.Uint64(2),
  187. SampleSum: ptr.Float64(6),
  188. Bucket: []*dto.Bucket{
  189. {CumulativeCount: ptr.Uint64(0), UpperBound: ptr.Float64(1)},
  190. {CumulativeCount: ptr.Uint64(1), UpperBound: ptr.Float64(3)},
  191. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(5)},
  192. },
  193. },
  194. },
  195. },
  196. },
  197. }
  198. assertpb.Equal(t, expected, mr)
  199. }
  200. func TestRegistry_Gauge(t *testing.T) {
  201. r := NewRegistry(nil)
  202. sr := r.WithPrefix("myprefix").
  203. WithTags(map[string]string{"ololo": "trololo"})
  204. // must panic on empty name
  205. assert.Panics(t, func() { r.Gauge("") })
  206. cnt := sr.Gauge("mygauge")
  207. cnt.Add(42)
  208. mr, err := r.Gather()
  209. assert.NoError(t, err)
  210. assert.IsType(t, mr, []*dto.MetricFamily{})
  211. expected := []*dto.MetricFamily{
  212. {
  213. Name: ptr.String("myprefix_mygauge"),
  214. Help: ptr.String(""),
  215. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_GAUGE),
  216. Metric: []*dto.Metric{
  217. {
  218. Label: []*dto.LabelPair{{Name: ptr.String("ololo"), Value: ptr.String("trololo")}},
  219. Gauge: &dto.Gauge{Value: ptr.Float64(42)},
  220. },
  221. },
  222. },
  223. }
  224. assertpb.Equal(t, expected, mr)
  225. }
  226. func TestRegistry_Histogram(t *testing.T) {
  227. r := NewRegistry(nil)
  228. sr := r.WithPrefix("myprefix").
  229. WithTags(map[string]string{"ololo": "trololo"})
  230. // must panic on empty name
  231. assert.Panics(t, func() { r.Histogram("", nil) })
  232. cnt := sr.Histogram("myhistogram", metrics.NewBuckets(1, 3, 5))
  233. cnt.RecordValue(2)
  234. cnt.RecordValue(4)
  235. mr, err := r.Gather()
  236. assert.NoError(t, err)
  237. assert.IsType(t, mr, []*dto.MetricFamily{})
  238. expected := []*dto.MetricFamily{
  239. {
  240. Name: ptr.String("myprefix_myhistogram"),
  241. Help: ptr.String(""),
  242. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_HISTOGRAM),
  243. Metric: []*dto.Metric{
  244. {
  245. Label: []*dto.LabelPair{{Name: ptr.String("ololo"), Value: ptr.String("trololo")}},
  246. Histogram: &dto.Histogram{
  247. SampleCount: ptr.Uint64(2),
  248. SampleSum: ptr.Float64(6),
  249. Bucket: []*dto.Bucket{
  250. {CumulativeCount: ptr.Uint64(0), UpperBound: ptr.Float64(1)},
  251. {CumulativeCount: ptr.Uint64(1), UpperBound: ptr.Float64(3)},
  252. {CumulativeCount: ptr.Uint64(2), UpperBound: ptr.Float64(5)},
  253. },
  254. },
  255. },
  256. },
  257. },
  258. }
  259. assertpb.Equal(t, expected, mr)
  260. }
  261. func TestRegistry_Timer(t *testing.T) {
  262. r := NewRegistry(nil)
  263. sr := r.WithPrefix("myprefix").
  264. WithTags(map[string]string{"ololo": "trololo"})
  265. // must panic on empty name
  266. assert.Panics(t, func() { r.Timer("") })
  267. cnt := sr.Timer("mytimer")
  268. cnt.RecordDuration(42 * time.Second)
  269. mr, err := r.Gather()
  270. assert.NoError(t, err)
  271. assert.IsType(t, mr, []*dto.MetricFamily{})
  272. expected := []*dto.MetricFamily{
  273. {
  274. Name: ptr.String("myprefix_mytimer"),
  275. Help: ptr.String(""),
  276. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_GAUGE),
  277. Metric: []*dto.Metric{
  278. {
  279. Label: []*dto.LabelPair{{Name: ptr.String("ololo"), Value: ptr.String("trololo")}},
  280. Gauge: &dto.Gauge{Value: ptr.Float64(42)},
  281. },
  282. },
  283. },
  284. }
  285. assertpb.Equal(t, expected, mr)
  286. }
  287. func TestRegistry_WithPrefix(t *testing.T) {
  288. testCases := []struct {
  289. r metrics.Registry
  290. expected string
  291. }{
  292. {
  293. r: func() metrics.Registry {
  294. return NewRegistry(nil)
  295. }(),
  296. expected: "",
  297. },
  298. {
  299. r: func() metrics.Registry {
  300. return NewRegistry(nil).WithPrefix("myprefix")
  301. }(),
  302. expected: "myprefix",
  303. },
  304. {
  305. r: func() metrics.Registry {
  306. return NewRegistry(nil).WithPrefix("__myprefix_")
  307. }(),
  308. expected: "myprefix",
  309. },
  310. {
  311. r: func() metrics.Registry {
  312. return NewRegistry(nil).WithPrefix("__myprefix_").WithPrefix("mysubprefix______")
  313. }(),
  314. expected: "myprefix_mysubprefix",
  315. },
  316. }
  317. for _, tc := range testCases {
  318. t.Run("", func(t *testing.T) {
  319. assert.Equal(t, tc.expected, tc.r.(*Registry).prefix)
  320. })
  321. }
  322. }
  323. func TestRegistry_WithTags(t *testing.T) {
  324. testCases := []struct {
  325. r metrics.Registry
  326. expected map[string]string
  327. }{
  328. {
  329. r: func() metrics.Registry {
  330. return NewRegistry(nil)
  331. }(),
  332. expected: map[string]string{},
  333. },
  334. {
  335. r: func() metrics.Registry {
  336. return NewRegistry(nil).WithTags(map[string]string{"shimba": "boomba"})
  337. }(),
  338. expected: map[string]string{"shimba": "boomba"},
  339. },
  340. {
  341. r: func() metrics.Registry {
  342. return NewRegistry(nil).
  343. WithTags(map[string]string{"shimba": "boomba"}).
  344. WithTags(map[string]string{"looken": "tooken"})
  345. }(),
  346. expected: map[string]string{
  347. "shimba": "boomba",
  348. "looken": "tooken",
  349. },
  350. },
  351. {
  352. r: func() metrics.Registry {
  353. return NewRegistry(nil).
  354. WithTags(map[string]string{"shimba": "boomba"}).
  355. WithTags(map[string]string{"looken": "tooken"}).
  356. WithTags(map[string]string{"shimba": "cooken"})
  357. }(),
  358. expected: map[string]string{
  359. "shimba": "cooken",
  360. "looken": "tooken",
  361. },
  362. },
  363. }
  364. for _, tc := range testCases {
  365. t.Run("", func(t *testing.T) {
  366. assert.Equal(t, tc.expected, tc.r.(*Registry).tags)
  367. })
  368. }
  369. }
  370. func TestRegistry_WithTags_NoPanic(t *testing.T) {
  371. _ = NewRegistry(nil).WithTags(map[string]string{"foo": "bar"})
  372. _ = NewRegistry(nil).WithTags(map[string]string{"foo": "bar"})
  373. }
  374. func TestRegistry_Counter_NoPanic(t *testing.T) {
  375. r := NewRegistry(nil)
  376. sr := r.WithPrefix("myprefix").
  377. WithTags(map[string]string{"ololo": "trololo"})
  378. cntrRaz := sr.Counter("mycounter").(*Counter)
  379. cntrDvaz := sr.Counter("mycounter").(*Counter)
  380. assert.Equal(t, cntrRaz.cnt, cntrDvaz.cnt)
  381. cntrRaz.Add(100)
  382. cntrDvaz.Add(100)
  383. mr, err := r.Gather()
  384. assert.NoError(t, err)
  385. expected := []*dto.MetricFamily{
  386. {
  387. Name: ptr.String("myprefix_mycounter"),
  388. Help: ptr.String(""),
  389. Type: func(mt dto.MetricType) *dto.MetricType { return &mt }(dto.MetricType_COUNTER),
  390. Metric: []*dto.Metric{
  391. {
  392. Label: []*dto.LabelPair{{Name: ptr.String("ololo"), Value: ptr.String("trololo")}},
  393. Counter: &dto.Counter{Value: ptr.Float64(200)},
  394. },
  395. },
  396. },
  397. }
  398. assertpb.Equal(t, expected, mr)
  399. }
  400. func TestRegistry_NameSanitizer(t *testing.T) {
  401. testCases := []struct {
  402. opts *RegistryOpts
  403. name string
  404. want string
  405. }{
  406. {
  407. opts: nil,
  408. name: "some_name",
  409. want: "some_name",
  410. },
  411. {
  412. opts: NewRegistryOpts().SetNameSanitizer(func(s string) string {
  413. return strings.ReplaceAll(s, "/", "_")
  414. }),
  415. name: "other/name",
  416. want: "other_name",
  417. },
  418. }
  419. for _, tc := range testCases {
  420. r := NewRegistry(tc.opts)
  421. _ = r.Counter(tc.name)
  422. mfs, err := r.Gather()
  423. assert.NoError(t, err)
  424. assert.NotEmpty(t, mfs)
  425. assert.Equal(t, tc.want, *mfs[0].Name)
  426. }
  427. }