Skip to content

Commit 6817e54

Browse files
Manask322shivanthzen
authored andcommitted
test add headers round tripper
Signed-off-by: manask322 <[email protected]> Signed-off-by: Shivanth <[email protected]>
1 parent 6641349 commit 6817e54

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Diff for: api/prometheus/v1/example_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,45 @@ func ExampleAPI_queryRangeWithAuthBearerToken() {
199199
fmt.Printf("Result:\n%v\n", result)
200200
}
201201

202+
func ExampleAPI_queryRangeWithAuthBearerTokenHeadersRoundTripper() {
203+
client, err := api.NewClient(api.Config{
204+
Address: "http://demo.robustperception.io:9090",
205+
// We can use amazing github.com/prometheus/common/config helper!
206+
RoundTripper: config.NewHeadersRoundTripper(
207+
&config.Headers{
208+
Headers: map[string]config.Header{
209+
"Authorization": {
210+
Values: []string{"Bearer secret"},
211+
},
212+
},
213+
},
214+
api.DefaultRoundTripper,
215+
),
216+
})
217+
if err != nil {
218+
fmt.Printf("Error creating client: %v\n", err)
219+
os.Exit(1)
220+
}
221+
222+
v1api := v1.NewAPI(client)
223+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
224+
defer cancel()
225+
r := v1.Range{
226+
Start: time.Now().Add(-time.Hour),
227+
End: time.Now(),
228+
Step: time.Minute,
229+
}
230+
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
231+
if err != nil {
232+
fmt.Printf("Error querying Prometheus: %v\n", err)
233+
os.Exit(1)
234+
}
235+
if len(warnings) > 0 {
236+
fmt.Printf("Warnings: %v\n", warnings)
237+
}
238+
fmt.Printf("Result:\n%v\n", result)
239+
}
240+
202241
func ExampleAPI_series() {
203242
client, err := api.NewClient(api.Config{
204243
Address: "http://demo.robustperception.io:9090",

0 commit comments

Comments
 (0)