Skip to content

Commit

Permalink
Merge pull request #479 from grafana/prometheus-2023-04-03-3923e83
Browse files Browse the repository at this point in the history
Sync with Prometheus up to 2023-04-03 (3923e83)
  • Loading branch information
colega committed Apr 13, 2023
2 parents ae170f6 + 4086a5f commit 8ef48ad
Show file tree
Hide file tree
Showing 31 changed files with 667 additions and 300 deletions.
13 changes: 12 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ linters-settings:
- io/ioutil: "Use corresponding 'os' or 'io' functions instead."
- regexp: "Use github.com/grafana/regexp instead of regexp"
errcheck:
exclude: scripts/errcheck_excludes.txt
exclude-functions:
# Don't flag lines such as "io.Copy(io.Discard, resp.Body)".
- io.Copy
# The next two are used in HTTP handlers, any error is handled by the server itself.
- io.WriteString
- (net/http.ResponseWriter).Write
# No need to check for errors on server's shutdown.
- (*net/http.Server).Shutdown
# Never check for logger errors.
- (github.com/go-kit/log.Logger).Log
# Never check for rollback errors as Rollback() is called when a previous error was detected.
- (github.com/prometheus/prometheus/storage.Appender).Rollback
goimports:
local-prefixes: github.com/prometheus/prometheus
gofumpt:
Expand Down
2 changes: 1 addition & 1 deletion MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Julien Pivotto (<roidelapluie@prometheus.io> / @roidelapluie) and Levi Harrison
* `prometheus-mixin`: Björn Rabenstein (<beorn@grafana.com> / @beorn7)
* `storage`
* `remote`: Chris Marchbanks (<csmarchbanks@gmail.com> / @csmarchbanks), Callum Styan (<callumstyan@gmail.com> / @cstyan), Bartłomiej Płotka (<bwplotka@gmail.com> / @bwplotka), Tom Wilkie (<tom.wilkie@gmail.com> / @tomwilkie)
* `tsdb`: Ganesh Vernekar (<ganesh@grafana.com> / @codesome), Bartłomiej Płotka (<bwplotka@gmail.com> / @bwplotka)
* `tsdb`: Ganesh Vernekar (<ganesh@grafana.com> / @codesome), Bartłomiej Płotka (<bwplotka@gmail.com> / @bwplotka), Jesús Vázquez (<jesus.vazquez@grafana.com> / @jesusvazquez)
* `agent`: Robert Fratto (<robert.fratto@grafana.com> / @rfratto)
* `web`
* `ui`: Julius Volz (<julius.volz@gmail.com> / @juliusv)
Expand Down
10 changes: 5 additions & 5 deletions cmd/prometheus/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestFailedStartupExitCode(t *testing.T) {
fakeInputFile := "fake-input-file"
expectedExitStatus := 2

prom := exec.Command(promPath, "-test.main", "--config.file="+fakeInputFile)
prom := exec.Command(promPath, "-test.main", "--web.listen-address=0.0.0.0:0", "--config.file="+fakeInputFile)
err := prom.Run()
require.Error(t, err)

Expand Down Expand Up @@ -358,7 +358,7 @@ func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames
}

