datasource_plugin.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package plugins
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "time"
  13. "github.com/grafana/grafana/pkg/log"
  14. "github.com/grafana/grafana/pkg/models"
  15. shared "github.com/grafana/grafana/pkg/plugins/datasource/tsdb"
  16. "github.com/grafana/grafana/pkg/tsdb"
  17. plugin "github.com/hashicorp/go-plugin"
  18. )
  19. type DataSourcePlugin struct {
  20. FrontendPluginBase
  21. Annotations bool `json:"annotations"`
  22. Metrics bool `json:"metrics"`
  23. Alerting bool `json:"alerting"`
  24. QueryOptions map[string]bool `json:"queryOptions,omitempty"`
  25. BuiltIn bool `json:"builtIn,omitempty"`
  26. Mixed bool `json:"mixed,omitempty"`
  27. HasQueryHelp bool `json:"hasQueryHelp,omitempty"`
  28. Routes []*AppPluginRoute `json:"routes"`
  29. Backend bool `json:"backend,omitempty"`
  30. Executable string `json:"executable,omitempty"`
  31. log log.Logger
  32. client *plugin.Client
  33. }
  34. func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
  35. if err := decoder.Decode(&p); err != nil {
  36. return err
  37. }
  38. if err := p.registerPlugin(pluginDir); err != nil {
  39. return err
  40. }
  41. // look for help markdown
  42. helpPath := filepath.Join(p.PluginDir, "QUERY_HELP.md")
  43. if _, err := os.Stat(helpPath); os.IsNotExist(err) {
  44. helpPath = filepath.Join(p.PluginDir, "query_help.md")
  45. }
  46. if _, err := os.Stat(helpPath); err == nil {
  47. p.HasQueryHelp = true
  48. }
  49. DataSources[p.Id] = p
  50. return nil
  51. }
  52. var handshakeConfig = plugin.HandshakeConfig{
  53. ProtocolVersion: 1,
  54. MagicCookieKey: "grafana_plugin_type",
  55. MagicCookieValue: "datasource",
  56. }
  57. func buildExecutablePath(pluginDir, executable, os, arch string) string {
  58. return path.Join(pluginDir, fmt.Sprintf("%s_%s_%s", executable, strings.ToLower(os), strings.ToLower(arch)))
  59. }
  60. func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error {
  61. p.log = log.New("plugin-id", p.Id)
  62. err := p.spawnSubProcess()
  63. if err == nil {
  64. go p.restartKilledProcess(ctx)
  65. }
  66. return err
  67. }
  68. func (p *DataSourcePlugin) spawnSubProcess() error {
  69. cmd := buildExecutablePath(p.PluginDir, p.Executable, runtime.GOOS, runtime.GOARCH)
  70. p.client = plugin.NewClient(&plugin.ClientConfig{
  71. HandshakeConfig: handshakeConfig,
  72. Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
  73. Cmd: exec.Command(cmd),
  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.(shared.TsdbPlugin)
  86. tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  87. return &shared.DatasourcePluginWrapper{TsdbPlugin: 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. }