datasource_plugin.go 3.4 KB

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