datasource_plugin.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package plugins
  2. import (
  3. "context"
  4. "encoding/json"
  5. "os/exec"
  6. "path"
  7. "time"
  8. "github.com/grafana/grafana-plugin-model/go/datasource"
  9. "github.com/grafana/grafana/pkg/infra/log"
  10. "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
  12. "github.com/grafana/grafana/pkg/tsdb"
  13. plugin "github.com/hashicorp/go-plugin"
  14. )
  15. // DataSourcePlugin contains all metadata about a datasource plugin
  16. type DataSourcePlugin struct {
  17. FrontendPluginBase
  18. Annotations bool `json:"annotations"`
  19. Metrics bool `json:"metrics"`
  20. Alerting bool `json:"alerting"`
  21. Explore bool `json:"explore"`
  22. Table bool `json:"tables"`
  23. HiddenQueries bool `json:"hiddenQueries"`
  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. Routes []*AppPluginRoute `json:"routes"`
  29. Streaming bool `json:"streaming"`
  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. DataSources[p.Id] = p
  43. return nil
  44. }
  45. var handshakeConfig = plugin.HandshakeConfig{
  46. ProtocolVersion: 1,
  47. MagicCookieKey: "grafana_plugin_type",
  48. MagicCookieValue: "datasource",
  49. }
  50. func (p *DataSourcePlugin) startBackendPlugin(ctx context.Context, log log.Logger) error {
  51. p.log = log.New("plugin-id", p.Id)
  52. err := p.spawnSubProcess()
  53. if err == nil {
  54. go p.restartKilledProcess(ctx)
  55. }
  56. return err
  57. }
  58. func (p *DataSourcePlugin) spawnSubProcess() error {
  59. cmd := ComposePluginStartCommmand(p.Executable)
  60. fullpath := path.Join(p.PluginDir, cmd)
  61. p.client = plugin.NewClient(&plugin.ClientConfig{
  62. HandshakeConfig: handshakeConfig,
  63. Plugins: map[string]plugin.Plugin{p.Id: &datasource.DatasourcePluginImpl{}},
  64. Cmd: exec.Command(fullpath),
  65. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  66. Logger: LogWrapper{Logger: p.log},
  67. })
  68. rpcClient, err := p.client.Client()
  69. if err != nil {
  70. return err
  71. }
  72. raw, err := rpcClient.Dispense(p.Id)
  73. if err != nil {
  74. return err
  75. }
  76. plugin := raw.(datasource.DatasourcePlugin)
  77. tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  78. return wrapper.NewDatasourcePluginWrapper(p.log, plugin), nil
  79. })
  80. return nil
  81. }
  82. func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
  83. ticker := time.NewTicker(time.Second * 1)
  84. for {
  85. select {
  86. case <-ctx.Done():
  87. return ctx.Err()
  88. case <-ticker.C:
  89. if p.client.Exited() {
  90. err := p.spawnSubProcess()
  91. p.log.Debug("Spawning new sub process", "name", p.Name, "id", p.Id)
  92. if err != nil {
  93. p.log.Error("Failed to spawn subprocess")
  94. }
  95. }
  96. }
  97. }
  98. }
  99. func (p *DataSourcePlugin) Kill() {
  100. if p.client != nil {
  101. p.log.Debug("Killing subprocess ", "name", p.Name)
  102. p.client.Kill()
  103. }
  104. }