mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
add hclog wrapper for grafanas logger in plugins
This commit is contained in:
@@ -68,13 +68,12 @@ func (g *GrafanaServerImpl) Start() {
|
||||
login.Init()
|
||||
social.NewOAuthService()
|
||||
plugins.Init()
|
||||
client, err := tsdbplugins.Init()
|
||||
defer client.Kill()
|
||||
|
||||
pluginClient, err := tsdbplugins.Init()
|
||||
if err != nil {
|
||||
g.log.Error("failed to start plugins", "error", err)
|
||||
g.Shutdown(1, "Startup failed")
|
||||
}
|
||||
defer pluginClient.Kill()
|
||||
|
||||
if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil {
|
||||
logger.Error("Failed to provision Grafana from config", "error", err)
|
||||
|
||||
38
pkg/tsdb/plugins/log-wrapper.go
Normal file
38
pkg/tsdb/plugins/log-wrapper.go
Normal 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
|
||||
}
|
||||
4
pkg/tsdb/plugins/mock_tsdb_plugin/Makefile
Normal file
4
pkg/tsdb/plugins/mock_tsdb_plugin/Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
all: build
|
||||
|
||||
build:
|
||||
go build -o simple-plugin .
|
||||
39
pkg/tsdb/plugins/mock_tsdb_plugin/plugin.go
Normal file
39
pkg/tsdb/plugins/mock_tsdb_plugin/plugin.go
Normal 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,
|
||||
})
|
||||
}
|
||||
BIN
pkg/tsdb/plugins/mock_tsdb_plugin/simple-plugin
Executable file
BIN
pkg/tsdb/plugins/mock_tsdb_plugin/simple-plugin
Executable file
Binary file not shown.
@@ -1,8 +1,6 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
@@ -20,47 +18,44 @@ func Init() (*plugin.Client, error) {
|
||||
go get -u google.golang.org/grpc \
|
||||
go get -u github.com/golang/protobuf/{proto,protoc-gen-go} \
|
||||
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
|
||||
*/
|
||||
// initial goal: pass a string object back and forth over grpc.
|
||||
// simplify tsdb req/res message/service
|
||||
|
||||
//lets be silly
|
||||
//plugin path
|
||||
logger := log.New("grafana.plugins")
|
||||
client := plugin.NewClient(&plugin.ClientConfig{
|
||||
HandshakeConfig: shared.Handshake,
|
||||
HandshakeConfig: plugin.HandshakeConfig{
|
||||
ProtocolVersion: 1,
|
||||
MagicCookieKey: "BASIC_PLUGIN",
|
||||
MagicCookieValue: "hello",
|
||||
},
|
||||
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},
|
||||
Logger: logWrapper{logger: logger},
|
||||
})
|
||||
|
||||
// Connect via RPC
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
os.Exit(1)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Request the plugin
|
||||
raw, err := rpcClient.Dispense("kv")
|
||||
raw, err := rpcClient.Dispense("tsdb_mock")
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
//os.Exit(1)
|
||||
//client.Kill()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
plugin := raw.(shared.TsdbPlugin)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -12,12 +12,6 @@ var PluginMap = map[string]plugin.Plugin{
|
||||
"tsdb_mock": &TsdbPluginImpl{},
|
||||
}
|
||||
|
||||
var Handshake = plugin.HandshakeConfig{
|
||||
ProtocolVersion: 1,
|
||||
MagicCookieKey: "BASIC_PLUGIN",
|
||||
MagicCookieValue: "hello",
|
||||
}
|
||||
|
||||
type TsdbPlugin interface {
|
||||
Get(ctx context.Context, req *proto.TsdbRequest) (*proto.TsdbResponse, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user