-
Notifications
You must be signed in to change notification settings - Fork 1
/
component.go
207 lines (182 loc) · 4.73 KB
/
component.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package prom2click
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/gotomicro/ego/core/constant"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/server"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage/remote"
)
// PackageName 包名
const PackageName = "server.prom2click"
// Component ...
type Component struct {
*gin.Engine
mu sync.Mutex
name string
config *config
logger *elog.Component
Server *http.Server
listener net.Listener
writer *promWriter
reader *promReader
}
func newComponent(name string, config *config, logger *elog.Component) *Component {
var err error
gin.SetMode(config.Mode)
comp := &Component{
name: name,
config: config,
logger: logger,
Engine: gin.New(),
listener: nil,
}
comp.writer, err = NewWriter(config)
if err != nil {
elog.Panic("p2c writer fail", elog.FieldErr(err))
return nil
}
comp.reader, err = NewReader(config)
if err != nil {
elog.Panic("p2c reader fail", elog.FieldErr(err))
return nil
}
// 设置信任的header头
comp.Engine.TrustedPlatform = config.TrustedPlatform
comp.route()
return comp
}
func (c *Component) route() {
c.Engine.Any(c.config.ClickhouseHTTPWritePath, func(ctx *gin.Context) {
prompbReq, err := remote.DecodeWriteRequest(ctx.Request.Body)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
c.writer.process(prompbReq)
})
c.Engine.POST(c.config.ClickhouseHTTPReadPath, func(ctx *gin.Context) {
prompbReq, err := remote.DecodeReadRequest(ctx.Request)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
var resp *prompb.ReadResponse
resp, err = c.reader.Read(prompbReq)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
// ctx.Header("Content-Type", "application/x-protobuf")
// ctx.Header("Content-Encoding", "snappy")
err = remote.EncodeReadResponse(resp, ctx.Writer)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
})
}
// Name 配置名称
func (c *Component) Name() string {
return c.name
}
// PackageName 包名
func (c *Component) PackageName() string {
return PackageName
}
// Init 初始化
func (c *Component) Init() error {
var err error
c.listener, err = net.Listen(c.config.Network, c.config.Address())
if err != nil {
c.logger.Panic("new prom2click server err", elog.FieldErrKind("listen err"), elog.FieldErr(err))
}
c.config.Port = c.listener.Addr().(*net.TCPAddr).Port
return nil
}
// Start implements server.Component interface.
func (c *Component) Start() error {
for _, route := range c.Engine.Routes() {
c.logger.Info("add route", elog.FieldMethod(route.Method), elog.String("path", route.Path))
}
// 因为start和stop在多个goroutine里,需要对Server上写锁
c.mu.Lock()
c.Server = &http.Server{
Addr: c.config.Address(),
Handler: c,
ReadHeaderTimeout: c.config.ServerReadHeaderTimeout,
ReadTimeout: c.config.ServerReadTimeout,
WriteTimeout: c.config.ServerWriteTimeout,
}
c.mu.Unlock()
c.writer.Start()
var err error
err = c.Server.Serve(c.listener)
if err == http.ErrServerClosed {
return nil
}
return err
}
// Stop implements server.Component interface
// it will terminate gin server immediately
func (c *Component) Stop() error {
close(c.writer.requests)
c.writer.Wait()
wchan := make(chan struct{})
go func() {
c.writer.Wait()
close(wchan)
}()
select {
case <-wchan:
c.logger.Info("Prom2Click shutdown cleanly..")
// All done!
case <-time.After(10 * time.Second):
c.logger.Info("Prom2Click shutdown timed out, samples will be lost..")
}
c.mu.Lock()
err := c.Server.Close()
c.mu.Unlock()
return err
}
// GracefulStop implements server.Component interface
// it will stop gin server gracefully
func (c *Component) GracefulStop(ctx context.Context) error {
close(c.writer.requests)
c.writer.Wait()
wchan := make(chan struct{})
go func() {
c.writer.Wait()
close(wchan)
}()
select {
case <-wchan:
c.logger.Info("Prom2Click shutdown cleanly..")
// All done!
case <-time.After(10 * time.Second):
c.logger.Info("Prom2Click shutdown timed out, samples will be lost..")
}
c.mu.Lock()
err := c.Server.Shutdown(ctx)
c.mu.Unlock()
return err
}
// Info returns server info, used by governor and consumer balancer
func (c *Component) Info() *server.ServiceInfo {
info := server.ApplyOptions(
server.WithScheme("http"),
server.WithAddress(c.listener.Addr().String()),
server.WithKind(constant.ServiceProvider),
)
return &info
}
func commentUniqKey(method, path string) string {
return fmt.Sprintf("%s@%s", strings.ToLower(method), path)
}