datasource_plugin.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "github.com/grafana/grafana/pkg/log"
  12. "github.com/grafana/grafana/pkg/models"
  13. "github.com/grafana/grafana/pkg/plugins/backend"
  14. "github.com/grafana/grafana/pkg/tsdb"
  15. shared "github.com/grafana/grafana/pkg/tsdb/models/proxy"
  16. plugin "github.com/hashicorp/go-plugin"
  17. )
  18. type DataSourcePlugin struct {
  19. FrontendPluginBase
  20. Annotations bool `json:"annotations"`
  21. Metrics bool `json:"metrics"`
  22. Alerting bool `json:"alerting"`
  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_BACKEND_DATASOURCE",
  54. MagicCookieValue: "55d2200a-6492-493a-9353-73b728d468aa",
  55. }
  56. func (p *DataSourcePlugin) initBackendPlugin(log log.Logger) error {
  57. p.log = log.New("plugin-id", p.Id)
  58. cmd := path.Join(p.PluginDir, fmt.Sprintf("%s_%s_%s", p.Executable, strings.ToLower(runtime.GOOS), strings.ToLower(runtime.GOARCH)))
  59. p.client = plugin.NewClient(&plugin.ClientConfig{
  60. HandshakeConfig: handshakeConfig,
  61. Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
  62. Cmd: exec.Command(cmd),
  63. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  64. Logger: backend.LogWrapper{Logger: p.log},
  65. })
  66. rpcClient, err := p.client.Client()
  67. if err != nil {
  68. return err
  69. }
  70. raw, err := rpcClient.Dispense(p.Id)
  71. if err != nil {
  72. return err
  73. }
  74. plugin := raw.(shared.TsdbPlugin)
  75. tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
  76. return &shared.TsdbWrapper{TsdbPlugin: plugin}, nil
  77. })
  78. return nil
  79. }
  80. func (p *DataSourcePlugin) Kill() {
  81. if p.client != nil {
  82. p.client.Kill()
  83. }
  84. }