Move metric type definitions to common/model

They are used in multiple repos, so common is a better place for them.
Several packages now don't depend on `model/textparse`, e.g.
`storage/remote`.

Also remove `metadata` struct from `api.go`, since it was identical to
a struct in the `metadata` package.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-11-22 14:39:21 +00:00
parent 99c17b4319
commit 8065bef172
21 changed files with 185 additions and 197 deletions

2
go.mod
View file

@ -48,7 +48,7 @@ require (
github.com/prometheus/alertmanager v0.26.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.45.0
github.com/prometheus/common v0.45.1-0.20231122191551-832cd6e99f99
github.com/prometheus/common/assets v0.2.0
github.com/prometheus/common/sigv4 v0.1.0
github.com/prometheus/exporter-toolkit v0.10.0

4
go.sum
View file

@ -653,8 +653,8 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
github.com/prometheus/common v0.45.1-0.20231122191551-832cd6e99f99 h1:V5ajRiLiCQGO+ggTr+07gMUcTqlIMMkDBfrJe5zKLmc=
github.com/prometheus/common v0.45.1-0.20231122191551-832cd6e99f99/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
github.com/prometheus/common/assets v0.2.0 h1:0P5OrzoHrYBOSM1OigWL3mY8ZvV2N4zIE/5AahrSrfM=
github.com/prometheus/common/assets v0.2.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI=
github.com/prometheus/common/sigv4 v0.1.0 h1:qoVebwtwwEhS85Czm2dSROY5fTo2PAPEVdDeppTwGX4=

View file

@ -13,11 +13,11 @@
package metadata
import "github.com/prometheus/prometheus/model/textparse"
import "github.com/prometheus/common/model"
// Metadata stores a series' metadata information.
type Metadata struct {
Type textparse.MetricType
Type model.MetricType
Unit string
Help string
}

View file

@ -16,6 +16,8 @@ package textparse
import (
"mime"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
@ -110,16 +112,4 @@ const (
EntryHistogram Entry = 5 // A series with a native histogram as a value.
)
// MetricType represents metric type values.
type MetricType string
const (
MetricTypeCounter = MetricType("counter")
MetricTypeGauge = MetricType("gauge")
MetricTypeHistogram = MetricType("histogram")
MetricTypeGaugeHistogram = MetricType("gaugehistogram")
MetricTypeSummary = MetricType("summary")
MetricTypeInfo = MetricType("info")
MetricTypeStateset = MetricType("stateset")
MetricTypeUnknown = MetricType("unknown")
)
type MetricType = model.MetricType

View file

@ -24,6 +24,8 @@ import (
"strings"
"unicode/utf8"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
@ -74,7 +76,7 @@ type OpenMetricsParser struct {
builder labels.ScratchBuilder
series []byte
text []byte
mtype MetricType
mtype model.MetricType
val float64
ts int64
hasTS bool
@ -272,21 +274,21 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
case tType:
switch s := yoloString(p.text); s {
case "counter":
p.mtype = MetricTypeCounter
p.mtype = model.MetricTypeCounter
case "gauge":
p.mtype = MetricTypeGauge
p.mtype = model.MetricTypeGauge
case "histogram":
p.mtype = MetricTypeHistogram
p.mtype = model.MetricTypeHistogram
case "gaugehistogram":
p.mtype = MetricTypeGaugeHistogram
p.mtype = model.MetricTypeGaugeHistogram
case "summary":
p.mtype = MetricTypeSummary
p.mtype = model.MetricTypeSummary
case "info":
p.mtype = MetricTypeInfo
p.mtype = model.MetricTypeInfo
case "stateset":
p.mtype = MetricTypeStateset
p.mtype = model.MetricTypeStateset
case "unknown":
p.mtype = MetricTypeUnknown
p.mtype = model.MetricTypeUnknown
default:
return EntryInvalid, fmt.Errorf("invalid metric type %q", s)
}

View file

@ -18,6 +18,7 @@ import (
"io"
"testing"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/exemplar"
@ -77,7 +78,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
m string
t *int64
v float64
typ MetricType
typ model.MetricType
help string
unit string
comment string
@ -88,7 +89,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
help: "A summary of the GC invocation durations.",
}, {
m: "go_gc_duration_seconds",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
}, {
m: "go_gc_duration_seconds",
unit: "seconds",
@ -130,7 +131,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
help: "Number of goroutines that currently exist.",
}, {
m: "go_goroutines",
typ: MetricTypeGauge,
typ: model.MetricTypeGauge,
}, {
m: `go_goroutines`,
v: 33,
@ -138,21 +139,21 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
lset: labels.FromStrings("__name__", "go_goroutines"),
}, {
m: "hh",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
}, {
m: `hh_bucket{le="+Inf"}`,
v: 1,
lset: labels.FromStrings("__name__", "hh_bucket", "le", "+Inf"),
}, {
m: "gh",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
}, {
m: `gh_bucket{le="+Inf"}`,
v: 1,
lset: labels.FromStrings("__name__", "gh_bucket", "le", "+Inf"),
}, {
m: "hhh",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
}, {
m: `hhh_bucket{le="+Inf"}`,
v: 1,
@ -165,7 +166,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
e: &exemplar.Exemplar{Labels: labels.FromStrings("id", "histogram-count-test"), Value: 4},
}, {
m: "ggh",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
}, {
m: `ggh_bucket{le="+Inf"}`,
v: 1,
@ -178,7 +179,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
e: &exemplar.Exemplar{Labels: labels.FromStrings("id", "gaugehistogram-count-test", "xx", "yy"), Value: 4, HasTs: true, Ts: 123123},
}, {
m: "smr_seconds",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
}, {
m: `smr_seconds_count`,
v: 2,
@ -191,14 +192,14 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
e: &exemplar.Exemplar{Labels: labels.FromStrings("id", "summary-sum-test"), Value: 1, HasTs: true, Ts: 123321},
}, {
m: "ii",
typ: MetricTypeInfo,
typ: model.MetricTypeInfo,
}, {
m: `ii{foo="bar"}`,
v: 1,
lset: labels.FromStrings("__name__", "ii", "foo", "bar"),
}, {
m: "ss",
typ: MetricTypeStateset,
typ: model.MetricTypeStateset,
}, {
m: `ss{ss="foo"}`,
v: 1,
@ -213,7 +214,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
lset: labels.FromStrings("A", "a", "__name__", "ss"),
}, {
m: "un",
typ: MetricTypeUnknown,
typ: model.MetricTypeUnknown,
}, {
m: "_metric_starting_with_underscore",
v: 1,
@ -228,7 +229,7 @@ foo_total 17.0 1520879607.789 # {id="counter-test"} 5`
lset: labels.FromStrings("__name__", "testmetric", "label", `"bar"`),
}, {
m: "foo",
typ: MetricTypeCounter,
typ: model.MetricTypeCounter,
}, {
m: "foo_total",
v: 17,

View file

@ -26,6 +26,8 @@ import (
"unicode/utf8"
"unsafe"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
@ -305,15 +307,15 @@ func (p *PromParser) Next() (Entry, error) {
case tType:
switch s := yoloString(p.text); s {
case "counter":
p.mtype = MetricTypeCounter
p.mtype = model.MetricTypeCounter
case "gauge":
p.mtype = MetricTypeGauge
p.mtype = model.MetricTypeGauge
case "histogram":
p.mtype = MetricTypeHistogram
p.mtype = model.MetricTypeHistogram
case "summary":
p.mtype = MetricTypeSummary
p.mtype = model.MetricTypeSummary
case "untyped":
p.mtype = MetricTypeUnknown
p.mtype = model.MetricTypeUnknown
default:
return EntryInvalid, fmt.Errorf("invalid metric type %q", s)
}

View file

@ -65,7 +65,7 @@ testmetric{label="\"bar\""} 1`
m string
t *int64
v float64
typ MetricType
typ model.MetricType
help string
comment string
}{
@ -74,7 +74,7 @@ testmetric{label="\"bar\""} 1`
help: "A summary of the GC invocation durations.",
}, {
m: "go_gc_duration_seconds",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
}, {
m: `go_gc_duration_seconds{quantile="0"}`,
v: 4.9351e-05,
@ -142,7 +142,7 @@ testmetric{label="\"bar\""} 1`
help: "Number of goroutines that currently exist.",
}, {
m: "go_goroutines",
typ: MetricTypeGauge,
typ: model.MetricTypeGauge,
}, {
m: `go_goroutines`,
v: 33,

View file

@ -252,21 +252,21 @@ func (p *ProtobufParser) Help() ([]byte, []byte) {
// Type returns the metric name and type in the current entry.
// Must only be called after Next returned a type entry.
// The returned byte slices become invalid after the next call to Next.
func (p *ProtobufParser) Type() ([]byte, MetricType) {
func (p *ProtobufParser) Type() ([]byte, model.MetricType) {
n := p.metricBytes.Bytes()
switch p.mf.GetType() {
case dto.MetricType_COUNTER:
return n, MetricTypeCounter
return n, model.MetricTypeCounter
case dto.MetricType_GAUGE:
return n, MetricTypeGauge
return n, model.MetricTypeGauge
case dto.MetricType_HISTOGRAM:
return n, MetricTypeHistogram
return n, model.MetricTypeHistogram
case dto.MetricType_GAUGE_HISTOGRAM:
return n, MetricTypeGaugeHistogram
return n, model.MetricTypeGaugeHistogram
case dto.MetricType_SUMMARY:
return n, MetricTypeSummary
return n, model.MetricTypeSummary
}
return n, MetricTypeUnknown
return n, model.MetricTypeUnknown
}
// Unit always returns (nil, nil) because units aren't supported by the protobuf

View file

@ -21,6 +21,7 @@ import (
"testing"
"github.com/gogo/protobuf/proto"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/exemplar"
@ -649,7 +650,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "go_build_info",
typ: MetricTypeGauge,
typ: model.MetricTypeGauge,
},
{
m: "go_build_info\xFFchecksum\xFF\xFFpath\xFFgithub.com/prometheus/client_golang\xFFversion\xFF(devel)",
@ -667,7 +668,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "go_memstats_alloc_bytes_total",
typ: MetricTypeCounter,
typ: model.MetricTypeCounter,
},
{
m: "go_memstats_alloc_bytes_total",
@ -685,7 +686,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "something_untyped",
typ: MetricTypeUnknown,
typ: model.MetricTypeUnknown,
},
{
m: "something_untyped",
@ -701,7 +702,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_histogram",
@ -736,7 +737,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_gauge_histogram",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{
m: "test_gauge_histogram",
@ -772,7 +773,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_float_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_float_histogram",
@ -807,7 +808,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_gauge_float_histogram",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{
m: "test_gauge_float_histogram",
@ -843,7 +844,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_histogram2",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_histogram2_count",
@ -903,7 +904,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_histogram_family",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_histogram_family\xfffoo\xffbar",
@ -947,7 +948,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_float_histogram_with_zerothreshold_zero",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_float_histogram_with_zerothreshold_zero",
@ -971,7 +972,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "rpc_durations_seconds",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{
m: "rpc_durations_seconds_count\xffservice\xffexponential",
@ -1022,7 +1023,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "without_quantiles",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{
m: "without_quantiles_count",
@ -1044,7 +1045,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "empty_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "empty_histogram",
@ -1063,7 +1064,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_counter_with_createdtimestamp",
typ: MetricTypeCounter,
typ: model.MetricTypeCounter,
},
{
m: "test_counter_with_createdtimestamp",
@ -1079,7 +1080,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_summary_with_createdtimestamp",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{
m: "test_summary_with_createdtimestamp_count",
@ -1103,7 +1104,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_histogram_with_createdtimestamp",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{
m: "test_histogram_with_createdtimestamp",
@ -1123,7 +1124,7 @@ func TestProtobufParse(t *testing.T) {
},
{
m: "test_gaugehistogram_with_createdtimestamp",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{
m: "test_gaugehistogram_with_createdtimestamp",
@ -1149,7 +1150,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 1
m: "go_build_info",
typ: MetricTypeGauge,
typ: model.MetricTypeGauge,
},
{ // 2
m: "go_build_info\xFFchecksum\xFF\xFFpath\xFFgithub.com/prometheus/client_golang\xFFversion\xFF(devel)",
@ -1167,7 +1168,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 4
m: "go_memstats_alloc_bytes_total",
typ: MetricTypeCounter,
typ: model.MetricTypeCounter,
},
{ // 5
m: "go_memstats_alloc_bytes_total",
@ -1185,7 +1186,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 7
m: "something_untyped",
typ: MetricTypeUnknown,
typ: model.MetricTypeUnknown,
},
{ // 8
m: "something_untyped",
@ -1201,7 +1202,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 10
m: "test_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 11
m: "test_histogram",
@ -1294,7 +1295,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 19
m: "test_gauge_histogram",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{ // 20
m: "test_gauge_histogram",
@ -1388,7 +1389,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 28
m: "test_float_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 29
m: "test_float_histogram",
@ -1481,7 +1482,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 37
m: "test_gauge_float_histogram",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{ // 38
m: "test_gauge_float_histogram",
@ -1575,7 +1576,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 46
m: "test_histogram2",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 47
m: "test_histogram2_count",
@ -1635,7 +1636,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 54
m: "test_histogram_family",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 55
m: "test_histogram_family\xfffoo\xffbar",
@ -1765,7 +1766,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 68
m: "test_float_histogram_with_zerothreshold_zero",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 69
m: "test_float_histogram_with_zerothreshold_zero",
@ -1789,7 +1790,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 71
m: "rpc_durations_seconds",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{ // 72
m: "rpc_durations_seconds_count\xffservice\xffexponential",
@ -1840,7 +1841,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 78
m: "without_quantiles",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{ // 79
m: "without_quantiles_count",
@ -1862,7 +1863,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 79
m: "empty_histogram",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 80
m: "empty_histogram",
@ -1881,7 +1882,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 82
m: "test_counter_with_createdtimestamp",
typ: MetricTypeCounter,
typ: model.MetricTypeCounter,
},
{ // 83
m: "test_counter_with_createdtimestamp",
@ -1897,7 +1898,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 85
m: "test_summary_with_createdtimestamp",
typ: MetricTypeSummary,
typ: model.MetricTypeSummary,
},
{ // 86
m: "test_summary_with_createdtimestamp_count",
@ -1921,7 +1922,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 89
m: "test_histogram_with_createdtimestamp",
typ: MetricTypeHistogram,
typ: model.MetricTypeHistogram,
},
{ // 90
m: "test_histogram_with_createdtimestamp",
@ -1941,7 +1942,7 @@ func TestProtobufParse(t *testing.T) {
},
{ // 92
m: "test_gaugehistogram_with_createdtimestamp",
typ: MetricTypeGaugeHistogram,
typ: model.MetricTypeGaugeHistogram,
},
{ // 93
m: "test_gaugehistogram_with_createdtimestamp",

View file

@ -966,7 +966,7 @@ func (c *scrapeCache) setType(metric []byte, t textparse.MetricType) {
e, ok := c.metadata[string(metric)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: textparse.MetricTypeUnknown}}
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(metric)] = e
}
if e.Type != t {
@ -983,7 +983,7 @@ func (c *scrapeCache) setHelp(metric, help []byte) {
e, ok := c.metadata[string(metric)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: textparse.MetricTypeUnknown}}
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(metric)] = e
}
if e.Help != string(help) {
@ -1000,7 +1000,7 @@ func (c *scrapeCache) setUnit(metric, unit []byte) {
e, ok := c.metadata[string(metric)]
if !ok {
e = &metaEntry{Metadata: metadata.Metadata{Type: textparse.MetricTypeUnknown}}
e = &metaEntry{Metadata: metadata.Metadata{Type: model.MetricTypeUnknown}}
c.metadata[string(metric)] = e
}
if e.Unit != string(unit) {

View file

@ -971,19 +971,19 @@ test_metric 1
md, ok := cache.GetMetadata("test_metric")
require.True(t, ok, "expected metadata to be present")
require.Equal(t, textparse.MetricTypeCounter, md.Type, "unexpected metric type")
require.Equal(t, model.MetricTypeCounter, md.Type, "unexpected metric type")
require.Equal(t, "some help text", md.Help)
require.Equal(t, "metric", md.Unit)
md, ok = cache.GetMetadata("test_metric_no_help")
require.True(t, ok, "expected metadata to be present")
require.Equal(t, textparse.MetricTypeGauge, md.Type, "unexpected metric type")
require.Equal(t, model.MetricTypeGauge, md.Type, "unexpected metric type")
require.Equal(t, "", md.Help)
require.Equal(t, "", md.Unit)
md, ok = cache.GetMetadata("test_metric_no_type")
require.True(t, ok, "expected metadata to be present")
require.Equal(t, textparse.MetricTypeUnknown, md.Type, "unexpected metric type")
require.Equal(t, model.MetricTypeUnknown, md.Type, "unexpected metric type")
require.Equal(t, "other help text", md.Help)
require.Equal(t, "", md.Unit)
}

View file

@ -33,7 +33,6 @@ import (
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
@ -784,7 +783,7 @@ func labelsToLabelsProto(lbls labels.Labels, buf []prompb.Label) []prompb.Label
}
// metricTypeToMetricTypeProto transforms a Prometheus metricType into prompb metricType. Since the former is a string we need to transform it to an enum.
func metricTypeToMetricTypeProto(t textparse.MetricType) prompb.MetricMetadata_MetricType {
func metricTypeToMetricTypeProto(t model.MetricType) prompb.MetricMetadata_MetricType {
mt := strings.ToUpper(string(t))
v, ok := prompb.MetricMetadata_MetricType_value[mt]
if !ok {

View file

@ -20,11 +20,11 @@ import (
"testing"
"github.com/gogo/protobuf/proto"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
@ -488,17 +488,17 @@ func TestMergeLabels(t *testing.T) {
func TestMetricTypeToMetricTypeProto(t *testing.T) {
tc := []struct {
desc string
input textparse.MetricType
input model.MetricType
expected prompb.MetricMetadata_MetricType
}{
{
desc: "with a single-word metric",
input: textparse.MetricTypeCounter,
input: model.MetricTypeCounter,
expected: prompb.MetricMetadata_COUNTER,
},
{
desc: "with a two-word metric",
input: textparse.MetricTypeStateset,
input: model.MetricTypeStateset,
expected: prompb.MetricMetadata_STATESET,
},
{

View file

@ -22,7 +22,6 @@ import (
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/scrape"
)
@ -108,13 +107,13 @@ func TestWatchScrapeManager_ReadyForCollection(t *testing.T) {
Metadata: []scrape.MetricMetadata{
{
Metric: "prometheus_tsdb_head_chunks_created_total",
Type: textparse.MetricTypeCounter,
Type: model.MetricTypeCounter,
Help: "Total number",
Unit: "",
},
{
Metric: "prometheus_remote_storage_retried_samples_total",
Type: textparse.MetricTypeCounter,
Type: model.MetricTypeCounter,
Help: "Total number",
Unit: "",
},
@ -124,7 +123,7 @@ func TestWatchScrapeManager_ReadyForCollection(t *testing.T) {
Metadata: []scrape.MetricMetadata{
{
Metric: "prometheus_tsdb_head_chunks_created_total",
Type: textparse.MetricTypeCounter,
Type: model.MetricTypeCounter,
Help: "Total number",
Unit: "",
},

View file

@ -38,7 +38,6 @@ import (
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/scrape"
@ -180,7 +179,7 @@ func TestMetadataDelivery(t *testing.T) {
for i := 0; i < numMetadata; i++ {
metadata = append(metadata, scrape.MetricMetadata{
Metric: "prometheus_remote_storage_sent_metadata_bytes_total_" + strconv.Itoa(i),
Type: textparse.MetricTypeCounter,
Type: model.MetricTypeCounter,
Help: "a nice help text",
Unit: "",
})

View file

@ -1038,7 +1038,7 @@ func (a *headAppender) Commit() (err error) {
for i, m := range a.metadata {
series = a.metadataSeries[i]
series.Lock()
series.meta = &metadata.Metadata{Type: record.ToTextparseMetricType(m.Type), Unit: m.Unit, Help: m.Help}
series.meta = &metadata.Metadata{Type: record.ToMetricType(m.Type), Unit: m.Unit, Help: m.Help}
series.Unlock()
}

View file

@ -388,7 +388,7 @@ Outer:
continue
}
s.meta = &metadata.Metadata{
Type: record.ToTextparseMetricType(m.Type),
Type: record.ToMetricType(m.Type),
Unit: m.Unit,
Help: m.Help,
}

View file

@ -20,9 +20,10 @@ import (
"fmt"
"math"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/encoding"
@ -90,45 +91,45 @@ const (
Stateset MetricType = 7
)
func GetMetricType(t textparse.MetricType) uint8 {
func GetMetricType(t model.MetricType) uint8 {
switch t {
case textparse.MetricTypeCounter:
case model.MetricTypeCounter:
return uint8(Counter)
case textparse.MetricTypeGauge:
case model.MetricTypeGauge:
return uint8(Gauge)
case textparse.MetricTypeHistogram:
case model.MetricTypeHistogram:
return uint8(HistogramSample)
case textparse.MetricTypeGaugeHistogram:
case model.MetricTypeGaugeHistogram:
return uint8(GaugeHistogram)
case textparse.MetricTypeSummary:
case model.MetricTypeSummary:
return uint8(Summary)
case textparse.MetricTypeInfo:
case model.MetricTypeInfo:
return uint8(Info)
case textparse.MetricTypeStateset:
case model.MetricTypeStateset:
return uint8(Stateset)
default:
return uint8(UnknownMT)
}
}
func ToTextparseMetricType(m uint8) textparse.MetricType {
func ToMetricType(m uint8) model.MetricType {
switch m {
case uint8(Counter):
return textparse.MetricTypeCounter
return model.MetricTypeCounter
case uint8(Gauge):
return textparse.MetricTypeGauge
return model.MetricTypeGauge
case uint8(HistogramSample):
return textparse.MetricTypeHistogram
return model.MetricTypeHistogram
case uint8(GaugeHistogram):
return textparse.MetricTypeGaugeHistogram
return model.MetricTypeGaugeHistogram
case uint8(Summary):
return textparse.MetricTypeSummary
return model.MetricTypeSummary
case uint8(Info):
return textparse.MetricTypeInfo
return model.MetricTypeInfo
case uint8(Stateset):
return textparse.MetricTypeStateset
return model.MetricTypeStateset
default:
return textparse.MetricTypeUnknown
return model.MetricTypeUnknown
}
}

View file

@ -41,7 +41,7 @@ import (
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/model/metadata"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
@ -1141,11 +1141,11 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
}
type metricMetadata struct {
Target labels.Labels `json:"target"`
Metric string `json:"metric,omitempty"`
Type textparse.MetricType `json:"type"`
Help string `json:"help"`
Unit string `json:"unit"`
Target labels.Labels `json:"target"`
Metric string `json:"metric,omitempty"`
Type model.MetricType `json:"type"`
Help string `json:"help"`
Unit string `json:"unit"`
}
// AlertmanagerDiscovery has all the active Alertmanagers.
@ -1221,14 +1221,8 @@ func rulesAlertsToAPIAlerts(rulesAlerts []*rules.Alert) []*Alert {
return apiAlerts
}
type metadata struct {
Type textparse.MetricType `json:"type"`
Help string `json:"help"`
Unit string `json:"unit"`
}
func (api *API) metricMetadata(r *http.Request) apiFuncResult {
metrics := map[string]map[metadata]struct{}{}
metrics := map[string]map[metadata.Metadata]struct{}{}
limit := -1
if s := r.FormValue("limit"); s != "" {
@ -1250,7 +1244,7 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
for _, t := range tt {
if metric == "" {
for _, mm := range t.ListMetadata() {
m := metadata{Type: mm.Type, Help: mm.Help, Unit: mm.Unit}
m := metadata.Metadata{Type: mm.Type, Help: mm.Help, Unit: mm.Unit}
ms, ok := metrics[mm.Metric]
if limitPerMetric > 0 && len(ms) >= limitPerMetric {
@ -1258,7 +1252,7 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
}
if !ok {
ms = map[metadata]struct{}{}
ms = map[metadata.Metadata]struct{}{}
metrics[mm.Metric] = ms
}
ms[m] = struct{}{}
@ -1267,7 +1261,7 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
}
if md, ok := t.GetMetadata(metric); ok {
m := metadata{Type: md.Type, Help: md.Help, Unit: md.Unit}
m := metadata.Metadata{Type: md.Type, Help: md.Help, Unit: md.Unit}
ms, ok := metrics[md.Metric]
if limitPerMetric > 0 && len(ms) >= limitPerMetric {
@ -1275,7 +1269,7 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
}
if !ok {
ms = map[metadata]struct{}{}
ms = map[metadata.Metadata]struct{}{}
metrics[md.Metric] = ms
}
ms[m] = struct{}{}
@ -1284,13 +1278,13 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
}
// Put the elements from the pseudo-set into a slice for marshaling.
res := map[string][]metadata{}
res := map[string][]metadata.Metadata{}
for name, set := range metrics {
if limit >= 0 && len(res) >= limit {
break
}
s := []metadata{}
s := []metadata.Metadata{}
for metadata := range set {
s = append(s, metadata)
}

View file

@ -44,7 +44,7 @@ import (
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/model/metadata"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
@ -1584,7 +1584,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created.",
Unit: "",
},
@ -1597,7 +1597,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
"job": "test",
}),
Help: "Number of OS threads created.",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Unit: "",
},
},
@ -1614,7 +1614,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "prometheus_tsdb_storage_blocks_bytes",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "The number of bytes that are currently used for local storage by all blocks.",
Unit: "",
},
@ -1628,7 +1628,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
}),
Metric: "prometheus_tsdb_storage_blocks_bytes",
Help: "The number of bytes that are currently used for local storage by all blocks.",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Unit: "",
},
},
@ -1642,7 +1642,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created.",
Unit: "",
},
@ -1653,7 +1653,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "prometheus_tsdb_storage_blocks_bytes",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "The number of bytes that are currently used for local storage by all blocks.",
Unit: "",
},
@ -1667,7 +1667,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
}),
Metric: "go_threads",
Help: "Number of OS threads created.",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Unit: "",
},
{
@ -1676,7 +1676,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
}),
Metric: "prometheus_tsdb_storage_blocks_bytes",
Help: "The number of bytes that are currently used for local storage by all blocks.",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Unit: "",
},
},
@ -1719,22 +1719,22 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "prometheus_engine_query_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "Query timings",
Unit: "",
},
{
Metric: "go_info",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Information about the Go environment.",
Unit: "",
},
},
},
},
response: map[string][]metadata{
"prometheus_engine_query_duration_seconds": {{textparse.MetricTypeSummary, "Query timings", ""}},
"go_info": {{textparse.MetricTypeGauge, "Information about the Go environment.", ""}},
response: map[string][]metadata.Metadata{
"prometheus_engine_query_duration_seconds": {{Type: model.MetricTypeSummary, Help: "Query timings", Unit: ""}},
"go_info": {{Type: model.MetricTypeGauge, Help: "Information about the Go environment.", Unit: ""}},
},
},
// With duplicate metadata for a metric that comes from different targets.
@ -1746,7 +1746,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
@ -1757,15 +1757,15 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
},
},
},
response: map[string][]metadata{
"go_threads": {{textparse.MetricTypeGauge, "Number of OS threads created", ""}},
response: map[string][]metadata.Metadata{
"go_threads": {{Type: model.MetricTypeGauge, Help: "Number of OS threads created"}},
},
},
// With non-duplicate metadata for the same metric from different targets.
@ -1777,7 +1777,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
@ -1788,21 +1788,21 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads that were created.",
Unit: "",
},
},
},
},
response: map[string][]metadata{
response: map[string][]metadata.Metadata{
"go_threads": {
{textparse.MetricTypeGauge, "Number of OS threads created", ""},
{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
{Type: model.MetricTypeGauge, Help: "Number of OS threads created"},
{Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."},
},
},
sorter: func(m interface{}) {
v := m.(map[string][]metadata)["go_threads"]
v := m.(map[string][]metadata.Metadata)["go_threads"]
sort.Slice(v, func(i, j int) bool {
return v[i].Help < v[j].Help
@ -1821,13 +1821,13 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
{
Metric: "prometheus_engine_query_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "Query Timmings.",
Unit: "",
},
@ -1838,7 +1838,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations.",
Unit: "",
},
@ -1857,31 +1857,31 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Repeated metadata",
Unit: "",
},
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations.",
Unit: "",
},
},
},
},
response: map[string][]metadata{
response: map[string][]metadata.Metadata{
"go_threads": {
{textparse.MetricTypeGauge, "Number of OS threads created", ""},
{Type: model.MetricTypeGauge, Help: "Number of OS threads created"},
},
"go_gc_duration_seconds": {
{textparse.MetricTypeSummary, "A summary of the GC invocation durations.", ""},
{Type: model.MetricTypeSummary, Help: "A summary of the GC invocation durations."},
},
},
},
@ -1895,19 +1895,19 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Repeated metadata",
Unit: "",
},
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations.",
Unit: "",
},
@ -1928,19 +1928,19 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Repeated metadata",
Unit: "",
},
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations.",
Unit: "",
},
@ -1951,13 +1951,13 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created, but from a different target",
Unit: "",
},
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations, but from a different target.",
Unit: "",
},
@ -1977,7 +1977,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
@ -1988,27 +1988,27 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_gc_duration_seconds",
Type: textparse.MetricTypeSummary,
Type: model.MetricTypeSummary,
Help: "A summary of the GC invocation durations.",
Unit: "",
},
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads that were created.",
Unit: "",
},
},
},
},
response: map[string][]metadata{
response: map[string][]metadata.Metadata{
"go_threads": {
{textparse.MetricTypeGauge, "Number of OS threads created", ""},
{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
{Type: model.MetricTypeGauge, Help: "Number of OS threads created"},
{Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."},
},
},
sorter: func(m interface{}) {
v := m.(map[string][]metadata)["go_threads"]
v := m.(map[string][]metadata.Metadata)["go_threads"]
sort.Slice(v, func(i, j int) bool {
return v[i].Help < v[j].Help
@ -2025,19 +2025,19 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
metadata: []scrape.MetricMetadata{
{
Metric: "go_threads",
Type: textparse.MetricTypeGauge,
Type: model.MetricTypeGauge,
Help: "Number of OS threads created",
Unit: "",
},
},
},
},
response: map[string][]metadata{},
response: map[string][]metadata.Metadata{},
},
// With no available metadata.
{
endpoint: api.metricMetadata,
response: map[string][]metadata{},
response: map[string][]metadata.Metadata{},
},
{
endpoint: api.serveConfig,
@ -2931,7 +2931,7 @@ func assertAPIResponseMetadataLen(t *testing.T, got interface{}, expLen int) {
t.Helper()
var gotLen int
response := got.(map[string][]metadata)
response := got.(map[string][]metadata.Metadata)
for _, m := range response {
gotLen += len(m)
}