datasource_plugin.go 3.4 KB

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