Implement internal plugin metric collection

This commit is contained in:
Alexander Zobnin
2022-04-28 13:08:53 +03:00
parent 6f41260acf
commit 8b0174f8a7
6 changed files with 307 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
"github.com/alexanderzobnin/grafana-zabbix/pkg/settings"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
@@ -104,6 +105,7 @@ func (ds *ZabbixDatasource) CheckHealth(ctx context.Context, req *backend.CheckH
}
func (ds *ZabbixDatasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
metrics.DataSourceQueryTotal.WithLabelValues("metrics").Inc()
qdr := backend.NewQueryDataResponse()
zabbixDS, err := ds.getDSInstance(req.PluginContext)

53
pkg/metrics/metrics.go Normal file
View File

@@ -0,0 +1,53 @@
// This package contains internal plugin metrics exposed
// by Grafana at /api/plugins/alexanderzobnin-zabbix-datasource/metrics
// in Prometheus format
package metrics
import "github.com/prometheus/client_golang/prometheus"
var (
// DataSourceQueryTotal is metric counter for getting total number of data source queries
DataSourceQueryTotal *prometheus.CounterVec
// ZabbixAPIQueryTotal is metric counter for getting total number of zabbix API queries
ZabbixAPIQueryTotal *prometheus.CounterVec
// CacheHitTotal is metric counter for getting total number of cache hits for requests
CacheHitTotal *prometheus.CounterVec
)
func init() {
DataSourceQueryTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "datasource_query_total",
Help: "Total number of data source queries.",
Namespace: "zabbix_datasource",
},
[]string{"query_type"},
)
ZabbixAPIQueryTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "zabbix_api_query_total",
Help: "Total number of Zabbix API queries.",
Namespace: "zabbix_datasource",
},
[]string{"method"},
)
CacheHitTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cache_hit_total",
Help: "Total number of cache hits.",
Namespace: "zabbix_datasource",
},
[]string{"method"},
)
prometheus.MustRegister(
DataSourceQueryTotal,
ZabbixAPIQueryTotal,
CacheHitTotal,
)
}

View File

@@ -5,6 +5,7 @@ import (
"strings"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
"github.com/alexanderzobnin/grafana-zabbix/pkg/settings"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
"github.com/bitly/go-simplejson"
@@ -67,6 +68,7 @@ func (ds *Zabbix) Request(ctx context.Context, apiReq *ZabbixAPIRequest) (*simpl
ds.cache.SetAPIRequest(apiReq, resultJson)
}
} else {
metrics.CacheHitTotal.WithLabelValues(apiReq.Method).Inc()
var ok bool
resultJson, ok = cachedResult.(*simplejson.Json)
if !ok {

View File

@@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"github.com/alexanderzobnin/grafana-zabbix/pkg/metrics"
"github.com/bitly/go-simplejson"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"golang.org/x/net/context/ctxhttp"
@@ -106,6 +107,8 @@ func (api *ZabbixAPI) request(ctx context.Context, method string, params ZabbixA
return nil, err
}
metrics.ZabbixAPIQueryTotal.WithLabelValues(method).Inc()
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Grafana/grafana-zabbix")