File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
plugins/outputs/prometheus_client Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -242,6 +242,15 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
242242 labels [sanitize (k )] = v
243243 }
244244
245+ // Prometheus doesn't have a string value type, so convert string
246+ // fields to labels.
247+ for fn , fv := range point .Fields () {
248+ switch fv := fv .(type ) {
249+ case string :
250+ labels [sanitize (fn )] = fv
251+ }
252+ }
253+
245254 for fn , fv := range point .Fields () {
246255 // Ignore string and bool fields.
247256 var value float64
Original file line number Diff line number Diff line change @@ -324,6 +324,34 @@ func TestWrite_Tags(t *testing.T) {
324324 require .True (t , now .Before (sample2 .Expiration ))
325325}
326326
327+ func TestWrite_StringFields (t * testing.T ) {
328+ now := time .Now ()
329+ p1 , err := metric .New (
330+ "foo" ,
331+ make (map [string ]string ),
332+ map [string ]interface {}{"value" : 1.0 , "status" : "good" },
333+ now ,
334+ telegraf .Counter )
335+ p2 , err := metric .New (
336+ "bar" ,
337+ make (map [string ]string ),
338+ map [string ]interface {}{"status" : "needs numeric field" },
339+ now ,
340+ telegraf .Gauge )
341+ var metrics = []telegraf.Metric {p1 , p2 }
342+
343+ client := NewClient ()
344+ err = client .Write (metrics )
345+ require .NoError (t , err )
346+
347+ fam , ok := client .fam ["foo" ]
348+ require .True (t , ok )
349+ require .Equal (t , 1 , fam .LabelSet ["status" ])
350+
351+ fam , ok = client .fam ["bar" ]
352+ require .False (t , ok )
353+ }
354+
327355func TestExpire (t * testing.T ) {
328356 client := NewClient ()
329357
You can’t perform that action at this time.
0 commit comments