forked from FUSAKLA/autograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
113 lines (107 loc) · 2.92 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/fusakla/autograf/pkg/generator"
"github.com/fusakla/autograf/pkg/grafana"
"github.com/fusakla/autograf/pkg/prometheus"
)
type AuthenticatedTransport struct {
http.RoundTripper
bearerToken string
}
func (at *AuthenticatedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+at.bearerToken)
return at.RoundTripper.RoundTrip(req)
}
func openInBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
}
return err
}
func (r *Command) Run(ctx *Context) error {
var metrics map[string]*generator.Metric
if r.MetricsFile != "" {
var data []byte
var err error
if r.MetricsFile == "-" {
data, err = io.ReadAll(os.Stdin)
} else {
data, err = os.ReadFile(strings.TrimSpace(r.MetricsFile))
}
if err != nil {
return err
}
metrics, err = prometheus.ParseMetricsText(data, r.OpenMetricsFormat)
if err != nil {
return err
}
} else if r.PrometheusURL != "" {
tr := http.DefaultTransport
if r.PrometheusBearerToken != "" {
tr = &AuthenticatedTransport{RoundTripper: tr, bearerToken: r.PrometheusBearerToken}
}
client, err := prometheus.NewClient(ctx.logger, strings.TrimSpace(r.PrometheusURL), tr)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
metrics, err = client.MetricsForSelector(ctx, strings.TrimSpace(r.Selector))
if err != nil {
return err
}
} else {
return fmt.Errorf("At least one of inputs metrics file or Prometheus URL is required")
}
if err := prometheus.ProcessMetrics(metrics); err != nil {
return err
}
pseudoDashboard := generator.NewPseudoDashboardFromMetrics(metrics)
grafanaDashboard, err := grafana.NewDashboard(strings.TrimSpace(r.GrafanaDashboardName), strings.TrimSpace(r.GrafanaDataSource), strings.TrimSpace(r.Selector), r.GrafanaVariables, pseudoDashboard)
if err != nil {
return err
}
if r.GrafanaURL != "" {
if r.grafanaToken == "" {
return fmt.Errorf("you have to specify the GRAFANA_TOKEN variable")
}
cli := grafana.NewClient(r.GrafanaURL, r.grafanaToken)
folderUid, err := cli.EnsureFolder(strings.TrimSpace(r.GrafanaFolder))
if err != nil {
return err
}
dashboardUrl, err := cli.UpsertDashboard(folderUid, grafanaDashboard)
if err != nil {
return err
}
fmt.Println("Dashboard successfully generated, see " + dashboardUrl)
if r.OpenBrowser {
if err := openInBrowser(dashboardUrl); err != nil {
return err
}
}
} else {
jsonData, err := json.Marshal(grafanaDashboard)
if err != nil {
return err
}
fmt.Println(string(jsonData))
}
return nil
}