MCPcopy Create free account
hub / github.com/prometheus/common / TestNegotiateAcceptEncodeUTF8Names

Function TestNegotiateAcceptEncodeUTF8Names

expfmt/encode_test.go:695–763  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

693}
694
695func TestNegotiateAcceptEncodeUTF8Names(t *testing.T) {
696 mf := &dto.MetricFamily{
697 Name: proto.String("my.metric"),
698 Type: dto.MetricType_GAUGE.Enum(),
699 Metric: []*dto.Metric{
700 {
701 Label: []*dto.LabelPair{{Name: proto.String("my.label"), Value: proto.String("v")}},
702 Gauge: &dto.Gauge{Value: proto.Float64(1)},
703 },
704 },
705 }
706
707 tests := []struct {
708 name string
709 accept string
710 expected string
711 }{
712 {
713 name: "OM 2.0-rc without escaping keeps UTF-8 names",
714 accept: "application/openmetrics-text;version=2.0.0-rc",
715 expected: `{"my.metric","my.label"="v"} 1.0`,
716 },
717 {
718 name: "OM 2.0-rc with escaping=underscores escapes",
719 accept: "application/openmetrics-text;version=2.0.0-rc;escaping=underscores",
720 expected: `my_metric{my_label="v"} 1.0`,
721 },
722 {
723 name: "OM 2.0 without escaping keeps UTF-8 names",
724 accept: "application/openmetrics-text;version=2.0.0",
725 expected: `{"my.metric","my.label"="v"} 1.0`,
726 },
727 {
728 name: "OM 2.0 with escaping=underscores escapes",
729 accept: "application/openmetrics-text;version=2.0.0;escaping=underscores",
730 expected: `my_metric{my_label="v"} 1.0`,
731 },
732 {
733 name: "OM 1.0 without escaping escapes",
734 accept: "application/openmetrics-text;version=1.0.0",
735 expected: `my_metric{my_label="v"} 1.0`,
736 },
737 {
738 name: "text without escaping escapes",
739 accept: "text/plain;version=0.0.4",
740 expected: `my_metric{my_label="v"} 1`,
741 },
742 }
743
744 oldDefault := model.NameEscapingScheme
745 model.NameEscapingScheme = model.UnderscoreEscaping
746 defer func() {
747 model.NameEscapingScheme = oldDefault
748 }()
749
750 for _, tt := range tests {
751 t.Run(tt.name, func(t *testing.T) {
752 h := http.Header{}

Callers

nothing calls this directly

Calls 6

EncodeMethod · 0.95
NegotiateAcceptFunction · 0.85
NewEncoderFunction · 0.85
AddMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected