plugins.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/tsdb/plugins/proto"
  7. "github.com/grafana/grafana/pkg/tsdb/plugins/shared"
  8. plugin "github.com/hashicorp/go-plugin"
  9. )
  10. func Init() (*plugin.Client, error) {
  11. /*
  12. Setup
  13. go get -u google.golang.org/grpc \
  14. go get -u github.com/golang/protobuf/{proto,protoc-gen-go} \
  15. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
  16. */
  17. /*
  18. protoc --go_out=plugins=grpc:. *.proto
  19. */
  20. logger := log.New("grafana.plugins")
  21. client := plugin.NewClient(&plugin.ClientConfig{
  22. HandshakeConfig: plugin.HandshakeConfig{
  23. ProtocolVersion: 1,
  24. MagicCookieKey: "BASIC_PLUGIN",
  25. MagicCookieValue: "hello",
  26. },
  27. Plugins: shared.PluginMap,
  28. Cmd: exec.Command("sh", "-c", "/home/carl/go/src/github.com/grafana/grafana/pkg/tsdb/plugins/mock_tsdb_plugin/simple-plugin"),
  29. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  30. Logger: logWrapper{logger: logger},
  31. })
  32. // Connect via RPC
  33. rpcClient, err := client.Client()
  34. if err != nil {
  35. return nil, err
  36. }
  37. // Request the plugin
  38. raw, err := rpcClient.Dispense("tsdb_mock")
  39. if err != nil {
  40. return nil, err
  41. }
  42. plugin := raw.(shared.TsdbPlugin)
  43. response, err := plugin.Get(context.Background(), &proto.TsdbRequest{})
  44. if err != nil {
  45. logger.Error("Response from plugin. ", "response", response)
  46. } else {
  47. logger.Info("Response from plugin. ", "response", response)
  48. }
  49. return client, nil
  50. }