func TestAgentSuccessfulStartup(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--config.file="+agentConfig)
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--web.listen-address=0.0.0.0:0", "--config.file="+agentConfig)
require.NoError(t, prom.Start())

actualExitStatus := 0
Expand All @@ -376,7 +376,7 @@ func TestAgentSuccessfulStartup(t *testing.T) {
}

func TestAgentFailedStartupWithServerFlag(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--storage.tsdb.path=.", "--config.file="+promConfig)
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--storage.tsdb.path=.", "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig)

output := bytes.Buffer{}
prom.Stderr = &output
Expand All @@ -403,7 +403,7 @@ func TestAgentFailedStartupWithServerFlag(t *testing.T) {
}

func TestAgentFailedStartupWithInvalidConfig(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--config.file="+promConfig)
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig)
require.NoError(t, prom.Start())

actualExitStatus := 0
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestModeSpecificFlags(t *testing.T) {

for _, tc := range testcases {
t.Run(fmt.Sprintf("%s mode with option %s", tc.mode, tc.arg), func(t *testing.T) {
args := []string{"-test.main", tc.arg, t.TempDir()}
args := []string{"-test.main", tc.arg, t.TempDir(), "--web.listen-address=0.0.0.0:0"}

if tc.mode == "agent" {
args = append(args, "--enable-feature=agent", "--config.file="+agentConfig)
Expand Down
60 changes: 60 additions & 0 deletions cmd/promtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
lintOptionAll = "all"
lintOptionDuplicateRules = "duplicate-rules"
lintOptionNone = "none"
checkHealth = "/-/healthy"
checkReadiness = "/-/ready"
)

var lintOptions = []string{lintOptionAll, lintOptionDuplicateRules, lintOptionNone}
Expand All @@ -87,6 +89,7 @@ func main() {
app.HelpFlag.Short('h')

checkCmd := app.Command("check", "Check the resources for validity.")
checkCmd.Flag("http.config.file", "HTTP client configuration file for promtool to connect to Prometheus.").PlaceHolder("<filename>").ExistingFileVar(&httpConfigFilePath)

sdCheckCmd := checkCmd.Command("service-discovery", "Perform service discovery for the given job name and report the results, including relabeling.")
sdConfigFile := sdCheckCmd.Arg("config-file", "The prometheus config file.").Required().ExistingFile()
Expand All @@ -113,6 +116,18 @@ func main() {
"The config files to check.",
).Required().ExistingFiles()

checkServerHealthCmd := checkCmd.Command("healthy", "Check if the Prometheus server is healthy.")
serverHealthURLArg := checkServerHealthCmd.Arg(
"server",
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()

checkServerReadyCmd := checkCmd.Command("ready", "Check if the Prometheus server is ready.")
serverReadyURLArg := checkServerReadyCmd.Arg(
"server",
"The URL of the Prometheus server to check (e.g. http://localhost:9090)",
).URL()

checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
ruleFiles := checkRulesCmd.Arg(
"rule-files",
Expand Down Expand Up @@ -276,6 +291,12 @@ func main() {
case checkConfigCmd.FullCommand():
os.Exit(CheckConfig(*agentMode, *checkConfigSyntaxOnly, newLintConfig(*checkConfigLint, *checkConfigLintFatal), *configFiles...))

case checkServerHealthCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverHealthURLArg, checkHealth, httpRoundTripper)))

case checkServerReadyCmd.FullCommand():
os.Exit(checkErr(CheckServerStatus(*serverReadyURLArg, checkReadiness, httpRoundTripper)))

case checkWebConfigCmd.FullCommand():
os.Exit(CheckWebConfig(*webConfigFiles...))

Expand Down Expand Up @@ -369,6 +390,45 @@ func (ls lintConfig) lintDuplicateRules() bool {
return ls.all || ls.duplicateRules
}

const promDefaultURL = "http://localhost:9090"

// Check server status - healthy & ready.
func CheckServerStatus(serverURL *url.URL, checkEndpoint string, roundTripper http.RoundTripper) error {
if serverURL == nil {
serverURL, _ = url.Parse(promDefaultURL)
}

config := api.Config{
Address: serverURL.String() + checkEndpoint,
RoundTripper: roundTripper,
}

// Create new client.
c, err := api.NewClient(config)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating API client:", err)
return err
}

request, err := http.NewRequest("GET", config.Address, nil)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
response, dataBytes, err := c.Do(ctx, request)
if err != nil {
return err
}

if response.StatusCode != http.StatusOK {
return fmt.Errorf("check failed: URL=%s, status=%d", serverURL, response.StatusCode)
}

fmt.Fprintln(os.Stderr, " SUCCESS: ", string(dataBytes))
return nil
}

// CheckConfig validates configuration files.
func CheckConfig(agentMode, checkSyntaxOnly bool, lintSettings lintConfig, files ...string) int {
failed := false
Expand Down
31 changes: 31 additions & 0 deletions docs/command-line/promtool.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Check the resources for validity.

| Flag | Description |
| --- | --- |
| <code class="text-nowrap">--http.config.file</code> | HTTP client configuration file for promtool to connect to Prometheus. |
| <code class="text-nowrap">--extended</code> | Print extended information related to the cardinality of the metrics. |


Expand Down Expand Up @@ -130,6 +131,36 @@ Check if the web config files are valid or not.



##### `promtool check healthy`

Check if the Prometheus server is healthy.



###### Arguments

| Argument | Description |
| --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) |




##### `promtool check ready`

Check if the Prometheus server is ready.



###### Arguments

| Argument | Description |
| --- | --- |
| server | The URL of the Prometheus server to check (e.g. http://localhost:9090) |




##### `promtool check rules`

Check if the rule files are valid or not.
Expand Down
32 changes: 16 additions & 16 deletions documentation/examples/remote_storage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ require (
github.com/influxdata/influxdb v1.11.0
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/common v0.42.0
github.com/prometheus/prometheus v0.42.0
github.com/prometheus/prometheus v0.43.0
github.com/stretchr/testify v1.8.2
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/aws/aws-sdk-go v1.44.187 // indirect
github.com/aws/aws-sdk-go v1.44.217 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
Expand All @@ -36,22 +36,22 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.37.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.2.0 // indirect
golang.org/x/exp v0.0.0-20230124195608-d38c7dcee874 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.29.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
Loading

0 comments on commit 8ef48ad

Please sign in to comment.