add hclog wrapper for grafanas logger in plugins

This commit is contained in:
bergquist
2017-09-20 17:08:09 +02:00
parent 0f6c470e03
commit 3f5daa5e58
7 changed files with 98 additions and 29 deletions

View File

@@ -68,13 +68,12 @@ func (g *GrafanaServerImpl) Start() {
login.Init() login.Init()
social.NewOAuthService() social.NewOAuthService()
plugins.Init() plugins.Init()
client, err := tsdbplugins.Init() pluginClient, err := tsdbplugins.Init()
defer client.Kill()
if err != nil { if err != nil {
g.log.Error("failed to start plugins", "error", err) g.log.Error("failed to start plugins", "error", err)
g.Shutdown(1, "Startup failed") g.Shutdown(1, "Startup failed")
} }
defer pluginClient.Kill()
if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil { if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil {
logger.Error("Failed to provision Grafana from config", "error", err) logger.Error("Failed to provision Grafana from config", "error", err)

View File

@@ -0,0 +1,38 @@
package plugins
import (
"log"
glog "github.com/grafana/grafana/pkg/log"
hclog "github.com/hashicorp/go-hclog"
)
type logWrapper struct {
logger glog.Logger
}
func (lw logWrapper) Trace(msg string, args ...interface{}) {}
func (lw logWrapper) Debug(msg string, args ...interface{}) {}
func (lw logWrapper) Info(msg string, args ...interface{}) {}
func (lw logWrapper) Warn(msg string, args ...interface{}) {}
func (lw logWrapper) Error(msg string, args ...interface{}) {}
func (lw logWrapper) IsTrace() bool { return true }
func (lw logWrapper) IsDebug() bool { return true }
func (lw logWrapper) IsInfo() bool { return true }
func (lw logWrapper) IsWarn() bool { return true }
func (lw logWrapper) IsError() bool { return true }
func (lw logWrapper) With(args ...interface{}) hclog.Logger {
return logWrapper{logger: glog.New("logger", args)}
}
func (lw logWrapper) Named(name string) hclog.Logger {
return logWrapper{logger: glog.New(name)}
}
func (lw logWrapper) ResetNamed(name string) hclog.Logger {
return logWrapper{logger: glog.New(name)}
}
func (lw logWrapper) StandardLogger(ops *hclog.StandardLoggerOptions) *log.Logger {
return nil
}

View File

@@ -0,0 +1,4 @@
all: build
build:
go build -o simple-plugin .

View File

@@ -0,0 +1,39 @@
package main
import (
"golang.org/x/net/context"
"log"
"github.com/grafana/grafana/pkg/tsdb/plugins/proto"
shared "github.com/grafana/grafana/pkg/tsdb/plugins/shared"
plugin "github.com/hashicorp/go-plugin"
)
type Tsdb struct {
plugin.NetRPCUnsupportedPlugin
}
func (Tsdb) Get(ctx context.Context, req *proto.TsdbRequest) (*proto.TsdbResponse, error) {
log.Print("Tsdb.Get() from plugin")
return &proto.TsdbResponse{
MetaJson: "from plugins! meta meta",
}, nil
}
func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
},
Plugins: map[string]plugin.Plugin{
"tsdb_mock": &shared.TsdbPluginImpl{Plugin: &Tsdb{}},
},
// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}

Binary file not shown.

View File

@@ -1,8 +1,6 @@
package plugins package plugins
import ( import (
"fmt"
"os"
"os/exec" "os/exec"
"golang.org/x/net/context" "golang.org/x/net/context"
@@ -20,47 +18,44 @@ func Init() (*plugin.Client, error) {
go get -u google.golang.org/grpc \ go get -u google.golang.org/grpc \
go get -u github.com/golang/protobuf/{proto,protoc-gen-go} \ go get -u github.com/golang/protobuf/{proto,protoc-gen-go} \
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
*/ */
/* /*
Generate
sudo protoc --go_out=. *.proto
protoc --go_out=plugins=grpc:. *.proto protoc --go_out=plugins=grpc:. *.proto
*/ */
// initial goal: pass a string object back and forth over grpc. logger := log.New("grafana.plugins")
// simplify tsdb req/res message/service
//lets be silly
//plugin path
client := plugin.NewClient(&plugin.ClientConfig{ client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: shared.Handshake, HandshakeConfig: plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
},
Plugins: shared.PluginMap, Plugins: shared.PluginMap,
Cmd: exec.Command("sh", "-c", "/home/carl/go/src/github.com/grafana/simple-plugin/simple-plugin"), Cmd: exec.Command("sh", "-c", "/home/carl/go/src/github.com/grafana/grafana/pkg/tsdb/plugins/mock_tsdb_plugin/simple-plugin"),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Logger: logWrapper{logger: logger},
}) })
// Connect via RPC // Connect via RPC
rpcClient, err := client.Client() rpcClient, err := client.Client()
if err != nil { if err != nil {
fmt.Println("Error:", err.Error()) return nil, err
os.Exit(1)
} }
// Request the plugin // Request the plugin
raw, err := rpcClient.Dispense("kv") raw, err := rpcClient.Dispense("tsdb_mock")
if err != nil { if err != nil {
fmt.Println("Error:", err.Error())
//os.Exit(1)
//client.Kill()
return nil, err return nil, err
} }
plugin := raw.(shared.TsdbPlugin) plugin := raw.(shared.TsdbPlugin)
response, err := plugin.Get(context.Background(), &proto.TsdbRequest{}) response, err := plugin.Get(context.Background(), &proto.TsdbRequest{})
log.Error2("got response from plugin. ", "response", response) if err != nil {
logger.Error("Response from plugin. ", "response", response)
} else {
logger.Info("Response from plugin. ", "response", response)
}
return client, nil return client, nil
} }

View File

@@ -12,12 +12,6 @@ var PluginMap = map[string]plugin.Plugin{
"tsdb_mock": &TsdbPluginImpl{}, "tsdb_mock": &TsdbPluginImpl{},
} }
var Handshake = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
type TsdbPlugin interface { type TsdbPlugin interface {
Get(ctx context.Context, req *proto.TsdbRequest) (*proto.TsdbResponse, error) Get(ctx context.Context, req *proto.TsdbRequest) (*proto.TsdbResponse, error)
} }