datasource_plugin.go 3.2 KB

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