Skip to content

Commit 7252fa6

Browse files
authored
Merge pull request #10 from chaosnative/sync-upstream
Sync the upstream changes for auth and tls
2 parents 96471e7 + d8f6b60 commit 7252fa6

11 files changed

Lines changed: 214 additions & 65 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "PromQL CLI Dev",
3+
"dockerComposeFile": "../docker-compose.dev.yml",
4+
"service": "promql",
5+
"workspaceFolder": "/promql-cli/",
6+
"remoteUser": "promql",
7+
"features": {
8+
"ghcr.io/devcontainers/features/common-utils:2": {
9+
"configureZshAsDefaultShell": true,
10+
"username": "promql"
11+
},
12+
"ghcr.io/devcontainers/features/git:1": {},
13+
"ghcr.io/eitsupi/devcontainer-features/jq-likes:1": {},
14+
"ghcr.io/devcontainers/features/go:1": {},
15+
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
16+
},
17+
"customizations": {
18+
"vscode": {
19+
"extensions": [
20+
"golang.go",
21+
"ms-azuretools.vscode-docker",
22+
"Gruntfuggly.todo-tree"
23+
],
24+
"settings": {
25+
"go.toolsManagement.autoUpdate": true,
26+
"go.useLanguageServer": true,
27+
"go.gopath": "/go"
28+
}
29+
}
30+
}
31+
}

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
go-version:
1616
- "1.20"
1717
os:
18-
- ubuntu-20.04
18+
- ubuntu-latest
1919
- macos-latest
2020
runs-on: ${{ matrix.os }}
2121
steps:

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
*.swp
22
build/*
33
.idea/
4-
.devcontainer/

Dockerfile

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
FROM golang:1.18-buster AS build
22

3-
ADD go.mod /promql-cli/go.mod
4-
ADD go.sum /promql-cli/go.sum
3+
COPY go.mod /promql-cli/go.mod
4+
COPY go.sum /promql-cli/go.sum
55

66
WORKDIR /promql-cli/
77
RUN go mod download
88

9-
RUN apt-get update && apt-get install -y make
10-
11-
ADD ./ /promql-cli/
9+
COPY ./ /promql-cli/
1210
ARG TARGETARCH
1311
RUN OS=linux ARCH=${TARGETARCH} INSTALL_PATH=/promql-cli/build/bin/ make install
1412

15-
# TODO explore other base images here.
16-
# Requirements:
17-
# - small
18-
# - correct perms available for mounting and using a config file (config > cmdline flags), right now we just run as root... (see below)
19-
# I'm generally not a fan of alpine/busybox for a cmdline env but maybe minideb or similar?
20-
# Stock deb slim is pretty rad already :)
21-
FROM debian:buster-slim AS promql-cli
22-
COPY --from=build /promql-cli/build/bin/promql /bin/promql
23-
24-
RUN apt-get update \
25-
&& apt-get install -y ca-certificates \
26-
&& rm -rf /var/lib/apt/lists/*
27-
28-
# TODO don't run as root...
29-
ENTRYPOINT [ "/bin/promql" ]
13+
FROM gcr.io/distroless/base-debian11:nonroot AS promql-cli
14+
COPY --from=build /promql-cli/build/bin/promql /promql
15+
ENTRYPOINT [ "/promql" ]

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ go_memstats_frees_total
147147
148148
```
149149

150+
A query can be provided to narrow the list of metrics returned, for example to return all metrics that have the string `gc` in their name you can run:
151+
152+
```
153+
➜ ~ promql metrics '{__name__=~".+gc.+"}'
154+
METRICS
155+
go_gc_duration_seconds
156+
go_gc_duration_seconds_count
157+
go_gc_duration_seconds_sum
158+
go_memstats_gc_sys_bytes
159+
go_memstats_last_gc_time_seconds
160+
go_memstats_next_gc_bytes
161+
prometheus_tsdb_head_gc_duration_seconds_count
162+
prometheus_tsdb_head_gc_duration_seconds_sum
163+
```
164+
150165
You can also view the metadata information for a metric (or all metrics) with the `promql meta` command.
151166

152167
```

cmd/metrics.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,12 +22,18 @@ import (
2222

2323
// metricsCmd represents the metrics command
2424
var metricsCmd = &cobra.Command{
25-
Use: "metrics",
26-
Short: "Get a list of all prometheus metric names",
27-
Long: `Get a list of all prometheus metric names`,
25+
Use: "metrics [query_string]",
26+
Short: "Get a list of prometheus metric names matching the provided query",
27+
Long: `Get a list of prometheus metric names matching the provided query. If no query is provided, all metric names will be returned.`,
2828
Run: func(cmd *cobra.Command, args []string) {
29-
var r writer.MetaResult
30-
result, err := pql.MetaQuery("")
29+
var r writer.SeriesResult
30+
if query == "" {
31+
query = `{job=~".+"}`
32+
}
33+
result, warnings, err := pql.SeriesQuery(query)
34+
if len(warnings) > 0 {
35+
errlog.Printf("Warnings: %v\n", warnings)
36+
}
3137
if err != nil {
3238
errlog.Fatalln(err)
3339
}

cmd/root.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ var rootCmd = &cobra.Command{
5656
pql.Auth.Credentials = config.Secret(viper.GetString("auth-credentials"))
5757
pql.Auth.CredentialsFile = viper.GetString("auth-credentials-file")
5858
pql.TLSConfig = config.TLSConfig{
59-
CAFile: viper.GetString("tls_config.ca_cert_file"),
60-
CertFile: viper.GetString("tls_config.cert_file"),
61-
KeyFile: viper.GetString("tls_config.key_file"),
62-
ServerName: viper.GetString("tls_config.servername"),
59+
CAFile: viper.GetString("tls_config.ca_cert_file"),
60+
CertFile: viper.GetString("tls_config.cert_file"),
61+
KeyFile: viper.GetString("tls_config.key_file"),
62+
ServerName: viper.GetString("tls_config.servername"),
6363
InsecureSkipVerify: viper.GetBool("tls_config.insecure_skip_verify"),
6464
}
6565

@@ -167,23 +167,26 @@ func init() {
167167
if err := viper.BindPFlag("auth-credentials-file", rootCmd.PersistentFlags().Lookup("auth-credentials-file")); err != nil {
168168
errlog.Fatalln(err)
169169
}
170-
rootCmd.PersistentFlags().String("tls_config.ca_cert_file","","CA cert Path for TLS config")
171-
if err := viper.BindPFlag("tls_config.ca_cert_file",rootCmd.PersistentFlags().Lookup("tls_config.ca_cert_file")); err != nil {
170+
rootCmd.PersistentFlags().String("tls_config.ca_cert_file", "", "CA cert Path for TLS config")
171+
if err := viper.BindPFlag("tls_config.ca_cert_file", rootCmd.PersistentFlags().Lookup("tls_config.ca_cert_file")); err != nil {
172172
errlog.Fatalln(err)
173173
}
174-
rootCmd.PersistentFlags().String("tls_config.cert_file","","client cert Path for TLS config")
175-
if err := viper.BindPFlag("tls_config.cert_file",rootCmd.PersistentFlags().Lookup("tls_config.cert_file")); err != nil {
174+
rootCmd.PersistentFlags().String("tls_config.cert_file", "", "client cert Path for TLS config")
175+
if err := viper.BindPFlag("tls_config.cert_file", rootCmd.PersistentFlags().Lookup("tls_config.cert_file")); err != nil {
176176
errlog.Fatalln(err)
177177
}
178-
rootCmd.PersistentFlags().String("tls_config.key_file","","client key for TLS config")
179-
if err := viper.BindPFlag("tls_config.key_file",rootCmd.PersistentFlags().Lookup("tls_config.key_file")); err != nil {
178+
rootCmd.PersistentFlags().String("tls_config.key_file", "", "client key for TLS config")
179+
if err := viper.BindPFlag("tls_config.key_file", rootCmd.PersistentFlags().Lookup("tls_config.key_file")); err != nil {
180180
errlog.Fatalln(err)
181181
}
182-
rootCmd.PersistentFlags().String("tls_config.servername","","server name for TLS config")
183-
if err := viper.BindPFlag("tls_config.servername",rootCmd.PersistentFlags().Lookup("tls_config.servername")); err != nil {
182+
rootCmd.PersistentFlags().String("tls_config.servername", "", "server name for TLS config")
183+
if err := viper.BindPFlag("tls_config.servername", rootCmd.PersistentFlags().Lookup("tls_config.servername")); err != nil {
184+
errlog.Fatalln(err)
185+
}
186+
rootCmd.PersistentFlags().Bool("tls_config.insecure_skip_verify", false, "disable the TLS verification of server certificates.")
187+
if err := viper.BindPFlag("tls_config.insecure_skip_verify", rootCmd.PersistentFlags().Lookup("tls_config.insecure_skip_verify")); err != nil {
184188
errlog.Fatalln(err)
185189
}
186-
rootCmd.PersistentFlags().Bool("tls_config.insecure_skip_verify",false,"disable the TLS verification of server certificates.")
187190
}
188191

189192
// initConfig reads in config file and ENV variables if set.

docker-compose.dev.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.8'
2+
services:
3+
promql:
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
target: build
8+
volumes:
9+
- "./:/promql-cli/"
10+
command: sleep infinity
11+
network_mode: service:prometheus
12+
prometheus:
13+
image: prom/prometheus:latest

0 commit comments

Comments
 (0)