datasource_plugin.go 3.3 KB

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