Skip to content

Commit

Permalink
extract metric usage from panel in grafana collector
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis committed Oct 25, 2024
1 parent f702cba commit 08d5e36
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
55 changes: 55 additions & 0 deletions source/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package grafana
import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/go-openapi/strfmt"
grafanaapi "github.com/grafana/grafana-openapi-client-go/client"
Expand All @@ -12,9 +14,22 @@ import (
"github.com/perses/metrics-usage/config"
"github.com/perses/metrics-usage/database"
modelAPIV1 "github.com/perses/metrics-usage/pkg/api/v1"
"github.com/perses/metrics-usage/utils"
"github.com/perses/metrics-usage/utils/prometheus"
"github.com/sirupsen/logrus"
)

var variableReplacer = strings.NewReplacer(

Check failure on line 22 in source/grafana/grafana.go

View workflow job for this annotation

GitHub Actions / lint

var `variableReplacer` is unused (unused)
"$__interval", "5m",
"$interval", "5m",
"$resolution", "5m",
"$__rate_interval", "15s",
"$rate_interval", "15s",
"$__range", "1d",
"${__range_s:glob}", "15",
"${__range_s}", "15",
)

func NewCollector(db database.Database, cfg config.GrafanaCollector) (async.SimpleTask, error) {
httpClient, err := config.NewHTTPClient(cfg.HTTPClient)
url := cfg.HTTPClient.URL.URL
Expand Down Expand Up @@ -63,6 +78,29 @@ func (c *grafanaCollector) Execute(ctx context.Context, _ context.CancelFunc) er
return nil
}

func (c *grafanaCollector) extractMetricUsage(metricUsage map[string]*modelAPIV1.MetricUsage, dashboard *simplifiedDashboard) {

Check failure on line 81 in source/grafana/grafana.go

View workflow job for this annotation

GitHub Actions / lint

func `(*grafanaCollector).extractMetricUsage` is unused (unused)
c.extractMetricUsageFromPanels(metricUsage, dashboard.Panels, dashboard)
for _, r := range dashboard.Rows {
c.extractMetricUsageFromPanels(metricUsage, r.Panels, dashboard)
// TODO extract metric usage from variable
}
}

func (c *grafanaCollector) extractMetricUsageFromPanels(metricUsage map[string]*modelAPIV1.MetricUsage, panels []panel, dashboard *simplifiedDashboard) {

Check failure on line 89 in source/grafana/grafana.go

View workflow job for this annotation

GitHub Actions / lint

func `(*grafanaCollector).extractMetricUsageFromPanels` is unused (unused)
for _, p := range panels {
for _, t := range extractTarget(p) {
if len(t.Expr) == 0 {
continue
}
metrics, err := prometheus.ExtractMetricNamesFromPromQL(replaceVariables(t.Expr))
if err != nil {
logrus.WithError(err).Errorf("failed to extract metric names from PromQL expression in the panel %q for the dashboard %s/%s", p.Title, dashboard.Title, dashboard.UID)
}
c.populateUsage(metricUsage, metrics, dashboard)
}
}
}

func (c *grafanaCollector) getDashboard(uid string) (*simplifiedDashboard, error) {
response, err := c.client.Dashboards.GetDashboardByUID(uid)
if err != nil {
Expand Down Expand Up @@ -101,6 +139,23 @@ func (c *grafanaCollector) collectAllDashboardUID(ctx context.Context) ([]*grafa
return result, nil
}

func (c *grafanaCollector) populateUsage(metricUsage map[string]*modelAPIV1.MetricUsage, metricNames []string, currentDashboard *simplifiedDashboard) {

Check failure on line 142 in source/grafana/grafana.go

View workflow job for this annotation

GitHub Actions / lint

func `(*grafanaCollector).populateUsage` is unused (unused)
dashboardURL := fmt.Sprintf("%s/d/%s", c.grafanaURL, currentDashboard.UID)
for _, metricName := range metricNames {
if usage, ok := metricUsage[metricName]; ok {
usage.Dashboards = utils.InsertIfNotPresent(usage.Dashboards, dashboardURL)
} else {
metricUsage[metricName] = &modelAPIV1.MetricUsage{
Dashboards: []string{dashboardURL},
}
}
}
}

func (c *grafanaCollector) String() string {
return "grafana collector"
}

func replaceVariables(expr string) string {

Check failure on line 159 in source/grafana/grafana.go

View workflow job for this annotation

GitHub Actions / lint

func `replaceVariables` is unused (unused)
return variableReplacer.Replace(expr)
}
18 changes: 8 additions & 10 deletions source/grafana/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ type row struct {
Panels []panel `json:"panels"`
}

type annotation struct {
Name string `json:"name"`
Query string `json:"query"`
Expr string `json:"expr"`
Type string `json:"type"`
}

type templateVar struct {
Name string `json:"name"`
Type string `json:"type"`
Expand All @@ -36,7 +29,12 @@ type simplifiedDashboard struct {
Templating struct {
List []templateVar `json:"list"`
} `json:"templating"`
Annotations struct {
List []annotation `json:"list"`
} `json:"annotations"`
}

func extractTarget(panel panel) []target {

Check failure on line 34 in source/grafana/model.go

View workflow job for this annotation

GitHub Actions / lint

func `extractTarget` is unused (unused)
var targets []target
for _, p := range panel.Panels {
targets = append(targets, extractTarget(p)...)
}
return append(targets, panel.Targets...)
}
16 changes: 8 additions & 8 deletions source/perses/perses.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ func NewCollector(db database.Database, cfg config.PersesCollector) (async.Simpl
return nil, err
}
return &persesCollector{
SimpleTask: nil,
client: persesClientV1.NewWithClient(restClient).Dashboard(""),
db: db,
DashboardURL: cfg.HTTPClient.URL.String(),
SimpleTask: nil,
client: persesClientV1.NewWithClient(restClient).Dashboard(""),
db: db,
persesURL: cfg.HTTPClient.URL.String(),
}, nil
}

type persesCollector struct {
async.SimpleTask
client persesClientV1.DashboardInterface
db database.Database
DashboardURL string
client persesClientV1.DashboardInterface
db database.Database
persesURL string
}

func (c *persesCollector) Execute(_ context.Context, _ context.CancelFunc) error {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (c *persesCollector) extractMetricUsageFromVariables(metricUsage map[string
}

func (c *persesCollector) populateUsage(metricUsage map[string]*modelAPIV1.MetricUsage, metricNames []string, currentDashboard *v1.Dashboard) {
dashboardURL := fmt.Sprintf("%s/api/v1/projects/%s/dashboards/%s", c.DashboardURL, currentDashboard.Metadata.Project, currentDashboard.Metadata.Name)
dashboardURL := fmt.Sprintf("%s/api/v1/projects/%s/dashboards/%s", c.persesURL, currentDashboard.Metadata.Project, currentDashboard.Metadata.Name)
for _, metricName := range metricNames {
if usage, ok := metricUsage[metricName]; ok {
usage.Dashboards = utils.InsertIfNotPresent(usage.Dashboards, dashboardURL)
Expand Down

0 comments on commit 08d5e36

Please sign in to comment.