-
Notifications
You must be signed in to change notification settings - Fork 0
/
plug.go
83 lines (70 loc) · 1.79 KB
/
plug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package analysis
import (
routing "github.com/gly-hub/fasthttp-routing"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/smallnest/rpcx/server"
"github.com/spf13/cast"
"github.com/team-dandelion/analysis-plug/prometheus"
"net"
"net/http"
)
var (
Config *config
prom prometheus.Prometheus
)
type config struct {
AnalysisServer analysisServer `json:"analysis_server" yaml:"analysisServer"`
}
type analysisServer struct {
Type string `json:"type" yaml:"type"`
Port int32 `json:"port" yaml:"port"`
ServiceName string `json:"service_name" yaml:"serviceName"`
Prometheus bool `json:"prometheus" yaml:"prometheus"`
}
func Plug() *Plugin {
return &Plugin{}
}
type Plugin struct {
}
func (p *Plugin) Config() interface{} {
Config = &config{}
return Config
}
func (p *Plugin) InitPlugin() error {
switch Config.AnalysisServer.Type {
case "http":
prom = prometheus.NewHttpPrometheus(Config.AnalysisServer.ServiceName, []*prometheus.Metric{})
case "rpc":
prom = prometheus.NewRpcPrometheus(Config.AnalysisServer.ServiceName, []*prometheus.Metric{})
}
http.Handle("/metrics", promhttp.Handler())
go func() {
listener, _ := net.Listen("tcp", net.JoinHostPort("", cast.ToString(Config.AnalysisServer.Port)))
_ = http.Serve(listener, nil)
}()
return nil
}
func HttpPrometheus() routing.Handler {
if prom == nil {
return func(c *routing.Context) error {
return c.Next()
}
}
switch prom.(type) {
case *prometheus.HttpPrometheus:
return prom.(*prometheus.HttpPrometheus).HttpMiddleware()
}
return func(c *routing.Context) error {
return c.Next()
}
}
func RpcPrometheus() server.Plugin {
if prom == nil {
return nil
}
switch prom.(type) {
case *prometheus.RpcPrometheus:
return prom.(*prometheus.RpcPrometheus).RpcMiddleware()
}
return nil
}