2019-08-08 05:53:38 +03:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package v1_test provides examples making requests to Prometheus using the
|
|
|
|
// Golang client.
|
|
|
|
package v1_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-11-20 22:58:16 +03:00
|
|
|
"net/http"
|
2019-08-08 05:53:38 +03:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-06-17 10:04:06 +03:00
|
|
|
"github.com/prometheus/common/config"
|
|
|
|
|
2019-08-08 05:53:38 +03:00
|
|
|
"github.com/prometheus/client_golang/api"
|
|
|
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
|
|
)
|
|
|
|
|
2019-10-15 14:43:53 +03:00
|
|
|
func ExampleAPI_query() {
|
2019-08-08 05:53:38 +03:00
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error creating client: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:18:44 +03:00
|
|
|
v1api := v1.NewAPI(client)
|
2019-08-08 05:53:38 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
2022-04-21 08:23:16 +03:00
|
|
|
result, warnings, err := v1api.Query(ctx, "up", time.Now(), v1.WithTimeout(5*time.Second))
|
2019-08-08 05:53:38 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:43:53 +03:00
|
|
|
func ExampleAPI_queryRange() {
|
2019-08-08 05:53:38 +03:00
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error creating client: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:18:44 +03:00
|
|
|
v1api := v1.NewAPI(client)
|
2019-08-08 05:53:38 +03:00
|
|
|
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,
|
|
|
|
}
|
2022-04-21 08:23:16 +03:00
|
|
|
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r, v1.WithTimeout(5*time.Second))
|
2020-11-20 22:58:16 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type userAgentRoundTripper struct {
|
|
|
|
name string
|
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip implements the http.RoundTripper interface.
|
|
|
|
func (u userAgentRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
if r.UserAgent() == "" {
|
|
|
|
// The specification of http.RoundTripper says that it shouldn't mutate
|
|
|
|
// the request so make a copy of req.Header since this is all that is
|
|
|
|
// modified.
|
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
r2.Header = make(http.Header)
|
|
|
|
for k, s := range r.Header {
|
|
|
|
r2.Header[k] = s
|
|
|
|
}
|
|
|
|
r2.Header.Set("User-Agent", u.name)
|
|
|
|
r = r2
|
|
|
|
}
|
|
|
|
return u.rt.RoundTrip(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleAPI_queryRangeWithUserAgent() {
|
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
RoundTripper: userAgentRoundTripper{name: "Client-Golang", rt: 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_queryRangeWithBasicAuth() {
|
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
// We can use amazing github.com/prometheus/common/config helper!
|
2023-09-21 14:31:08 +03:00
|
|
|
RoundTripper: config.NewBasicAuthRoundTripper("me", "definitely_me", "", api.DefaultRoundTripper),
|
2020-11-20 22:58:16 +03:00
|
|
|
})
|
|
|
|
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_queryRangeWithAuthBearerToken() {
|
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
// We can use amazing github.com/prometheus/common/config helper!
|
2021-03-11 22:14:20 +03:00
|
|
|
RoundTripper: config.NewAuthorizationCredentialsRoundTripper("Bearer", "secret_token", api.DefaultRoundTripper),
|
2020-11-20 22:58:16 +03:00
|
|
|
})
|
|
|
|
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)
|
2019-08-08 05:53:38 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-15 14:43:53 +03:00
|
|
|
func ExampleAPI_series() {
|
2019-08-08 05:53:38 +03:00
|
|
|
client, err := api.NewClient(api.Config{
|
|
|
|
Address: "http://demo.robustperception.io:9090",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error creating client: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:18:44 +03:00
|
|
|
v1api := v1.NewAPI(client)
|
2019-08-08 05:53:38 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
2019-12-09 15:18:44 +03:00
|
|
|
lbls, warnings, err := v1api.Series(ctx, []string{
|
2019-08-08 05:53:38 +03:00
|
|
|
"{__name__=~\"scrape_.+\",job=\"node\"}",
|
|
|
|
"{__name__=~\"scrape_.+\",job=\"prometheus\"}",
|
|
|
|
}, time.Now().Add(-time.Hour), time.Now())
|
|
|
|
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.Println("Result:")
|
|
|
|
for _, lbl := range lbls {
|
|
|
|
fmt.Println(lbl)
|
|
|
|
}
|
|
|
|
}
|