datasource_plugin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "time"
  12. "github.com/grafana/grafana/pkg/log"
  13. "github.com/grafana/grafana/pkg/models"
  14. "github.com/grafana/grafana/pkg/plugins/backend"
  15. "github.com/grafana/grafana/pkg/tsdb"
  16. shared "github.com/grafana/grafana/pkg/tsdb/models/proxy"
  17. plugin "github.com/hashicorp/go-plugin"
  18. )
  19. type DataSourcePlugin struct {
  20. FrontendPluginBase
  21. Annotations bool `json:"annotations"`
  22. Metrics bool `json:"metrics"`
  23. Alerting bool `json:"alerting"`
  24. QueryOptions map[string]bool `json:"queryOptions,omitempty"`
  25. BuiltIn bool `json:"builtIn,omitempty"`
  26. Mixed bool `json:"mixed,omitempty"`
  27. HasQueryHelp bool `json:"hasQueryHelp,omitempty"`
  28. Routes []*AppPluginRoute `json:"routes"`
  29. Backend bool `json:"backend,omitempty"`
  30. Executable string `json:"executable,omitempty"`
  31. log log.Logger
  32. client *plugin.Client
  33. }
  34. func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
  35. if err := decoder.Decode(&p); err != nil {
  36. return err
  37. }
  38. if err := p.registerPlugin(pluginDir); err != nil {
  39. return err
  40. }
  41. // look for help markdown
  42. helpPath := filepath.Join(p.PluginDir, "QUERY_HELP.md")
  43. if _, err := os.Stat(helpPath); os.IsNotExist(err) {
  44. helpPath = filepath.Join(p.PluginDir, "query_help.md")
  45. }
  46. if _, err := os.Stat(helpPath); err == nil {
  47. p.HasQueryHelp = true
  48. }
  49. DataSources[p.Id] = p
  50. return nil
  51. }
  52. var handshakeConfig = plugin.HandshakeConfig{
  53. ProtocolVersion: 1,
  54. MagicCookieKey: "GRAFANA_BACKEND_DATASOURCE",
  55. MagicCookieValue: "55d2200a-6492-493a-9353-73b728d468aa",
  56. }
  57. func buildExecutablePath(pluginDir, executable, os, arch string) string {
  58. return path.Join(pluginDir, fmt.Sprintf("%s_%s_%s", executable, strings.ToLower(os), strings.ToLower(arch)))
  59. }
  60. func (p *DataSourcePlugin) initBackendPlugin(log log.Logger) error {
  61. p.log = log.New("plugin-id", p.Id)
  62. p.spawnSubProcess()
  63. go p.reattachKilledProcess()
  64. return nil
  65. }
  66. func (p *DataSourcePlugin) spawnSubProcess() error {
  67. cmd := buildExecutablePath(p.PluginDir, p.Executable, runtime.GOOS, runtime.GOARCH)
  68. p.client = plugin.NewClient(&plugin.ClientConfig{
  69. HandshakeConfig: handshakeConfig,
  70. Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
  71. Cmd: exec.Command(cmd),
  72. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  73. Logger: backend.LogWrapper{Logger: p.log},
  74. })
  75. rpcClient, err := p.client.Client()
  76. if err != nil {
  77. return err
  78. }
  79. raw, err := rpcClient.Dispense(p.Id)
  80. if err != nil {
  81. return err
  82. }
  83. plugin := raw.(shared.TsdbPlugin)
  84. tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  85. return &shared.DatasourcePluginWrapper{TsdbPlugin: plugin}, nil
  86. })
  87. return nil
  88. }
  89. func (p *DataSourcePlugin) reattachKilledProcess() {
  90. ticker := time.NewTicker(time.Second * 1)
  91. for {
  92. select {
  93. case <-ticker.C:
  94. if p.client.Exited() {
  95. err := p.spawnSubProcess()
  96. if err != nil {
  97. p.log.Error("Failed to spawn subprocess")
  98. }
  99. }
  100. }
  101. }
  102. }
  103. func (p *DataSourcePlugin) Kill() {
  104. if p.client != nil {
  105. p.client.Kill()
  106. }
  107. }