plugins.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package plugins
  2. import (
  3. "os/exec"
  4. "golang.org/x/net/context"
  5. "github.com/grafana/grafana/pkg/log"
  6. "github.com/grafana/grafana/pkg/plugins/backend/shared"
  7. "github.com/grafana/grafana/pkg/tsdb/models"
  8. plugin "github.com/hashicorp/go-plugin"
  9. )
  10. func Init() (*plugin.Client, error) {
  11. /*
  12. setup protoc using https://gist.github.com/bergquist/5df1f201bb605e42538ef40f6ccf82a9
  13. run "protoc --go_out=plugins=grpc:. *.proto" to update proto files
  14. */
  15. logger := log.New("grafana.plugins")
  16. client := plugin.NewClient(&plugin.ClientConfig{
  17. HandshakeConfig: plugin.HandshakeConfig{
  18. ProtocolVersion: 1,
  19. MagicCookieKey: "BASIC_PLUGIN",
  20. MagicCookieValue: "hello",
  21. },
  22. Plugins: shared.PluginMap,
  23. Cmd: exec.Command("sh", "-c", "/home/carl/go/src/github.com/grafana/grafana/pkg/plugins/backend/mock_tsdb_plugin/simple-plugin"),
  24. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  25. Logger: logWrapper{logger: logger},
  26. })
  27. // Connect via RPC
  28. rpcClient, err := client.Client()
  29. if err != nil {
  30. return nil, err
  31. }
  32. // Request the plugin
  33. raw, err := rpcClient.Dispense("tsdb_mock")
  34. if err != nil {
  35. return nil, err
  36. }
  37. plugin := raw.(shared.TsdbPlugin)
  38. response, err := plugin.Get(context.Background(), &proto.TsdbRequest{})
  39. if err != nil {
  40. logger.Error("Response from plugin. ", "response", response)
  41. } else {
  42. logger.Info("Response from plugin. ", "response", response)
  43. }
  44. return client, nil
  45. }