Merge pull request #1657 from Manask322/addHeadersRoundTripperExample

test add headers round tripper
This commit is contained in:
Bartlomiej Plotka 2024-10-21 08:22:36 +02:00 committed by GitHub
commit aef8aedb4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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) 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() { func ExampleAPI_series() {
client, err := api.NewClient(api.Config{ client, err := api.NewClient(api.Config{
Address: "http://demo.robustperception.io:9090", Address: "http://demo.robustperception.io:9090",