github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/metrics/gendoc/table_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package gendoc_test 8 9 import ( 10 "bytes" 11 "io" 12 "os" 13 "path/filepath" 14 "strings" 15 16 "github.com/hyperledger/fabric/common/metrics/gendoc" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("Table", func() { 22 It("generates a markdown document the prometheus metrics", func() { 23 var options []interface{} 24 25 filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error { 26 defer GinkgoRecover() 27 if err == nil && !info.IsDir() && strings.HasSuffix(path, ".go") { 28 f, err := ParseFile(path) 29 Expect(err).NotTo(HaveOccurred()) 30 opts, err := gendoc.FileOptions(f) 31 Expect(err).NotTo(HaveOccurred()) 32 options = append(options, opts...) 33 } 34 return nil 35 }) 36 37 cells, err := gendoc.NewCells(options) 38 Expect(err).NotTo(HaveOccurred()) 39 40 buf := &bytes.Buffer{} 41 w := io.MultiWriter(buf, GinkgoWriter) 42 43 gendoc.NewPrometheusTable(cells).Generate(w) 44 Expect(buf.String()).To(Equal(strings.TrimPrefix(goldenPromTable, "\n"))) 45 }) 46 47 It("generates a markdown document the statsd metrics", func() { 48 var options []interface{} 49 50 filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error { 51 defer GinkgoRecover() 52 if err == nil && !info.IsDir() && strings.HasSuffix(path, ".go") { 53 f, err := ParseFile(path) 54 Expect(err).NotTo(HaveOccurred()) 55 opts, err := gendoc.FileOptions(f) 56 Expect(err).NotTo(HaveOccurred()) 57 options = append(options, opts...) 58 } 59 return nil 60 }) 61 62 cells, err := gendoc.NewCells(options) 63 Expect(err).NotTo(HaveOccurred()) 64 65 buf := &bytes.Buffer{} 66 w := io.MultiWriter(buf, GinkgoWriter) 67 68 gendoc.NewStatsdTable(cells).Generate(w) 69 Expect(buf.String()).To(Equal(strings.TrimPrefix(goldenStatsdTable, "\n"))) 70 }) 71 }) 72 73 const goldenPromTable = ` 74 +--------------------------+-----------+------------------------------------------------------------+--------------------------------------------------------------------------------+ 75 | Name | Type | Description | Labels | 76 +==========================+===========+============================================================+==============+=================================================================+ 77 | fixtures_counter | counter | This is some help text that is more than a few words long. | label_one | this is a really cool label that is the first of many | 78 | | | It really can be quite long. Really long. +--------------+-----------------------------------------------------------------+ 79 | | | | label_two | short and sweet | 80 | | | +--------------+-----------------------------------------------------------------+ 81 | | | | missing_help | | 82 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 83 | fixtures_gauge | gauge | This is some help text that is more than a few words long. | label_one | | 84 | | | It really can be quite long. Really long. This is some +--------------+-----------------------------------------------------------------+ 85 | | | help text that is more than a few words long. It really | label_two | | 86 | | | can be quite long. Really long. | | | 87 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 88 | fixtures_histogram | histogram | This is some help text | label_one | This is a very long help message for label_one, which could be | 89 | | | | | really, really long, and it may never end... | 90 | | | +--------------+-----------------------------------------------------------------+ 91 | | | | label_two | | 92 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 93 | namespace_counter_name | counter | This is some help text | label_one | | 94 | | | +--------------+-----------------------------------------------------------------+ 95 | | | | label_two | | 96 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 97 | namespace_gauge_name | gauge | This is some help text | label_one | | 98 | | | +--------------+-----------------------------------------------------------------+ 99 | | | | label_two | | 100 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 101 | namespace_histogram_name | histogram | This is some help text | label_one | | 102 | | | +--------------+-----------------------------------------------------------------+ 103 | | | | label_two | | 104 +--------------------------+-----------+------------------------------------------------------------+--------------+-----------------------------------------------------------------+ 105 ` 106 107 const goldenStatsdTable = ` 108 +----------------------------------------------------+-----------+------------------------------------------------------------+ 109 | Bucket | Type | Description | 110 +====================================================+===========+============================================================+ 111 | fixtures.counter.%{label_one}.%{label_two} | counter | This is some help text that is more than a few words long. | 112 | | | It really can be quite long. Really long. | 113 +----------------------------------------------------+-----------+------------------------------------------------------------+ 114 | fixtures.gauge.%{label_one}.%{label_two} | gauge | This is some help text that is more than a few words long. | 115 | | | It really can be quite long. Really long. This is some | 116 | | | help text that is more than a few words long. It really | 117 | | | can be quite long. Really long. | 118 +----------------------------------------------------+-----------+------------------------------------------------------------+ 119 | fixtures.histogram.%{label_one}.%{label_two} | histogram | This is some help text | 120 +----------------------------------------------------+-----------+------------------------------------------------------------+ 121 | namespace.counter.name.%{label_one}.%{label_two} | counter | This is some help text | 122 +----------------------------------------------------+-----------+------------------------------------------------------------+ 123 | namespace.gauge.name.%{label_one}.%{label_two} | gauge | This is some help text | 124 +----------------------------------------------------+-----------+------------------------------------------------------------+ 125 | namespace.histogram.name.%{label_one}.%{label_two} | histogram | This is some help text | 126 +----------------------------------------------------+-----------+------------------------------------------------------------+ 127 `