Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 101 additions & 97 deletions api/prometheus/v1/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"os"
"time"

"github.com/prometheus/common/config"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
Expand Down Expand Up @@ -133,111 +131,117 @@ func ExampleAPI_queryRangeWithUserAgent() {
fmt.Printf("Result:\n%v\n", result)
}

// NOTE: This example is commented to avoid adding unnecessary dependencies to client_golang.
// To run the example, please uncomment the commented lines.
func ExampleAPI_queryRangeWithBasicAuth() {
client, err := api.NewClient(api.Config{
Address: DemoPrometheusURL,
// We can use amazing github.com/prometheus/common/config helper!
RoundTripper: config.NewBasicAuthRoundTripper(
config.NewInlineSecret("me"),
config.NewInlineSecret("definitely_me"),
api.DefaultRoundTripper,
),
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
// client, err := api.NewClient(api.Config{
// Address: DemoPrometheusURL,
// // We can use amazing github.com/prometheus/common/config helper!
// RoundTripper: config.NewBasicAuthRoundTripper(
// config.NewInlineSecret("me"),
// config.NewInlineSecret("definitely_me"),
// api.DefaultRoundTripper,
// ),
// })
// if err != nil {
// fmt.Printf("Error creating client: %v\n", err)
// os.Exit(1)
// }

v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r := v1.Range{
Start: time.Now().Add(-time.Hour),
End: time.Now(),
Step: time.Minute,
}
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
fmt.Printf("Result:\n%v\n", result)
// v1api := v1.NewAPI(client)
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// r := v1.Range{
// Start: time.Now().Add(-time.Hour),
// End: time.Now(),
// Step: time.Minute,
// }
// result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
// if err != nil {
// fmt.Printf("Error querying Prometheus: %v\n", err)
// os.Exit(1)
// }
// if len(warnings) > 0 {
// fmt.Printf("Warnings: %v\n", warnings)
// }
// fmt.Printf("Result:\n%v\n", result)
}

// NOTE: This example is commented to avoid adding unnecessary dependencies to client_golang.
// To run the example, please uncomment the commented lines.
func ExampleAPI_queryRangeWithAuthBearerToken() {
client, err := api.NewClient(api.Config{
Address: DemoPrometheusURL,
// We can use amazing github.com/prometheus/common/config helper!
RoundTripper: config.NewAuthorizationCredentialsRoundTripper(
"Bearer",
config.NewInlineSecret("secret_token"),
api.DefaultRoundTripper,
),
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
// client, err := api.NewClient(api.Config{
// Address: DemoPrometheusURL,
// // We can use amazing github.com/prometheus/common/config helper!
// RoundTripper: config.NewAuthorizationCredentialsRoundTripper(
// "Bearer",
// config.NewInlineSecret("secret_token"),
// api.DefaultRoundTripper,
// ),
// })
// if err != nil {
// fmt.Printf("Error creating client: %v\n", err)
// os.Exit(1)
// }

v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r := v1.Range{
Start: time.Now().Add(-time.Hour),
End: time.Now(),
Step: time.Minute,
}
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
fmt.Printf("Result:\n%v\n", result)
// v1api := v1.NewAPI(client)
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// r := v1.Range{
// Start: time.Now().Add(-time.Hour),
// End: time.Now(),
// Step: time.Minute,
// }
// result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
// if err != nil {
// fmt.Printf("Error querying Prometheus: %v\n", err)
// os.Exit(1)
// }
// if len(warnings) > 0 {
// fmt.Printf("Warnings: %v\n", warnings)
// }
// fmt.Printf("Result:\n%v\n", result)
}

// NOTE: This example is commented to avoid adding unnecessary dependencies to client_golang.
// To run the example, please uncomment the commented lines.
func ExampleAPI_queryRangeWithAuthBearerTokenHeadersRoundTripper() {
client, err := api.NewClient(api.Config{
Address: DemoPrometheusURL,
// We can use amazing github.com/prometheus/common/config helper!
RoundTripper: config.NewHeadersRoundTripper(
&config.Headers{
Headers: map[string]config.Header{
"Authorization": {
Values: []string{"Bearer secret"},
},
},
},
api.DefaultRoundTripper,
),
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
// client, err := api.NewClient(api.Config{
// Address: DemoPrometheusURL,
// // We can use amazing github.com/prometheus/common/config helper!
// RoundTripper: config.NewHeadersRoundTripper(
// &config.Headers{
// Headers: map[string]config.Header{
// "Authorization": {
// Values: []string{"Bearer secret"},
// },
// },
// },
// api.DefaultRoundTripper,
// ),
// })
// if err != nil {
// fmt.Printf("Error creating client: %v\n", err)
// os.Exit(1)
// }

v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r := v1.Range{
Start: time.Now().Add(-time.Hour),
End: time.Now(),
Step: time.Minute,
}
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
fmt.Printf("Result:\n%v\n", result)
// v1api := v1.NewAPI(client)
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// defer cancel()
// r := v1.Range{
// Start: time.Now().Add(-time.Hour),
// End: time.Now(),
// Step: time.Minute,
// }
// result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
// if err != nil {
// fmt.Printf("Error querying Prometheus: %v\n", err)
// os.Exit(1)
// }
// if len(warnings) > 0 {
// fmt.Printf("Warnings: %v\n", warnings)
// }
// fmt.Printf("Result:\n%v\n", result)
}

func ExampleAPI_series() {
Expand Down
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ require (
)

require (
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/oauth2 v0.32.0 // indirect
golang.org/x/text v0.30.0 // indirect
)

exclude github.com/prometheus/client_golang v1.12.1
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
Expand All @@ -28,8 +26,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -50,14 +46,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Loading