github.com/Jeffail/benthos/v3@v3.65.0/internal/component/metrics/namespaced_test.go (about) 1 package metrics 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/Jeffail/benthos/v3/lib/log" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 "github.com/Jeffail/benthos/v3/lib/types" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func getTestProm(t *testing.T) (metrics.Type, http.HandlerFunc) { 17 t.Helper() 18 19 conf := metrics.NewConfig() 20 conf.Prometheus.Prefix = "" 21 conf.Type = metrics.TypePrometheus 22 23 prom, err := metrics.New(conf) 24 require.NoError(t, err) 25 26 wHandler, ok := prom.(metrics.WithHandlerFunc) 27 require.True(t, ok) 28 29 return prom, wHandler.HandlerFunc() 30 } 31 32 func getPage(t *testing.T, handler http.HandlerFunc) string { 33 t.Helper() 34 35 req := httptest.NewRequest("GET", "http://example.com/foo", nil) 36 w := httptest.NewRecorder() 37 handler(w, req) 38 39 body, err := io.ReadAll(w.Result().Body) 40 require.NoError(t, err) 41 42 return string(body) 43 } 44 45 func TestNamespacedNothing(t *testing.T) { 46 prom, handler := getTestProm(t) 47 48 nm := NewNamespaced(prom) 49 50 ctr := nm.GetCounter("counterone") 51 ctr.Incr(10) 52 ctr.Incr(11) 53 54 gge := nm.GetGauge("gaugeone") 55 gge.Set(12) 56 57 tmr := nm.GetTimer("timerone") 58 tmr.Timing(13) 59 60 ctrTwo := nm.GetCounterVec("countertwo", []string{"label1"}) 61 ctrTwo.With("value1").Incr(10) 62 ctrTwo.With("value2").Incr(11) 63 64 ggeTwo := nm.GetGaugeVec("gaugetwo", []string{"label2"}) 65 ggeTwo.With("value3").Set(12) 66 67 tmrTwo := nm.GetTimerVec("timertwo", []string{"label3", "label4"}) 68 tmrTwo.With("value4", "value5").Timing(13) 69 70 body := getPage(t, handler) 71 72 assert.Contains(t, body, "\ncounterone 21") 73 assert.Contains(t, body, "\ngaugeone 12") 74 assert.Contains(t, body, "\ntimerone_sum 13") 75 assert.Contains(t, body, "\ncountertwo{label1=\"value1\"} 10") 76 assert.Contains(t, body, "\ncountertwo{label1=\"value2\"} 11") 77 assert.Contains(t, body, "\ngaugetwo{label2=\"value3\"} 12") 78 assert.Contains(t, body, "\ntimertwo_sum{label3=\"value4\",label4=\"value5\"} 13") 79 } 80 81 func TestNamespacedPrefix(t *testing.T) { 82 prom, handler := getTestProm(t) 83 84 nm := NewNamespaced(prom).WithPrefix("foo") 85 86 ctr := nm.GetCounter("counterone") 87 ctr.Incr(10) 88 ctr.Incr(11) 89 90 gge := nm.GetGauge("gaugeone") 91 gge.Set(12) 92 93 tmr := nm.GetTimer("timerone") 94 tmr.Timing(13) 95 96 ctrTwo := nm.GetCounterVec("countertwo", []string{"label1"}) 97 ctrTwo.With("value1").Incr(10) 98 ctrTwo.With("value2").Incr(11) 99 100 ggeTwo := nm.GetGaugeVec("gaugetwo", []string{"label2"}) 101 ggeTwo.With("value3").Set(12) 102 103 tmrTwo := nm.GetTimerVec("timertwo", []string{"label3", "label4"}) 104 tmrTwo.With("value4", "value5").Timing(13) 105 106 nm2 := nm.WithPrefix("bar") 107 108 ctrThree := nm2.GetCounter("counterthree") 109 ctrThree.Incr(22) 110 111 body := getPage(t, handler) 112 113 assert.Contains(t, body, "\nfoo_counterone 21") 114 assert.Contains(t, body, "\nfoo_gaugeone 12") 115 assert.Contains(t, body, "\nfoo_timerone_sum 13") 116 assert.Contains(t, body, "\nfoo_countertwo{label1=\"value1\"} 10") 117 assert.Contains(t, body, "\nfoo_countertwo{label1=\"value2\"} 11") 118 assert.Contains(t, body, "\nfoo_gaugetwo{label2=\"value3\"} 12") 119 assert.Contains(t, body, "\nfoo_timertwo_sum{label3=\"value4\",label4=\"value5\"} 13") 120 assert.Contains(t, body, "\nbar_counterthree 22") 121 } 122 123 func TestNamespacedPrefixStaticLabels(t *testing.T) { 124 prom, handler := getTestProm(t) 125 126 nm := NewNamespaced(prom).WithPrefix("foo").WithLabels("static1", "svalue1") 127 128 ctr := nm.GetCounter("counterone") 129 ctr.Incr(10) 130 ctr.Incr(11) 131 132 gge := nm.GetGauge("gaugeone") 133 gge.Set(12) 134 135 tmr := nm.GetTimer("timerone") 136 tmr.Timing(13) 137 138 ctrTwo := nm.GetCounterVec("countertwo", []string{"label1"}) 139 ctrTwo.With("value1").Incr(10) 140 ctrTwo.With("value2").Incr(11) 141 142 ggeTwo := nm.GetGaugeVec("gaugetwo", []string{"label2"}) 143 ggeTwo.With("value3").Set(12) 144 145 tmrTwo := nm.GetTimerVec("timertwo", []string{"label3", "label4"}) 146 tmrTwo.With("value4", "value5").Timing(13) 147 148 nm2 := nm.WithPrefix("bar").WithLabels("static2", "svalue2") 149 150 ctrThree := nm2.GetCounter("counterthree") 151 ctrThree.Incr(22) 152 153 body := getPage(t, handler) 154 155 assert.Contains(t, body, "\nfoo_counterone{static1=\"svalue1\"} 21") 156 assert.Contains(t, body, "\nfoo_gaugeone{static1=\"svalue1\"} 12") 157 assert.Contains(t, body, "\nfoo_timerone_sum{static1=\"svalue1\"} 13") 158 assert.Contains(t, body, "\nfoo_countertwo{label1=\"value1\",static1=\"svalue1\"} 10") 159 assert.Contains(t, body, "\nfoo_countertwo{label1=\"value2\",static1=\"svalue1\"} 11") 160 assert.Contains(t, body, "\nfoo_gaugetwo{label2=\"value3\",static1=\"svalue1\"} 12") 161 assert.Contains(t, body, "\nfoo_timertwo_sum{label3=\"value4\",label4=\"value5\",static1=\"svalue1\"} 13") 162 assert.Contains(t, body, "\nbar_counterthree{static1=\"svalue1\",static2=\"svalue2\"} 22") 163 } 164 165 func TestNamespacedPrefixStaticLabelsWithMappings(t *testing.T) { 166 prom, handler := getTestProm(t) 167 168 mappingFooToBar, err := NewMapping(types.NoopMgr(), `root = this.replace("foo","bar")`, log.Noop()) 169 require.NoError(t, err) 170 171 mappingBarToBaz, err := NewMapping(types.NoopMgr(), `root = this.replace("bar","baz")`, log.Noop()) 172 require.NoError(t, err) 173 174 nm := NewNamespaced(prom).WithPrefix("foo").WithLabels("static1", "svalue1") 175 nm = nm.WithMapping(mappingBarToBaz) 176 nm = nm.WithMapping(mappingFooToBar) 177 178 ctr := nm.GetCounter("counter") 179 ctr.Incr(10) 180 ctr.Incr(11) 181 182 gge := nm.GetGauge("gauge") 183 gge.Set(12) 184 185 tmr := nm.GetTimer("timer") 186 tmr.Timing(13) 187 188 ctrTwo := nm.GetCounterVec("countertwo", []string{"label1"}) 189 ctrTwo.With("value1").Incr(10) 190 ctrTwo.With("value2").Incr(11) 191 192 ggeTwo := nm.GetGaugeVec("gaugetwo", []string{"label2"}) 193 ggeTwo.With("value3").Set(12) 194 195 tmrTwo := nm.GetTimerVec("timertwo", []string{"label3", "label4"}) 196 tmrTwo.With("value4", "value5").Timing(13) 197 198 body := getPage(t, handler) 199 200 assert.Contains(t, body, "\nbaz_counter{static1=\"svalue1\"} 21") 201 assert.Contains(t, body, "\nbaz_gauge{static1=\"svalue1\"} 12") 202 assert.Contains(t, body, "\nbaz_timer_sum{static1=\"svalue1\"} 13") 203 assert.Contains(t, body, "\nbaz_countertwo{label1=\"value1\",static1=\"svalue1\"} 10") 204 assert.Contains(t, body, "\nbaz_countertwo{label1=\"value2\",static1=\"svalue1\"} 11") 205 assert.Contains(t, body, "\nbaz_gaugetwo{label2=\"value3\",static1=\"svalue1\"} 12") 206 assert.Contains(t, body, "\nbaz_timertwo_sum{label3=\"value4\",label4=\"value5\",static1=\"svalue1\"} 13") 207 } 208 209 func TestNamespacedPrefixStaticLabelsWithMappingLabels(t *testing.T) { 210 prom, handler := getTestProm(t) 211 212 mappingFooToBar, err := NewMapping(types.NoopMgr(), `meta = meta().map_each(kv -> kv.value.replace("value","bar")) 213 meta extra1 = "extravalue1" 214 root = this.replace("foo","bar")`, log.Noop()) 215 require.NoError(t, err) 216 217 mappingBarToBaz, err := NewMapping(types.NoopMgr(), `meta = meta().map_each(kv -> kv.value.replace("bar","baz")) 218 meta extra2 = "extravalue2" 219 root = this.replace("bar","baz")`, log.Noop()) 220 require.NoError(t, err) 221 222 nm := NewNamespaced(prom).WithPrefix("foo").WithLabels("static1", "svalue1") 223 nm = nm.WithMapping(mappingBarToBaz) 224 nm = nm.WithMapping(mappingFooToBar) 225 226 ctr := nm.GetCounter("counter") 227 ctr.Incr(10) 228 ctr.Incr(11) 229 230 gge := nm.GetGauge("gauge") 231 gge.Set(12) 232 233 tmr := nm.GetTimer("timer") 234 tmr.Timing(13) 235 236 ctrTwo := nm.GetCounterVec("countertwo", []string{"label1"}) 237 ctrTwo.With("value1").Incr(10) 238 ctrTwo.With("value2").Incr(11) 239 240 ggeTwo := nm.GetGaugeVec("gaugetwo", []string{"label2"}) 241 ggeTwo.With("value3").Set(12) 242 243 tmrTwo := nm.GetTimerVec("timertwo", []string{"label3", "label4"}) 244 tmrTwo.With("value4", "value5").Timing(13) 245 246 body := getPage(t, handler) 247 248 assert.Contains(t, body, "\nbaz_counter{extra1=\"extravalue1\",extra2=\"extravalue2\",static1=\"sbaz1\"} 21") 249 assert.Contains(t, body, "\nbaz_gauge{extra1=\"extravalue1\",extra2=\"extravalue2\",static1=\"sbaz1\"} 12") 250 assert.Contains(t, body, "\nbaz_timer_sum{extra1=\"extravalue1\",extra2=\"extravalue2\",static1=\"sbaz1\"} 13") 251 assert.Contains(t, body, "\nbaz_countertwo{extra1=\"extravalue1\",extra2=\"extravalue2\",label1=\"value1\",static1=\"sbaz1\"} 10") 252 assert.Contains(t, body, "\nbaz_countertwo{extra1=\"extravalue1\",extra2=\"extravalue2\",label1=\"value2\",static1=\"sbaz1\"} 11") 253 assert.Contains(t, body, "\nbaz_gaugetwo{extra1=\"extravalue1\",extra2=\"extravalue2\",label2=\"value3\",static1=\"sbaz1\"} 12") 254 assert.Contains(t, body, "\nbaz_timertwo_sum{extra1=\"extravalue1\",extra2=\"extravalue2\",label3=\"value4\",label4=\"value5\",static1=\"sbaz1\"} 13") 255 }