From 8ee24252d6cdc209eeb9422389841e58f49a6061 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 10 Dec 2023 18:22:40 +0100 Subject: [PATCH] Update .golangci.yml Signed-off-by: Matthieu MOREL --- .golangci.yml | 6 ++ config/http_config_test.go | 137 +++++++++++++++++++----------- config/tls_config_test.go | 31 ++++--- expfmt/decode.go | 1 + expfmt/decode_test.go | 11 ++- expfmt/encode.go | 3 +- expfmt/encode_test.go | 1 + expfmt/openmetrics_create_test.go | 134 ++++++++++++++--------------- expfmt/text_create_test.go | 126 ++++++++++++++------------- expfmt/text_parse.go | 3 +- expfmt/text_parse_test.go | 103 +++++++++++----------- model/alert_test.go | 2 +- model/fingerprinting_test.go | 1 - model/labels_test.go | 6 +- model/labelset_test.go | 2 - model/metric.go | 10 +-- model/metric_test.go | 4 +- model/signature.go | 6 +- model/signature_test.go | 14 +-- model/silence_test.go | 4 +- model/time_test.go | 46 ++++++---- model/value.go | 14 ++- model/value_float.go | 14 ++- model/value_test.go | 1 - promlog/flag/flag.go | 1 + promlog/log.go | 14 ++- server/static_file_server_test.go | 1 - version/info_go118.go | 6 +- 28 files changed, 376 insertions(+), 326 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f97b5284..107aabfe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,8 @@ linters: enable: - errorlint + - gofumpt + - goimports - misspell - revive - testifylint @@ -10,8 +12,12 @@ issues: - path: _test.go linters: - errcheck + max-issues-per-linter: 0 + max-same-issues: 0 linters-settings: + goimports: + local-prefixes: github.com/prometheus/common revive: rules: # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter diff --git a/config/http_config_test.go b/config/http_config_test.go index 72d1e798..7eeedfed 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -156,7 +156,8 @@ func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt Certificates: make([]tls.Certificate, 1), RootCAs: rootCAs, ClientAuth: tls.RequireAndVerifyClientCert, - ClientCAs: rootCAs} + ClientCAs: rootCAs, + } testServer.TLS.Certificates[0] = serverCertificate testServer.StartTLS() @@ -165,7 +166,7 @@ func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt } func TestNewClientFromConfig(t *testing.T) { - var newClientValidConfig = []struct { + newClientValidConfig := []struct { clientConfig HTTPClientConfig handler func(w http.ResponseWriter, r *http.Request) }{ @@ -176,24 +177,28 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: true}, + InsecureSkipVerify: true, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ExpectedMessage) }, - }, { + }, + { clientConfig: HTTPClientConfig{ TLSConfig: TLSConfig{ CAFile: TLSCAChainPath, CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ExpectedMessage) }, - }, { + }, + { clientConfig: HTTPClientConfig{ BearerToken: BearerToken, TLSConfig: TLSConfig{ @@ -201,7 +206,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -212,7 +218,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ BearerTokenFile: BearerTokenFile, TLSConfig: TLSConfig{ @@ -220,7 +227,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -231,7 +239,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ Authorization: &Authorization{Credentials: BearerToken}, TLSConfig: TLSConfig{ @@ -239,7 +248,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -250,7 +260,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ Authorization: &Authorization{CredentialsFile: AuthorizationCredentialsFile, Type: AuthorizationType}, TLSConfig: TLSConfig{ @@ -258,7 +269,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -269,7 +281,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ Authorization: &Authorization{ Type: AuthorizationType, @@ -279,7 +292,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -290,7 +304,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ Authorization: &Authorization{ Credentials: AuthorizationCredentials, @@ -301,7 +316,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -312,7 +328,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ Authorization: &Authorization{ CredentialsFile: BearerTokenFile, @@ -322,7 +339,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -333,7 +351,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ BasicAuth: &BasicAuth{ Username: ExpectedUsername, @@ -344,7 +363,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() @@ -358,7 +378,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, ExpectedMessage) } }, - }, { + }, + { clientConfig: HTTPClientConfig{ FollowRedirects: true, TLSConfig: TLSConfig{ @@ -366,7 +387,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { @@ -378,7 +400,8 @@ func TestNewClientFromConfig(t *testing.T) { fmt.Fprint(w, "It should follow the redirect.") } }, - }, { + }, + { clientConfig: HTTPClientConfig{ FollowRedirects: false, TLSConfig: TLSConfig{ @@ -386,7 +409,8 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { @@ -409,14 +433,16 @@ func TestNewClientFromConfig(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, TLSConfig: TLSConfig{ CAFile: TLSCAChainPath, CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, }, handler: func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { @@ -529,7 +555,7 @@ func TestProxyConfiguration(t *testing.T) { } func TestNewClientFromInvalidConfig(t *testing.T) { - var newClientInvalidConfig = []struct { + newClientInvalidConfig := []struct { clientConfig HTTPClientConfig errorMsg string }{ @@ -537,7 +563,8 @@ func TestNewClientFromInvalidConfig(t *testing.T) { clientConfig: HTTPClientConfig{ TLSConfig: TLSConfig{ CAFile: MissingCA, - InsecureSkipVerify: true}, + InsecureSkipVerify: true, + }, }, errorMsg: fmt.Sprintf("unable to load specified CA cert %s:", MissingCA), }, @@ -545,7 +572,8 @@ func TestNewClientFromInvalidConfig(t *testing.T) { clientConfig: HTTPClientConfig{ TLSConfig: TLSConfig{ CAFile: InvalidCA, - InsecureSkipVerify: true}, + InsecureSkipVerify: true, + }, }, errorMsg: fmt.Sprintf("unable to use specified CA cert %s", InvalidCA), }, @@ -609,7 +637,8 @@ func TestMissingBearerAuthFile(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, } handler := func(w http.ResponseWriter, r *http.Request) { bearer := r.Header.Get("Authorization") @@ -780,7 +809,7 @@ func TestTLSConfigEmpty(t *testing.T) { } func TestTLSConfigInvalidCA(t *testing.T) { - var invalidTLSConfig = []struct { + invalidTLSConfig := []struct { configTLSConfig TLSConfig errorMessage string }{ @@ -790,23 +819,28 @@ func TestTLSConfigInvalidCA(t *testing.T) { CertFile: "", KeyFile: "", ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, errorMessage: fmt.Sprintf("unable to load specified CA cert %s:", MissingCA), - }, { + }, + { configTLSConfig: TLSConfig{ CAFile: "", CertFile: MissingCert, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, errorMessage: fmt.Sprintf("unable to read specified client cert (%s):", MissingCert), - }, { + }, + { configTLSConfig: TLSConfig{ CAFile: "", CertFile: ClientCertificatePath, KeyFile: MissingKey, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, errorMessage: fmt.Sprintf("unable to read specified client key (%s):", MissingKey), }, { @@ -816,7 +850,8 @@ func TestTLSConfigInvalidCA(t *testing.T) { CertFile: ClientCertificatePath, KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, errorMessage: "at most one of cert and cert_file must be configured", }, { @@ -826,7 +861,8 @@ func TestTLSConfigInvalidCA(t *testing.T) { Key: Secret(readFile(t, ClientKeyNoPassPath)), KeyFile: ClientKeyNoPassPath, ServerName: "", - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, errorMessage: "at most one of key and key_file must be configured", }, } @@ -975,7 +1011,7 @@ func writeCertificate(bs map[string][]byte, src string, dst string) { if !ok { panic(fmt.Sprintf("Couldn't find %q in bs", src)) } - if err := os.WriteFile(dst, b, 0664); err != nil { + if err := os.WriteFile(dst, b, 0o664); err != nil { panic(err) } } @@ -1066,7 +1102,8 @@ func TestTLSRoundTripper(t *testing.T) { CAFile: ca, CertFile: cert, KeyFile: key, - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, } var c *http.Client @@ -1202,7 +1239,8 @@ func TestTLSRoundTripper_Inline(t *testing.T) { CertFile: tc.certFile, Key: Secret(tc.keyText), KeyFile: tc.keyFile, - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, } c, err := NewClientFromConfig(cfg, "test") @@ -1263,7 +1301,8 @@ func TestTLSRoundTripperRaces(t *testing.T) { CAFile: ca, CertFile: cert, KeyFile: key, - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, } var c *http.Client @@ -1399,7 +1438,9 @@ func NewRoundTripCheckRequest(checkRequest func(*http.Request), theResponse *htt checkRequest: checkRequest, roundTrip: roundTrip{ theResponse: theResponse, - theError: theError}} + theError: theError, + }, + } } type oauth2TestServerResponse struct { @@ -1418,7 +1459,7 @@ func TestOAuth2(t *testing.T) { })) defer ts.Close() - var yamlConfig = fmt.Sprintf(` + yamlConfig := fmt.Sprintf(` client_id: 1 client_secret: 2 scopes: @@ -1536,7 +1577,7 @@ func TestOAuth2WithFile(t *testing.T) { } defer os.Remove(secretFile.Name()) - var yamlConfig = fmt.Sprintf(` + yamlConfig := fmt.Sprintf(` client_id: 1 client_secret_file: %s scopes: @@ -1820,7 +1861,8 @@ func TestModifyTLSCertificates(t *testing.T) { CAFile: ca, CertFile: cert, KeyFile: key, - InsecureSkipVerify: false}, + InsecureSkipVerify: false, + }, } var c *http.Client @@ -2011,7 +2053,6 @@ no_proxy: promcon.io,cncf.io`, proxyServer.URL), proxyFunc := proxyConfig.Proxy() resultURL, err := proxyFunc(req) - if err != nil { t.Fatalf("expected no error, but got: %v", err) return diff --git a/config/tls_config_test.go b/config/tls_config_test.go index a1fcda7a..150c5619 100644 --- a/config/tls_config_test.go +++ b/config/tls_config_test.go @@ -16,6 +16,7 @@ package config import ( "bytes" "crypto/tls" + "encoding/json" "fmt" "os" "path/filepath" @@ -23,8 +24,6 @@ import ( "strings" "testing" - "encoding/json" - "gopkg.in/yaml.v2" ) @@ -59,32 +58,40 @@ var expectedTLSConfigs = []struct { { filename: "tls_config.empty.good.json", config: &tls.Config{}, - }, { + }, + { filename: "tls_config.insecure.good.json", config: &tls.Config{InsecureSkipVerify: true}, - }, { + }, + { filename: "tls_config.tlsversion.good.json", config: &tls.Config{MinVersion: tls.VersionTLS11}, - }, { + }, + { filename: "tls_config.max_version.good.json", config: &tls.Config{MaxVersion: tls.VersionTLS12}, }, { filename: "tls_config.empty.good.yml", config: &tls.Config{}, - }, { + }, + { filename: "tls_config.insecure.good.yml", config: &tls.Config{InsecureSkipVerify: true}, - }, { + }, + { filename: "tls_config.tlsversion.good.yml", config: &tls.Config{MinVersion: tls.VersionTLS11}, - }, { + }, + { filename: "tls_config.max_version.good.yml", config: &tls.Config{MaxVersion: tls.VersionTLS12}, - }, { + }, + { filename: "tls_config.max_and_min_version.good.yml", config: &tls.Config{MaxVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS11}, - }, { + }, + { filename: "tls_config.max_and_min_version_same.good.yml", config: &tls.Config{MaxVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12}, }, @@ -134,7 +141,7 @@ func TestTLSVersionStringer(t *testing.T) { } func TestTLSVersionMarshalYAML(t *testing.T) { - var tests = []struct { + tests := []struct { input TLSVersion expected string err error @@ -174,7 +181,7 @@ func TestTLSVersionMarshalYAML(t *testing.T) { } func TestTLSVersionMarshalJSON(t *testing.T) { - var tests = []struct { + tests := []struct { input TLSVersion expected string err error diff --git a/expfmt/decode.go b/expfmt/decode.go index 0ca86a3d..5090a167 100644 --- a/expfmt/decode.go +++ b/expfmt/decode.go @@ -23,6 +23,7 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/matttproud/golang_protobuf_extensions/v2/pbutil" + "github.com/prometheus/common/model" ) diff --git a/expfmt/decode_test.go b/expfmt/decode_test.go index 7b7b41fd..fff57ab6 100644 --- a/expfmt/decode_test.go +++ b/expfmt/decode_test.go @@ -101,8 +101,7 @@ mf2 4 } func TestProtoDecoder(t *testing.T) { - - var testTime = model.Now() + testTime := model.Now() scenarios := []struct { in string @@ -370,7 +369,7 @@ func TestProtoDecoder(t *testing.T) { } func testDiscriminatorHTTPHeader(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { input map[string]string output Format }{ @@ -436,7 +435,7 @@ func TestExtractSamples(t *testing.T) { Help: proto.String("Help for foo."), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Counter: &dto.Counter{ Value: proto.Float64(4711), }, @@ -448,7 +447,7 @@ func TestExtractSamples(t *testing.T) { Help: proto.String("Help for bar."), Type: dto.MetricType_GAUGE.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Gauge: &dto.Gauge{ Value: proto.Float64(3.14), }, @@ -460,7 +459,7 @@ func TestExtractSamples(t *testing.T) { Help: proto.String("Help for bad."), Type: dto.MetricType(42).Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Gauge: &dto.Gauge{ Value: proto.Float64(2.7), }, diff --git a/expfmt/encode.go b/expfmt/encode.go index ca214060..318531f5 100644 --- a/expfmt/encode.go +++ b/expfmt/encode.go @@ -19,9 +19,10 @@ import ( "net/http" "github.com/matttproud/golang_protobuf_extensions/v2/pbutil" - "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg" "google.golang.org/protobuf/encoding/prototext" + "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg" + dto "github.com/prometheus/client_model/go" ) diff --git a/expfmt/encode_test.go b/expfmt/encode_test.go index 70a829c9..67614613 100644 --- a/expfmt/encode_test.go +++ b/expfmt/encode_test.go @@ -102,6 +102,7 @@ func TestNegotiateOpenMetrics(t *testing.T) { }) } } + func TestEncode(t *testing.T) { var buff bytes.Buffer delimEncoder := NewEncoder(&buff, FmtProtoDelim) diff --git a/expfmt/openmetrics_create_test.go b/expfmt/openmetrics_create_test.go index 455e5e5e..ab1e8616 100644 --- a/expfmt/openmetrics_create_test.go +++ b/expfmt/openmetrics_create_test.go @@ -32,7 +32,7 @@ func TestCreateOpenMetrics(t *testing.T) { t.Error(err) } - var scenarios = []struct { + scenarios := []struct { in *dto.MetricFamily out string }{ @@ -43,13 +43,13 @@ func TestCreateOpenMetrics(t *testing.T) { Help: proto.String("two-line\n doc str\\ing"), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val1"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue"), }, @@ -58,13 +58,13 @@ func TestCreateOpenMetrics(t *testing.T) { Value: proto.Float64(42), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue"), }, @@ -89,13 +89,13 @@ name{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06 Help: proto.String("gauge\ndoc\nstr\"ing"), Type: dto.MetricType_GAUGE.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("val with\nnew line"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("val with \\backslash and \"quotes\""), }, @@ -104,13 +104,13 @@ name{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06 Value: proto.Float64(math.Inf(+1)), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("Björn"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("佖佥"), }, @@ -133,14 +133,14 @@ gauge_name{name_1="Björn",name_2="佖佥"} 3.14e+42 Name: proto.String("unknown_name"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("value 1"), }, @@ -163,33 +163,33 @@ unknown_name{name_1="value 1"} -1.23e-45 Help: proto.String("summary docstring"), Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Summary: &dto.Summary{ SampleCount: proto.Uint64(42), SampleSum: proto.Float64(-3.4567), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.5), Value: proto.Float64(-1.23), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.9), Value: proto.Float64(.2342354), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.99), Value: proto.Float64(0), }, }, }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("value 1"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("value 2"), }, @@ -198,15 +198,15 @@ unknown_name{name_1="value 1"} -1.23e-45 SampleCount: proto.Uint64(4711), SampleSum: proto.Float64(2010.1971), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.5), Value: proto.Float64(1), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.9), Value: proto.Float64(2), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.99), Value: proto.Float64(3), }, @@ -236,28 +236,28 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, - &dto.Bucket{ + { UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693), }, @@ -284,24 +284,24 @@ request_duration_microseconds_count 2693 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, @@ -328,21 +328,21 @@ request_duration_microseconds_count 2693 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), Exemplar: &dto.Exemplar{ Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("foo"), Value: proto.String("bar"), }, @@ -351,16 +351,16 @@ request_duration_microseconds_count 2693 Timestamp: openMetricsTimestamp, }, }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), Exemplar: &dto.Exemplar{ Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("foo"), Value: proto.String("baz"), }, - &dto.LabelPair{ + { Name: proto.String("dings"), Value: proto.String("bums"), }, @@ -368,7 +368,7 @@ request_duration_microseconds_count 2693 Value: proto.Float64(140.14), }, }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, @@ -395,7 +395,7 @@ request_duration_microseconds_count 2693 Help: proto.String("Number of foos."), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Counter: &dto.Counter{ Value: proto.Float64(42), }, @@ -441,7 +441,6 @@ foos_total 42.0 ) } } - } func BenchmarkOpenMetricsCreate(b *testing.B) { @@ -450,17 +449,17 @@ func BenchmarkOpenMetricsCreate(b *testing.B) { Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("val with\nnew line"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("val with \\backslash and \"quotes\""), }, - &dto.LabelPair{ + { Name: proto.String("name_3"), Value: proto.String("Just a quite long label value to test performance."), }, @@ -469,40 +468,40 @@ func BenchmarkOpenMetricsCreate(b *testing.B) { SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, - &dto.Bucket{ + { UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693), }, }, }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("Björn"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("佖佥"), }, - &dto.LabelPair{ + { Name: proto.String("name_3"), Value: proto.String("Just a quite long label value to test performance."), }, @@ -511,19 +510,19 @@ func BenchmarkOpenMetricsCreate(b *testing.B) { SampleCount: proto.Uint64(5699), SampleSum: proto.Float64(49484343543.4343), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(120), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(596), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1535), }, @@ -545,7 +544,7 @@ func BenchmarkOpenMetricsCreate(b *testing.B) { } func TestOpenMetricsCreateError(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in *dto.MetricFamily err string }{ @@ -555,7 +554,7 @@ func TestOpenMetricsCreateError(t *testing.T) { Help: proto.String("doc string"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, @@ -571,7 +570,7 @@ func TestOpenMetricsCreateError(t *testing.T) { Help: proto.String("doc string"), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, @@ -596,5 +595,4 @@ func TestOpenMetricsCreateError(t *testing.T) { ) } } - } diff --git a/expfmt/text_create_test.go b/expfmt/text_create_test.go index e6071666..8d3ac3d1 100644 --- a/expfmt/text_create_test.go +++ b/expfmt/text_create_test.go @@ -25,7 +25,7 @@ import ( ) func TestCreate(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in *dto.MetricFamily out string }{ @@ -36,13 +36,13 @@ func TestCreate(t *testing.T) { Help: proto.String("two-line\n doc str\\ing"), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val1"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue"), }, @@ -51,13 +51,13 @@ func TestCreate(t *testing.T) { Value: proto.Float64(math.NaN()), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue"), }, @@ -82,13 +82,13 @@ name{labelname="val2",basename="basevalue"} 0.23 1234567890 Help: proto.String("gauge\ndoc\nstr\"ing"), Type: dto.MetricType_GAUGE.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("val with\nnew line"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("val with \\backslash and \"quotes\""), }, @@ -97,13 +97,13 @@ name{labelname="val2",basename="basevalue"} 0.23 1234567890 Value: proto.Float64(math.Inf(+1)), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("Björn"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("佖佥"), }, @@ -126,14 +126,14 @@ gauge_name{name_1="Björn",name_2="佖佥"} 3.14e+42 Name: proto.String("untyped_name"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("value 1"), }, @@ -156,33 +156,33 @@ untyped_name{name_1="value 1"} -1.23e-45 Help: proto.String("summary docstring"), Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Summary: &dto.Summary{ SampleCount: proto.Uint64(42), SampleSum: proto.Float64(-3.4567), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.5), Value: proto.Float64(-1.23), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.9), Value: proto.Float64(.2342354), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.99), Value: proto.Float64(0), }, }, }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("value 1"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("value 2"), }, @@ -191,15 +191,15 @@ untyped_name{name_1="value 1"} -1.23e-45 SampleCount: proto.Uint64(4711), SampleSum: proto.Float64(2010.1971), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.5), Value: proto.Float64(1), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.9), Value: proto.Float64(2), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.99), Value: proto.Float64(3), }, @@ -229,28 +229,28 @@ summary_name_count{name_1="value 1",name_2="value 2"} 4711 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, - &dto.Bucket{ + { UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693), }, @@ -277,24 +277,24 @@ request_duration_microseconds_count 2693 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, @@ -320,7 +320,7 @@ request_duration_microseconds_count 2693 Name: proto.String("name"), Help: proto.String("doc string"), Metric: []*dto.Metric{ - &dto.Metric{ + { Counter: &dto.Counter{ Value: proto.Float64(math.Inf(-1)), }, @@ -354,7 +354,6 @@ name -Inf ) } } - } func BenchmarkCreate(b *testing.B) { @@ -363,17 +362,17 @@ func BenchmarkCreate(b *testing.B) { Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("val with\nnew line"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("val with \\backslash and \"quotes\""), }, - &dto.LabelPair{ + { Name: proto.String("name_3"), Value: proto.String("Just a quite long label value to test performance."), }, @@ -382,40 +381,40 @@ func BenchmarkCreate(b *testing.B) { SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, - &dto.Bucket{ + { UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693), }, }, }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("name_1"), Value: proto.String("Björn"), }, - &dto.LabelPair{ + { Name: proto.String("name_2"), Value: proto.String("佖佥"), }, - &dto.LabelPair{ + { Name: proto.String("name_3"), Value: proto.String("Just a quite long label value to test performance."), }, @@ -424,19 +423,19 @@ func BenchmarkCreate(b *testing.B) { SampleCount: proto.Uint64(5699), SampleSum: proto.Float64(49484343543.4343), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(120), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(596), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1535), }, @@ -463,17 +462,17 @@ func BenchmarkCreateBuildInfo(b *testing.B) { Help: proto.String("Test the creation of constant 1-value build_info metric."), Type: dto.MetricType_GAUGE.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("version"), Value: proto.String("1.2.3"), }, - &dto.LabelPair{ + { Name: proto.String("revision"), Value: proto.String("2e84f5e4eacdffb574035810305191ff390360fe"), }, - &dto.LabelPair{ + { Name: proto.String("go_version"), Value: proto.String("1.11.1"), }, @@ -496,7 +495,7 @@ func BenchmarkCreateBuildInfo(b *testing.B) { } func TestCreateError(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in *dto.MetricFamily err string }{ @@ -516,7 +515,7 @@ func TestCreateError(t *testing.T) { Help: proto.String("doc string"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, @@ -532,7 +531,7 @@ func TestCreateError(t *testing.T) { Help: proto.String("doc string"), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(math.Inf(-1)), }, @@ -557,5 +556,4 @@ func TestCreateError(t *testing.T) { ) } } - } diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index 196f5033..26490211 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -25,8 +25,9 @@ import ( dto "github.com/prometheus/client_model/go" - "github.com/prometheus/common/model" "google.golang.org/protobuf/proto" + + "github.com/prometheus/common/model" ) // A stateFn is a function that represents a state in a state machine. By diff --git a/expfmt/text_parse_test.go b/expfmt/text_parse_test.go index 204a88a3..0540546a 100644 --- a/expfmt/text_parse_test.go +++ b/expfmt/text_parse_test.go @@ -24,7 +24,7 @@ import ( ) func testTextParse(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { in string out []*dto.MetricFamily }{ @@ -45,22 +45,22 @@ no_labels{} 3 # HELP line for non-existing metric will be ignored. `, out: []*dto.MetricFamily{ - &dto.MetricFamily{ + { Name: proto.String("minimal_metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(1.234), }, }, }, }, - &dto.MetricFamily{ + { Name: proto.String("another_metric"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(-3e3), }, @@ -68,11 +68,11 @@ no_labels{} 3 }, }, }, - &dto.MetricFamily{ + { Name: proto.String("no_labels"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(3), }, @@ -97,18 +97,18 @@ name2{labelname="val2" ,basename = "basevalue2" } +Inf 54321 name2{ labelname = "val1" , }-Inf `, out: []*dto.MetricFamily{ - &dto.MetricFamily{ + { Name: proto.String("name"), Help: proto.String("two-line\n doc str\\ing"), Type: dto.MetricType_COUNTER.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val1"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue"), }, @@ -117,13 +117,13 @@ name2{ labelname = "val1" , }-Inf Value: proto.Float64(math.NaN()), }, }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("base\"v\\al\nue"), }, @@ -135,18 +135,18 @@ name2{ labelname = "val1" , }-Inf }, }, }, - &dto.MetricFamily{ + { Name: proto.String("name2"), Help: proto.String("doc str\"ing 2"), Type: dto.MetricType_GAUGE.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("basename"), Value: proto.String("basevalue2"), }, @@ -156,9 +156,9 @@ name2{ labelname = "val1" , }-Inf }, TimestampMs: proto.Int64(54321), }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("labelname"), Value: proto.String("val1"), }, @@ -197,13 +197,13 @@ my_summary{n1="val3", quantile="0.2"} 4711 # HELP my_summary `, out: []*dto.MetricFamily{ - &dto.MetricFamily{ + { Name: proto.String("fake_sum"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val1"), }, @@ -214,11 +214,11 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, }, }, - &dto.MetricFamily{ + { Name: proto.String("decoy"), Type: dto.MetricType_UNTYPED.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Untyped: &dto.Untyped{ Value: proto.Float64(-1), }, @@ -226,13 +226,13 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, }, }, - &dto.MetricFamily{ + { Name: proto.String("my_summary"), Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val1"), }, @@ -241,11 +241,11 @@ my_summary{n1="val3", quantile="0.2"} 4711 SampleCount: proto.Uint64(42), SampleSum: proto.Float64(4711), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.5), Value: proto.Float64(110), }, - &dto.Quantile{ + { Quantile: proto.Float64(0.9), Value: proto.Float64(140), }, @@ -253,13 +253,13 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, TimestampMs: proto.Int64(2), }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n2"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val1"), }, @@ -267,7 +267,7 @@ my_summary{n1="val3", quantile="0.2"} 4711 Summary: &dto.Summary{ SampleCount: proto.Uint64(5), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(-12.34), Value: proto.Float64(math.NaN()), }, @@ -275,9 +275,9 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, TimestampMs: proto.Int64(5), }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val2"), }, @@ -287,16 +287,16 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, TimestampMs: proto.Int64(15), }, - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val3"), }, }, Summary: &dto.Summary{ Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.2), Value: proto.Float64(4711), }, @@ -305,17 +305,17 @@ my_summary{n1="val3", quantile="0.2"} 4711 }, }, }, - &dto.MetricFamily{ + { Name: proto.String("another_summary"), Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{ + { Name: proto.String("n2"), Value: proto.String("val2"), }, - &dto.LabelPair{ + { Name: proto.String("n1"), Value: proto.String("val1"), }, @@ -323,7 +323,7 @@ my_summary{n1="val3", quantile="0.2"} 4711 Summary: &dto.Summary{ SampleCount: proto.Uint64(20), Quantile: []*dto.Quantile{ - &dto.Quantile{ + { Quantile: proto.Float64(0.3), Value: proto.Float64(-1.2), }, @@ -353,28 +353,28 @@ request_duration_microseconds_count 2693 Help: proto.String("The response latency."), Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{ - &dto.Metric{ + { Histogram: &dto.Histogram{ SampleCount: proto.Uint64(2693), SampleSum: proto.Float64(1756047.3), Bucket: []*dto.Bucket{ - &dto.Bucket{ + { UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123), }, - &dto.Bucket{ + { UpperBound: proto.Float64(120), CumulativeCount: proto.Uint64(412), }, - &dto.Bucket{ + { UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592), }, - &dto.Bucket{ + { UpperBound: proto.Float64(172.8), CumulativeCount: proto.Uint64(1524), }, - &dto.Bucket{ + { UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693), }, @@ -429,7 +429,7 @@ func BenchmarkTextParse(b *testing.B) { } func testTextParseError(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { in string err string }{ @@ -656,7 +656,6 @@ metric{quantile="0x1p-3"} 3.14 ) } } - } func TestTextParseError(t *testing.T) { diff --git a/model/alert_test.go b/model/alert_test.go index 8910da7a..c140b15e 100644 --- a/model/alert_test.go +++ b/model/alert_test.go @@ -24,7 +24,7 @@ import ( func TestAlertValidate(t *testing.T) { ts := time.Now() - var cases = []struct { + cases := []struct { alert *Alert err string }{ diff --git a/model/fingerprinting_test.go b/model/fingerprinting_test.go index b1237510..98f49714 100644 --- a/model/fingerprinting_test.go +++ b/model/fingerprinting_test.go @@ -22,7 +22,6 @@ func TestFingerprintFromString(t *testing.T) { fs := "4294967295" f, err := FingerprintFromString(fs) - if err != nil { t.Errorf("unexpected error while getting Fingerprint from string: %s", err.Error()) } diff --git a/model/labels_test.go b/model/labels_test.go index 2ee5b31a..c49d8e7c 100644 --- a/model/labels_test.go +++ b/model/labels_test.go @@ -19,7 +19,7 @@ import ( ) func testLabelNames(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { in LabelNames out LabelNames }{ @@ -55,7 +55,7 @@ func BenchmarkLabelNames(b *testing.B) { } func testLabelValues(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { in LabelValues out LabelValues }{ @@ -91,7 +91,7 @@ func BenchmarkLabelValues(b *testing.B) { } func TestLabelNameIsValid(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { ln LabelName valid bool }{ diff --git a/model/labelset_test.go b/model/labelset_test.go index dfdfc594..a6f1212c 100644 --- a/model/labelset_test.go +++ b/model/labelset_test.go @@ -32,7 +32,6 @@ func TestUnmarshalJSONLabelSet(t *testing.T) { }` var c testConfig err := json.Unmarshal([]byte(labelSetJSON), &c) - if err != nil { t.Errorf("unexpected error while marshalling JSON : %s", err.Error()) } @@ -116,5 +115,4 @@ func TestLabelSetMerge(t *testing.T) { t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln) } } - } diff --git a/model/metric.go b/model/metric.go index 00804b7f..f8c5eaba 100644 --- a/model/metric.go +++ b/model/metric.go @@ -20,12 +20,10 @@ import ( "strings" ) -var ( - // MetricNameRE is a regular expression matching valid metric - // names. Note that the IsValidMetricName function performs the same - // check but faster than a match with this regular expression. - MetricNameRE = regexp.MustCompile(`^[a-zA-Z_:][a-zA-Z0-9_:]*$`) -) +// MetricNameRE is a regular expression matching valid metric +// names. Note that the IsValidMetricName function performs the same +// check but faster than a match with this regular expression. +var MetricNameRE = regexp.MustCompile(`^[a-zA-Z_:][a-zA-Z0-9_:]*$`) // A Metric is similar to a LabelSet, but the key difference is that a Metric is // a singleton and refers to one and only one stream of samples. diff --git a/model/metric_test.go b/model/metric_test.go index db447f6f..cc4e27d0 100644 --- a/model/metric_test.go +++ b/model/metric_test.go @@ -16,7 +16,7 @@ package model import "testing" func testMetric(t testing.TB) { - var scenarios = []struct { + scenarios := []struct { input LabelSet fingerprint Fingerprint fastFingerprint Fingerprint @@ -83,7 +83,7 @@ func BenchmarkMetric(b *testing.B) { } func TestMetricNameIsValid(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { mn LabelValue valid bool }{ diff --git a/model/signature.go b/model/signature.go index 8762b13c..dc8a0026 100644 --- a/model/signature.go +++ b/model/signature.go @@ -22,10 +22,8 @@ import ( // when calculating their combined hash value (aka signature aka fingerprint). const SeparatorByte byte = 255 -var ( - // cache the signature of an empty label set. - emptyLabelSignature = hashNew() -) +// cache the signature of an empty label set. +var emptyLabelSignature = hashNew() // LabelsToSignature returns a quasi-unique signature (i.e., fingerprint) for a // given label set. (Collisions are possible but unlikely if the number of label diff --git a/model/signature_test.go b/model/signature_test.go index 0c797058..a075bdc6 100644 --- a/model/signature_test.go +++ b/model/signature_test.go @@ -21,7 +21,7 @@ import ( ) func TestLabelsToSignature(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in map[string]string out uint64 }{ @@ -45,7 +45,7 @@ func TestLabelsToSignature(t *testing.T) { } func TestMetricToFingerprint(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in LabelSet out Fingerprint }{ @@ -69,7 +69,7 @@ func TestMetricToFingerprint(t *testing.T) { } func TestMetricToFastFingerprint(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in LabelSet out Fingerprint }{ @@ -93,7 +93,7 @@ func TestMetricToFastFingerprint(t *testing.T) { } func TestSignatureForLabels(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in Metric labels LabelNames out uint64 @@ -145,7 +145,7 @@ func TestSignatureForLabels(t *testing.T) { } func TestSignatureWithoutLabels(t *testing.T) { - var scenarios = []struct { + scenarios := []struct { in Metric labels map[LabelName]struct{} out uint64 @@ -157,12 +157,12 @@ func TestSignatureWithoutLabels(t *testing.T) { }, { in: Metric{"name": "garland, briggs", "fear": "love is not enough"}, - labels: map[LabelName]struct{}{"fear": struct{}{}, "name": struct{}{}}, + labels: map[LabelName]struct{}{"fear": {}, "name": {}}, out: 14695981039346656037, }, { in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"}, - labels: map[LabelName]struct{}{"foo": struct{}{}}, + labels: map[LabelName]struct{}{"foo": {}}, out: 5799056148416392346, }, { diff --git a/model/silence_test.go b/model/silence_test.go index be50298b..e6b14116 100644 --- a/model/silence_test.go +++ b/model/silence_test.go @@ -20,7 +20,7 @@ import ( ) func TestMatcherValidate(t *testing.T) { - var cases = []struct { + cases := []struct { matcher *Matcher err string }{ @@ -89,7 +89,7 @@ func TestMatcherValidate(t *testing.T) { func TestSilenceValidate(t *testing.T) { ts := time.Now() - var cases = []struct { + cases := []struct { sil *Silence err string }{ diff --git a/model/time_test.go b/model/time_test.go index b0703fd1..f5724122 100644 --- a/model/time_test.go +++ b/model/time_test.go @@ -88,7 +88,7 @@ func TestDuration(t *testing.T) { } func TestParseDuration(t *testing.T) { - var cases = []struct { + cases := []struct { in string out time.Duration @@ -159,7 +159,7 @@ func TestParseDuration(t *testing.T) { } func TestDuration_UnmarshalText(t *testing.T) { - var cases = []struct { + cases := []struct { in string out time.Duration @@ -232,7 +232,7 @@ func TestDuration_UnmarshalText(t *testing.T) { } func TestDuration_UnmarshalJSON(t *testing.T) { - var cases = []struct { + cases := []struct { in string out time.Duration @@ -242,43 +242,55 @@ func TestDuration_UnmarshalJSON(t *testing.T) { in: `"0"`, out: 0, expectedString: `"0s"`, - }, { + }, + { in: `"0w"`, out: 0, expectedString: `"0s"`, - }, { + }, + { in: `"0s"`, out: 0, - }, { + }, + { in: `"324ms"`, out: 324 * time.Millisecond, - }, { + }, + { in: `"3s"`, out: 3 * time.Second, - }, { + }, + { in: `"5m"`, out: 5 * time.Minute, - }, { + }, + { in: `"1h"`, out: time.Hour, - }, { + }, + { in: `"4d"`, out: 4 * 24 * time.Hour, - }, { + }, + { in: `"4d1h"`, out: 4*24*time.Hour + time.Hour, - }, { + }, + { in: `"14d"`, out: 14 * 24 * time.Hour, expectedString: `"2w"`, - }, { + }, + { in: `"3w"`, out: 3 * 7 * 24 * time.Hour, - }, { + }, + { in: `"3w2d1h"`, out: 3*7*24*time.Hour + 2*24*time.Hour + time.Hour, expectedString: `"23d1h"`, - }, { + }, + { in: `"10y"`, out: 10 * 365 * 24 * time.Hour, }, @@ -312,7 +324,7 @@ func TestDuration_UnmarshalJSON(t *testing.T) { } func TestParseBadDuration(t *testing.T) { - var cases = []string{ + cases := []string{ "1", "1y1m1d", "-1w", @@ -362,10 +374,8 @@ func TestTimeJSON(t *testing.T) { if !test.in.Equal(tm) { t.Fatalf("Mismatch after Unmarshal expected=%v actual=%v", test.in, tm) } - }) } - } func BenchmarkParseDuration(b *testing.B) { diff --git a/model/value.go b/model/value.go index 71e0c50c..8050637d 100644 --- a/model/value.go +++ b/model/value.go @@ -21,14 +21,12 @@ import ( "strings" ) -var ( - // ZeroSample is the pseudo zero-value of Sample used to signal a - // non-existing sample. It is a Sample with timestamp Earliest, value 0.0, - // and metric nil. Note that the natural zero value of Sample has a timestamp - // of 0, which is possible to appear in a real Sample and thus not suitable - // to signal a non-existing Sample. - ZeroSample = Sample{Timestamp: Earliest} -) +// ZeroSample is the pseudo zero-value of Sample used to signal a +// non-existing sample. It is a Sample with timestamp Earliest, value 0.0, +// and metric nil. Note that the natural zero value of Sample has a timestamp +// of 0, which is possible to appear in a real Sample and thus not suitable +// to signal a non-existing Sample. +var ZeroSample = Sample{Timestamp: Earliest} // Sample is a sample pair associated with a metric. A single sample must either // define Value or Histogram but not both. Histogram == nil implies the Value diff --git a/model/value_float.go b/model/value_float.go index 0f615a70..ae35cc2a 100644 --- a/model/value_float.go +++ b/model/value_float.go @@ -20,14 +20,12 @@ import ( "strconv" ) -var ( - // ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a - // non-existing sample pair. It is a SamplePair with timestamp Earliest and - // value 0.0. Note that the natural zero value of SamplePair has a timestamp - // of 0, which is possible to appear in a real SamplePair and thus not - // suitable to signal a non-existing SamplePair. - ZeroSamplePair = SamplePair{Timestamp: Earliest} -) +// ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a +// non-existing sample pair. It is a SamplePair with timestamp Earliest and +// value 0.0. Note that the natural zero value of SamplePair has a timestamp +// of 0, which is possible to appear in a real SamplePair and thus not +// suitable to signal a non-existing SamplePair. +var ZeroSamplePair = SamplePair{Timestamp: Earliest} // A SampleValue is a representation of a value for a given sample at a given // time. diff --git a/model/value_test.go b/model/value_test.go index 1eeae338..c57efb16 100644 --- a/model/value_test.go +++ b/model/value_test.go @@ -153,7 +153,6 @@ func TestEqualSamples(t *testing.T) { t.Errorf("Comparing %s, %v and %v: got %t, want %t", name, test.in1, test.in2, got, test.want) } } - } func TestScalarJSON(t *testing.T) { diff --git a/promlog/flag/flag.go b/promlog/flag/flag.go index c25d818d..9295bbb2 100644 --- a/promlog/flag/flag.go +++ b/promlog/flag/flag.go @@ -15,6 +15,7 @@ package flag import ( kingpin "github.com/alecthomas/kingpin/v2" + "github.com/prometheus/common/promlog" ) diff --git a/promlog/log.go b/promlog/log.go index 3ac7b3fd..c8663ad8 100644 --- a/promlog/log.go +++ b/promlog/log.go @@ -26,14 +26,12 @@ import ( "github.com/go-kit/log/level" ) -var ( - // This timestamp format differs from RFC3339Nano by using .000 instead - // of .999999999 which changes the timestamp from 9 variable to 3 fixed - // decimals (.130 instead of .130987456). - timestampFormat = log.TimestampFormat( - func() time.Time { return time.Now().UTC() }, - "2006-01-02T15:04:05.000Z07:00", - ) +// This timestamp format differs from RFC3339Nano by using .000 instead +// of .999999999 which changes the timestamp from 9 variable to 3 fixed +// decimals (.130 instead of .130987456). +var timestampFormat = log.TimestampFormat( + func() time.Time { return time.Now().UTC() }, + "2006-01-02T15:04:05.000Z07:00", ) // AllowedLevel is a settable identifier for the minimum level a log entry diff --git a/server/static_file_server_test.go b/server/static_file_server_test.go index a75936b5..78fe56df 100644 --- a/server/static_file_server_test.go +++ b/server/static_file_server_test.go @@ -67,7 +67,6 @@ func TestServeHttp(t *testing.T) { t.Run(c.name, func(t *testing.T) { rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "http://localhost/"+c.path, nil) - if err != nil { t.Fatal(err) } diff --git a/version/info_go118.go b/version/info_go118.go index bfc7d410..5934919d 100644 --- a/version/info_go118.go +++ b/version/info_go118.go @@ -18,8 +18,10 @@ package version import "runtime/debug" -var computedRevision string -var computedTags string +var ( + computedRevision string + computedTags string +) func getRevision() string { if Revision != "" {