Documentation
¶
Overview ¶
Package grafana implements grafana client
References:
[Go Reference]: https://pkg.go.dev/badge/github.com/mo-silent/go-devops.svg
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AliGrafana ¶
type AliGrafana struct {
}
AliGrafana implements Alibaba Cloud grafana query and range query methods.
func (AliGrafana) Query ¶
func (ag AliGrafana) Query(ctx context.Context, addr, token, query string, options Options) ([]byte, error)
The Query method is used to query Alibaba Cloud grafana indicator data and return byte slices.
Example ¶
ExampleAliGrafana_Query demonstrates how to use AliGrafana.Query. Open the link to see an example: https://go.dev/play/p/GzFSQA0jut5
// An example of an Alibaba Cloud Grafana query
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
d := devops.NewDevops()
ag := d.AliGrafana()
addr := "https://xxxx.grafana.aliyuncs.com/api/datasources/proxy/:id/api/v1/query"
token := "Bearer xxxxx"
query := `sum by (rpc) (sum_over_time(arms_rpc_requests_count{}[%s]))`
timeframe := "68400s"
end := time.Now().Local().Unix()
query = fmt.Sprintf(query, timeframe)
// query metrics
res, err := ag.Query(ctx, addr, token, query, grafana.Options{To: end})
if err != nil {
log.Errorf("Post metrics data from ali grafana error: %s", err.Error())
}
fmt.Println(string(res))
func (AliGrafana) QueryRange ¶
func (ag AliGrafana) QueryRange(ctx context.Context, addr, token, query string, options Options) ([]byte, error)
The QueryRange method is used to query Alibaba Cloud grafana indicator data range and return byte slices.
Example ¶
ExampleAliGrafana_QueryRange demonstrates how to use AliGrafana.QueryRange. Open the link to see an example: https://go.dev/play/p/N9eFrFvkZmQ
// An example of an Alibaba Cloud Grafana range query
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
// new devops AliGrafana
d := devops.NewDevops()
ag := d.AliGrafana()
addr := "https://xxxx.grafana.aliyuncs.com/api/datasources/proxy/:id/api/v1/query"
token := "Bearer xxxxx"
query := `sum by (rpc) (sum_over_time(arms_rpc_requests_count{}[1m]))`
m, _ := time.ParseDuration("-15m")
now := time.Now().Local()
// options is grafana.Options.
// The example is in the same directory as the structure,
// so it does not have a directory beginning
options := grafana.Options{
From: now.Add(m).Unix(),
To: now.Unix(),
Step: int64(60),
}
// use QueryRange
res, err := ag.QueryRange(ctx, addr, token, query, options)
if err != nil {
log.Errorf("Post metrics data from ali grafana error: %s", err.Error())
}
fmt.Println(string(res))
type Datasource ¶
type Dimensions ¶
type Grafana ¶
type Grafana struct {
}
Grafana implements open-source grafana query and range query methods.
func (Grafana) Query ¶
The Query method is used to query open-source grafana indicator data and return byte slices.
Example ¶
ExampleGrafana_Query demonstrates how to use Grafana.Query. Open the link to see an example: https://go.dev/play/p/akMvLdzZNd-
// An example of an open-source Grafana query
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
// new devops Grafana
d := devops.NewDevops()
ag := d.Grafana()
addr := "https://localhost:3000/api/ds/query"
token := "Bearer xxxx"
query := `{"queries":[{"refId":"A","key":"Q-b34b5ac7-c2d7-44fc-bc77-06570400a564-0","instant":true,"range":false,"exemplar":false,"expr":"sum(rate(kube_pod_container_status_restarts_total{environment=~\"pd\"}[$__range])>=0) by (pod, environment)","datasource":{"uid":"yPdqCed7z","type":"prometheus"},"queryType":"timeSeriesQuery","requestId":"Q-b34b5ac7-c2d7-44fc-bc77-06570400a564-0A","utcOffsetSec":28800,"legendFormat":"","interval":"","datasourceId":34,"intervalMs":15000,"maxDataPoints":2512}],"range":{"from":"2023-04-23T02:40:33.146Z","to":"2023-04-23T03:10:33.146Z","raw":{"from":"now-30m","to":"now"}},"from":"1682217633146","to":"1682219433146"}`
// query metrics
bytes, err := ag.Query(ctx, addr, token, query, grafana.Options{})
if err != nil {
return
}
log.Debug(string(bytes))
type MetricsInterface ¶
type MetricsInterface interface {
Query(ctx context.Context, addr, token, query string, options Options) ([]byte, error)
QueryRange(ctx context.Context, addr, token, query string, options Options) ([]byte, error)
}
MetricsInterface is the interface that implements grafana metrics query and range query methods.
type Queries ¶
type Queries struct {
IntervalMs int `json:"intervalMs"`
MaxDataPoints int `json:"maxDataPoints"`
QueryMode string `json:"queryMode,omitempty"`
Namespace string `json:"namespace,omitempty"`
MetricName string `json:"metricName,omitempty"`
Expression string `json:"expression,omitempty"`
Dimensions Dimensions `json:"dimensions,omitempty"`
Region string `json:"region,omitempty"`
ID string `json:"id,omitempty"`
Alias string `json:"alias,omitempty"`
Statistic string `json:"statistic,omitempty"`
Period string `json:"period,omitempty"`
MetricQueryType int `json:"metricQueryType,omitempty"`
MetricEditorMode int `json:"metricEditorMode,omitempty"`
SQLExpression string `json:"sqlExpression,omitempty"`
RefID string `json:"refId"`
Key string `json:"key,omitempty"`
Instant bool `json:"instant,omitempty"`
Range bool `json:"range,omitempty"`
MatchExact bool `json:"matchExact,omitempty"`
Datasource Datasource `json:"datasource"`
Type string `json:"type,omitempty"`
Exemplar bool `json:"exemplar,omitempty"`
Expr string `json:"expr,omitempty"`
Format string `json:"format,omitempty"`
Interval string `json:"interval,omitempty"`
IntervalFactor int `json:"intervalFactor,omitempty"`
LegendFormat string `json:"legendFormat,omitempty"`
Metric string `json:"metric,omitempty"`
Step int `json:"step,omitempty"`
QueryType string `json:"queryType,omitempty"`
RequestID string `json:"requestId,omitempty"`
UtcOffsetSec int `json:"utcOffsetSec,omitempty"`
DatasourceID int `json:"datasourceId,omitempty"`
RawSQL string `json:"rawSql,omitempty"`
}