-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
94 lines (83 loc) · 1.96 KB
/
middleware.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
package gininflux
import (
"fmt"
"log"
"strconv"
"time"
"github.com/gin-gonic/gin"
_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
)
type GinInflux struct {
bp client.BatchPoints
database string
conn client.Client
pointName string
writeThreshold int
}
func New(addr, database, pointName string, writeThreshold int) GinInflux {
conn, err := client.NewHTTPClient(client.HTTPConfig{
Addr: addr,
})
if err != nil {
panic(err)
}
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: database,
Precision: "s",
})
if err != nil {
panic(err)
}
return GinInflux{
bp: bp,
conn: conn,
database: database,
pointName: pointName,
writeThreshold: writeThreshold,
}
}
func (g *GinInflux) write(bp *client.Point) {
g.bp.AddPoint(bp)
if len(g.bp.Points()) >= g.writeThreshold {
err := g.conn.Write(g.bp)
if err != nil {
fmt.Errorf("Write to InfluxDB error, err=%v", err)
} else {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: g.database,
Precision: "ms",
})
if err != nil {
fmt.Errorf("Create batch points error, err=%v", err)
}
g.bp = bp
}
}
}
func (g *GinInflux) HandlerFunc() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
status := strconv.Itoa(c.Writer.Status())
c.Next()
elapsed := float64(time.Since(start)) / float64(time.Second)
go func() {
fields := map[string]interface{}{
"request_uri": c.Request.RequestURI,
"elapsed": elapsed,
}
tags := map[string]string{
"method": c.Request.Method,
"path": c.FullPath(),
"status": status,
"request_uri": c.Request.RequestURI,
}
pt, err := client.NewPoint(g.pointName, tags, fields, time.Now())
if err != nil {
log.Fatal(err)
}
g.write(pt)
}()
}
}