Skip to content

Commit

Permalink
cherry pick pingcap#1305 to release-2.0 (pingcap#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Nov 27, 2020
1 parent 72a4669 commit 950a30b
Show file tree
Hide file tree
Showing 9 changed files with 6,420 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285
github.com/tidwall/gjson v1.6.1
github.com/tidwall/sjson v1.1.2
github.com/tikv/pd v1.1.0-beta.0.20200907085700-5b04bec39b99
github.com/uber-go/atomic v1.4.0 // indirect
github.com/unrolled/render v1.0.1
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,18 @@ github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285 h1:uSDYjYejelKy
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tidwall/gjson v1.3.5 h1:2oW9FBNu8qt9jy5URgrzsVx/T/KSn3qn/smJQ0crlDQ=
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws=
github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/sjson v1.1.2 h1:NC5okI+tQ8OG/oyzchvwXXxRxCV/FVdhODbPKkQ25jQ=
github.com/tidwall/sjson v1.1.2/go.mod h1:SEzaDwxiPzKzNfUEO4HbYF/m4UCSJDsGgNqsS1LvdoY=
github.com/tikv/pd v1.1.0-beta.0.20200818122340-ef1a4e920b2f/go.mod h1:mwZ3Lip1YXgtgBx6blADUPMxrqPGCfwABlreDzuJul8=
github.com/tikv/pd v1.1.0-beta.0.20200820084926-bcfa77a7a593/go.mod h1:quwjWtCmawAvS+YdxtSKG08sEexLzkhQgAno59wW+lI=
github.com/tikv/pd v1.1.0-beta.0.20200825070655-6b09f3acbb1f/go.mod h1:quwjWtCmawAvS+YdxtSKG08sEexLzkhQgAno59wW+lI=
Expand Down
11 changes: 11 additions & 0 deletions monitoring/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM busybox

ADD init.sh /usr/bin/init.sh
RUN chmod +x /usr/bin/init.sh

COPY dashboards/*.json /tmp/
COPY rules/*.rules.yml /tmp/
COPY datasources/*.yaml /tmp/

ENTRYPOINT ["/usr/bin/init.sh"]
CMD ["TIDB-Cluster", "/grafana-dashboard-definitions/tidb/", "false", "/etc/prometheus"]
104 changes: 104 additions & 0 deletions monitoring/dashboards/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/pingcap/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

const (
datasourceName = "dm-cluster"
outputDir = "./dashboards"
name = "dm.json"
)

var (
dashboards = map[string]string{
"dm.json": "Test-Cluster-DM",
}
)

func readDashboard(dir string, name string) (string, error) {
file := filepath.Join(dir, name)
data, err := ioutil.ReadFile(file)
if err != nil {
return "", errors.Errorf("File %s do not exists", file)
}
return string(data), nil
}

func writeDashboard(dir string, name string, body string) error {
title, exist := dashboards[name]
if !exist {
return errors.Errorf("%s dashboard is not found in operator", name)
}

writeFile(dir, name, filterDashboard(body, name, title))
return nil
}

func writeFile(baseDir string, fileName string, body string) {
if body == "" {
return
}

fn := filepath.Join(baseDir, fileName)
f, err := os.Create(fn)
checkErr(err, "create file failed, f="+fn)
defer f.Close()

if _, err := f.WriteString(body); err != nil {
checkErr(err, "write file failed, f="+fn)
}
}

func checkErr(err error, msg string) {
if err != nil {
panic(errors.Wrap(err, msg))
}
}

func filterDashboard(str string, dashboard string, title string) string {
// replace grafana item
var err error
r := gjson.Get(str, "__requires.0.type")
if r.Exists() && r.Str == "grafana" {
str, err = sjson.Set(str, "__requires.0.version", "")
checkErr(err, "update links filed failed")
}
// replace links item
if gjson.Get(str, "links").Exists() {
str, err = sjson.Set(str, "links", []struct{}{})
checkErr(err, "update links failed")
}

// replace datasource name
if gjson.Get(str, "__inputs").Exists() && gjson.Get(str, "__inputs.0.name").Exists() {
datasource := gjson.Get(str, "__inputs.0.name").Str
str = strings.ReplaceAll(str, fmt.Sprintf("${%s}", datasource), datasourceName)
}

// delete input defination
if gjson.Get(str, "__inputs").Exists() {
str, err = sjson.Delete(str, "__inputs")
checkErr(err, "delete path failed")
}

// unify the title name
str, err = sjson.Set(str, "title", title)
checkErr(err, "replace title failed")

return str
}

func main() {
str, err := readDashboard(outputDir, name)
checkErr(err, "read dashboard file failed")
checkErr(writeDashboard(outputDir, name, str), "write dashboard file failed")
}
Loading

0 comments on commit 950a30b

Please sign in to comment.