datasource_plugin.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package plugins
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "time"
  13. "github.com/grafana/grafana/pkg/log"
  14. "github.com/grafana/grafana/pkg/models"
  15. shared "github.com/grafana/grafana/pkg/plugins/datasource/tsdb"
  16. "github.com/grafana/grafana/pkg/tsdb"
  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_plugin_type",
  55. MagicCookieValue: "datasource",
  56. }
  57. func composeBinaryName(executable, os, arch string) string {
  58. var extension string
  59. os = strings.ToLower(os)
  60. if os == "windows" {
  61. extension = ".exe"
  62. }
  63. return fmt.Sprintf("%s_%s_%s%s", executable, os, strings.ToLower(arch), extension)
  64. }
  65. func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error {
  66. p.log = log.New("plugin-id", p.Id)
  67. err := p.spawnSubProcess()
  68. if err == nil {
  69. go p.restartKilledProcess(ctx)
  70. }
  71. return err
  72. }
  73. func (p *DataSourcePlugin) spawnSubProcess() error {
  74. cmd := composeBinaryName(p.Executable, runtime.GOOS, runtime.GOARCH)
  75. fullpath := path.Join(p.PluginDir, cmd)
  76. p.client = plugin.NewClient(&plugin.ClientConfig{
  77. HandshakeConfig: handshakeConfig,
  78. Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
  79. Cmd: exec.Command(fullpath),
  80. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  81. Logger: LogWrapper{Logger: p.log},
  82. })
  83. rpcClient, err := p.client.Client()
  84. if err != nil {
  85. return err
  86. }
  87. raw, err := rpcClient.Dispense(p.Id)
  88. if err != nil {
  89. return err
  90. }
  91. plugin := raw.(shared.TsdbPlugin)
  92. tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  93. return &shared.DatasourcePluginWrapper{TsdbPlugin: plugin}, nil
  94. })
  95. return nil
  96. }
  97. func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
  98. ticker := time.NewTicker(time.Second * 1)
  99. for {
  100. select {
  101. case <-ctx.Done():
  102. return ctx.Err()
  103. case <-ticker.C:
  104. if p.client.Exited() {
  105. err := p.spawnSubProcess()
  106. p.log.Debug("Spawning new sub process", "name", p.Name, "id", p.Id)
  107. if err != nil {
  108. p.log.Error("Failed to spawn subprocess")
  109. }
  110. }
  111. }
  112. }
  113. }
  114. func (p *DataSourcePlugin) Kill() {
  115. if p.client != nil {
  116. p.log.Debug("Killing subprocess ", "name", p.Name)
  117. p.client.Kill()
  118. }
  119. }