test add headers round tripper

Signed-off-by: manask322 <manask322@gmail.com>
This commit is contained in:
manask322 2024-10-20 23:02:32 +05:30
parent e1675ced80
commit 547b8dae6f
1 changed files with 39 additions and 0 deletions

View File

@ -199,6 +199,45 @@ func ExampleAPI_queryRangeWithAuthBearerToken() {
fmt.Printf("Result:\n%v\n", result)
}
func ExampleAPI_queryRangeWithAuthBearerTokenHeadersRoundTripper() {
client, err := api.NewClient(api.Config{
Address: "http://demo.robustperception.io:9090",
// 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)
}
func ExampleAPI_series() {
client, err := api.NewClient(api.Config{
Address: "http://demo.robustperception.io:9090",