Skip to content

Commit

Permalink
Allow to specify -prometheus-http-prefix for mimirtool analyze promet…
Browse files Browse the repository at this point in the history
…heus (#4966)

When configuring mimirtool via environment variables users might use the
following:

```
MIMIR_ADDRESS=https://mimir:9090
MIMIR_TENANT_ID=default
MIMIR_API_USER=default
MIMIR_API_KEY=1234
```

It works as expected for `mimirtool analyze ruler` but not for
`mimirtool analyze prometheus`.

The HTTP client for `analyze ruler` is done with
`github.com/grafana/mimir/pkg/mimirtool/client`, when for `analyze
prometheus` it is done with only the address.

Depending on which prometheus the user wants to analyze the path is not
the same. For a mimir based analysis user needs to provide
`/prometheus`.

This commit allow to specify the path to use for a mimir based analysis.
It also simplify the scripting around the tool.

Example:

```
source .secrets.env
mimirtool analyze grafana && \
mimirtool analyze ruler && \
mimirtool analyze prometheus --prometheus-http-prefix="/prometheus"
```

Signed-off-by: Wilfried Roset <wilfriedroset@users.noreply.github.com>
  • Loading branch information
wilfriedroset authored May 10, 2023
1 parent c9bdfe3 commit ee8a8e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
* [ENHANCEMENT] Compactor: configure `-compactor.first-level-compaction-wait-period` to TSDB head compaction interval plus 10 minutes. #4872
* [BUGFIX] Backend: configure `-ruler.alertmanager-url` to `mimir-backend` when running in read-write deployment mode. #4892

### Mimirtool

* [ENHANCEMENT] analyze prometheus: allow to specify `-prometheus-http-prefix`. #4966

## 2.8.0

### Grafana Mimir
Expand Down
17 changes: 9 additions & 8 deletions docs/sources/mimir/operators-guide/tools/mimirtool.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,15 @@ mimirtool analyze prometheus --address=<url> --id=<tenant_id>

##### Configuration

| Environment variable | Flag | Description |
| -------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------------ |
| `MIMIR_ADDRESS` | `--address` | Sets the address of the Prometheus instance. |
| `MIMIR_TENANT_ID` | `--id` | Sets the basic auth username. If you're using Grafana Cloud this variable is your instance ID. |
| `MIMIR_API_KEY` | `--key` | Sets the basic auth password. If you're using Grafana Cloud, this variable is your API key. |
| - | `--grafana-metrics-file` | `mimirtool analyze grafana` or `mimirtool analyze dashboard` output file, which by default is `metrics-in-grafana.json`. |
| - | `--ruler-metrics-file` | `mimirtool analyze ruler` or `mimirtool analyze rule-file` output file, which by default is `metrics-in-ruler.json`. |
| - | `--output` | Sets the output file path, which by default is `prometheus-metrics.json`. |
| Environment variable | Flag | Description |
| -------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `MIMIR_ADDRESS` | `--address` | Sets the address of the Prometheus instance. |
| `MIMIR_TENANT_ID` | `--id` | Sets the basic auth username. If you're using Grafana Cloud this variable is your instance ID. |
| `MIMIR_API_KEY` | `--key` | Sets the basic auth password. If you're using Grafana Cloud, this variable is your API key. |
| - | `--grafana-metrics-file` | `mimirtool analyze grafana` or `mimirtool analyze dashboard` output file, which by default is `metrics-in-grafana.json`. |
| - | `--ruler-metrics-file` | `mimirtool analyze ruler` or `mimirtool analyze rule-file` output file, which by default is `metrics-in-ruler.json`. |
| - | `--output` | Sets the output file path, which by default is `prometheus-metrics.json`. |
| - | `--prometheus-http-prefix` | Sets the HTTP URL path under which the Prometheus api will be served. |

##### Example output

Expand Down
3 changes: 3 additions & 0 deletions pkg/mimirtool/commands/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func (cmd *AnalyzeCommand) Register(app *kingpin.Application, envVars EnvVarName
Envar(envVars.Address).
Required().
StringVar(&paCmd.address)
prometheusAnalyzeCmd.Flag("prometheus-http-prefix", "HTTP URL path under which the Prometheus api will be served.").
Default("").
StringVar(&paCmd.prometheusHTTPPrefix)
prometheusAnalyzeCmd.Flag("id", "Username to use when contacting Prometheus or Grafana Mimir; alternatively, set "+envVars.TenantID+".").
Envar(envVars.TenantID).
Default("").
Expand Down
16 changes: 11 additions & 5 deletions pkg/mimirtool/commands/analyse_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"sort"
"sync"
Expand All @@ -30,10 +31,11 @@ import (
)

type PrometheusAnalyzeCommand struct {
address string
username string
password string
readTimeout time.Duration
address string
prometheusHTTPPrefix string
username string
password string
readTimeout time.Duration

grafanaMetricsFile string
rulerMetricsFile string
Expand Down Expand Up @@ -111,8 +113,12 @@ func (cmd *PrometheusAnalyzeCommand) newAPI() (v1.API, error) {
rt = config.NewBasicAuthRoundTripper(cmd.username, config.Secret(cmd.password), "", rt)
}

address, err := url.JoinPath(cmd.address, cmd.prometheusHTTPPrefix)
if err != nil {
return nil, err
}
client, err := api.NewClient(api.Config{
Address: cmd.address,
Address: address,
RoundTripper: rt,
})
if err != nil {
Expand Down

0 comments on commit ee8a8e2

Please sign in to comment.