@@ -21,9 +21,11 @@ package prometheus_test
21
21
22
22
import (
23
23
"bytes"
24
+ "io/ioutil"
24
25
"math/rand"
25
26
"net/http"
26
27
"net/http/httptest"
28
+ "os"
27
29
"sync"
28
30
"testing"
29
31
"time"
@@ -871,3 +873,102 @@ func TestHistogramVecRegisterGatherConcurrency(t *testing.T) {
871
873
close (quit )
872
874
wg .Wait ()
873
875
}
876
+
877
+ func TestWriteToTextfile (t * testing.T ) {
878
+ expectedOut := `# HELP test_counter test counter
879
+ # TYPE test_counter counter
880
+ test_counter{name="qux"} 1
881
+ # HELP test_gauge test gauge
882
+ # TYPE test_gauge gauge
883
+ test_gauge{name="baz"} 1.1
884
+ # HELP test_hist test histogram
885
+ # TYPE test_hist histogram
886
+ test_hist_bucket{name="bar",le="0.005"} 0
887
+ test_hist_bucket{name="bar",le="0.01"} 0
888
+ test_hist_bucket{name="bar",le="0.025"} 0
889
+ test_hist_bucket{name="bar",le="0.05"} 0
890
+ test_hist_bucket{name="bar",le="0.1"} 0
891
+ test_hist_bucket{name="bar",le="0.25"} 0
892
+ test_hist_bucket{name="bar",le="0.5"} 0
893
+ test_hist_bucket{name="bar",le="1"} 1
894
+ test_hist_bucket{name="bar",le="2.5"} 1
895
+ test_hist_bucket{name="bar",le="5"} 2
896
+ test_hist_bucket{name="bar",le="10"} 2
897
+ test_hist_bucket{name="bar",le="+Inf"} 2
898
+ test_hist_sum{name="bar"} 3.64
899
+ test_hist_count{name="bar"} 2
900
+ # HELP test_summary test summary
901
+ # TYPE test_summary summary
902
+ test_summary{name="foo",quantile="0.5"} 10
903
+ test_summary{name="foo",quantile="0.9"} 20
904
+ test_summary{name="foo",quantile="0.99"} 20
905
+ test_summary_sum{name="foo"} 30
906
+ test_summary_count{name="foo"} 2
907
+ `
908
+
909
+ registry := prometheus .NewRegistry ()
910
+
911
+ summary := prometheus .NewSummaryVec (
912
+ prometheus.SummaryOpts {
913
+ Name : "test_summary" ,
914
+ Help : "test summary" ,
915
+ },
916
+ []string {"name" },
917
+ )
918
+
919
+ histogram := prometheus .NewHistogramVec (
920
+ prometheus.HistogramOpts {
921
+ Name : "test_hist" ,
922
+ Help : "test histogram" ,
923
+ },
924
+ []string {"name" },
925
+ )
926
+
927
+ gauge := prometheus .NewGaugeVec (
928
+ prometheus.GaugeOpts {
929
+ Name : "test_gauge" ,
930
+ Help : "test gauge" ,
931
+ },
932
+ []string {"name" },
933
+ )
934
+
935
+ counter := prometheus .NewCounterVec (
936
+ prometheus.CounterOpts {
937
+ Name : "test_counter" ,
938
+ Help : "test counter" ,
939
+ },
940
+ []string {"name" },
941
+ )
942
+
943
+ registry .MustRegister (summary )
944
+ registry .MustRegister (histogram )
945
+ registry .MustRegister (gauge )
946
+ registry .MustRegister (counter )
947
+
948
+ summary .With (prometheus.Labels {"name" : "foo" }).Observe (10 )
949
+ summary .With (prometheus.Labels {"name" : "foo" }).Observe (20 )
950
+ histogram .With (prometheus.Labels {"name" : "bar" }).Observe (0.93 )
951
+ histogram .With (prometheus.Labels {"name" : "bar" }).Observe (2.71 )
952
+ gauge .With (prometheus.Labels {"name" : "baz" }).Set (1.1 )
953
+ counter .With (prometheus.Labels {"name" : "qux" }).Inc ()
954
+
955
+ tmpfile , err := ioutil .TempFile ("" , "prom_registry_test" )
956
+ if err != nil {
957
+ t .Fatal (err )
958
+ }
959
+ defer os .Remove (tmpfile .Name ())
960
+
961
+ if err := prometheus .WriteToTextfile (tmpfile .Name (), registry ); err != nil {
962
+ t .Fatal (err )
963
+ }
964
+
965
+ fileBytes , err := ioutil .ReadFile (tmpfile .Name ())
966
+ if err != nil {
967
+ t .Fatal (err )
968
+ }
969
+ fileContents := string (fileBytes )
970
+
971
+ if fileContents != expectedOut {
972
+ t .Error ("file contents didn't match unexpected" )
973
+ }
974
+ }
0 commit comments