/
requestsMonitor.go
153 lines (132 loc) · 3.8 KB
/
requestsMonitor.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
package insights
import (
"fmt"
"os"
"sync"
"github.com/asaskevich/EventBus"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/spf13/viper"
"github.com/techquest-tech/gin-shared/pkg/core"
"github.com/techquest-tech/gin-shared/pkg/schedule"
"github.com/techquest-tech/monitor"
"go.uber.org/zap"
)
type AppInsightsSettings struct {
Key string
Role string
Version string
Details bool
}
type ResquestMonitor struct {
AppInsightsSettings
logger *zap.Logger
client appinsights.TelemetryClient
Locker *sync.Mutex
}
func InitRequestMonitor(logger *zap.Logger) *ResquestMonitor {
azureSetting := AppInsightsSettings{
Role: core.AppName,
Version: core.Version,
}
rm := &ResquestMonitor{
logger: logger,
Locker: &sync.Mutex{},
}
settings := viper.Sub("tracing.azure")
if settings != nil {
settings.Unmarshal(&azureSetting)
}
rm.AppInsightsSettings = azureSetting
if keyFromenv := os.Getenv("APPINSIGHTS_INSTRUMENTATIONKEY"); keyFromenv != "" {
rm.Key = keyFromenv
logger.Info("read application insights key from ENV")
}
if rm.Key == "" {
logger.Warn("no application insights key provided, tracing function disabled.")
return nil
}
// bus.SubscribeAsync(event.EventError, client.ReportError, false)
// bus.SubscribeAsync(event.EventTracing, client.ReportTracing, false)
// bus.SubscribeAsync(cronext.EventJobFinished, client.ReportScheduleJob, false)
// logger.Info("event subscribed for application insights", zap.Bool("details", client.Details))
rm.client = rm.getClient()
logger.Info("enabled applicationInsights client cache.")
return rm
}
func (appins *ResquestMonitor) ReportScheduleJob(req *schedule.JobHistory) {
status := 200
if !req.Succeed {
status = 500
}
details := &monitor.TracingDetails{
Uri: req.Job,
Method: "Cron",
Durtion: req.Duration,
Status: status,
}
appins.ReportTracing(details)
}
func (appins *ResquestMonitor) getClient() appinsights.TelemetryClient {
if appins.client == nil {
client := appinsights.NewTelemetryClient(appins.Key)
if appins.Role != "" {
client.Context().Tags.Cloud().SetRole(appins.Role)
}
if appins.Version != "" {
client.Context().Tags.Application().SetVer(appins.Version)
}
appins.client = client
return client
}
return appins.client
}
func (appins *ResquestMonitor) ReportError(err error) {
client := appins.getClient()
trace := appinsights.NewTraceTelemetry(err.Error(), appinsights.Error)
client.Track(trace)
appins.logger.Debug("tracing error done", zap.Error(err))
}
func (appins *ResquestMonitor) ReportTracing(tr *monitor.TracingDetails) {
appins.Locker.Lock()
defer appins.Locker.Unlock()
client := appins.getClient()
client.Context().Tags.Operation().SetName(fmt.Sprintf("%s %s", tr.Method, tr.Optionname))
t := appinsights.NewRequestTelemetry(
tr.Method, tr.Uri, tr.Durtion, fmt.Sprintf("%d", tr.Status),
)
t.Source = tr.ClientIP
t.Properties["app"] = core.AppName
t.Properties["version"] = core.Version
t.Properties["user-agent"] = tr.UserAgent
t.Properties["device"] = tr.Device
// if tr.Tenant != "" {
t.Properties["owner"] = tr.Tenant
// }
t.Properties["operator"] = tr.Operator
req := monitor.ToByte(tr.Body)
resp := monitor.ToByte(tr.Resp)
if len(req) > 0 {
if appins.Details {
t.Properties["req"] = string(req)
}
t.Measurements["body-size"] = float64(len(req))
}
if len(resp) > 0 {
if appins.Details {
t.Properties["resp"] = string(resp)
}
t.Measurements["resp-size"] = float64(len(resp))
}
client.Track(t)
appins.logger.Debug("submit tracing done.")
}
func EnabledMonitor() {
core.Provide(InitRequestMonitor)
// tracing.EnabledTracing()
core.ProvideStartup(func(logger *zap.Logger, bus EventBus.Bus, client *ResquestMonitor) core.Startup {
if client != nil {
monitor.SubscribeMonitor(logger, bus, client)
}
return nil
})
}