forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics_go_plugin.go
66 lines (51 loc) · 1.5 KB
/
analytics_go_plugin.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
package gateway
import (
"errors"
"fmt"
"time"
"github.com/sirupsen/logrus"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk/goplugin"
)
type GoAnalyticsPlugin struct {
Path string // path to .so file
FuncName string // function symbol to look up
handler func(record *analytics.AnalyticsRecord)
logger *logrus.Entry
}
func (m *GoAnalyticsPlugin) loadAnalyticsPlugin() bool {
m.logger = log.WithFields(logrus.Fields{
"mwPath": m.Path,
"mwSymbolName": m.FuncName,
})
if m.handler != nil {
m.logger.Info("Go Analytics Plugin is already initialized")
return true
}
// try to load plugin
var err error
if m.handler, err = goplugin.GetAnalyticsHandler(m.Path, m.FuncName); err != nil {
m.logger.WithError(err).Error("Could not load Go-plugin for analytics")
return false
}
return true
}
func (m *GoAnalyticsPlugin) processRecord(record *analytics.AnalyticsRecord) (err error) {
if m == nil {
return errors.New("GoAnalyticsPlugin has nil value")
}
// make sure tyk recover in case Go-plugin function panics
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", errors.New(fmt.Sprint(err)))
m.logger.WithError(err).Error("Recovered from panic while running Go-plugin middleware func")
}
}()
// call Go-plugin function
t1 := time.Now()
m.handler(record)
// calculate latency
ms := DurationToMillisecond(time.Since(t1))
m.logger.WithField("ms", ms).Debug("Go-plugin analytics record processing took")
return nil
}