-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogh.go
executable file
·181 lines (153 loc) · 4.57 KB
/
gogh.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
package gogh
import (
"context"
runtime "github.com/aws/aws-lambda-go/lambda"
"github.com/genstackio/gogh/common"
"github.com/genstackio/gogh/services"
"github.com/go-chi/chi/v5"
"net/http"
)
type GhWrapper struct {
Handler runtime.Handler
Options common.Options
}
func prepare(options common.Options, payload []byte) (common.CaptureContext, common.Provider) {
c := EnrichContext(options)
opts := common.Options{
Logger: options.Logger,
Provider: options.Provider,
Environment: c.Environment,
}
localCtx := common.CaptureContext{
Data: map[string]string{
"payload": string(payload),
},
}
err := Init(opts)
p := *GetProvider()
if nil != err {
p.Error(err, localCtx)
}
return localCtx, p
}
func (w GhWrapper) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
localCtx, p := prepare(w.Options, payload)
h := p.Wrap(w.Handler.Invoke)
output, err := h(ctx, payload)
if nil != err {
p.Error(err, localCtx)
}
return output, err
}
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
defer (func() {
})()
})
}
//goland:noinspection GoUnusedExportedFunction
func GhHandlerWrapper(h runtime.Handler, options common.Options) runtime.Handler {
return GhWrapper{
Handler: h,
Options: options,
}
}
//goland:noinspection GoUnusedExportedFunction
func GhHttpRouterConfigurator(r *chi.Mux) {
r.Use(middleware)
}
func GetProvider() *common.Provider {
return services.GetProvider()
}
func GetLogger() *common.Logger {
return services.GetLogger()
}
//goland:noinspection GoUnusedExportedFunction
func Init(options common.Options) error {
err := services.InitLogger(options)
if nil != err {
return err
}
err = services.InitProvider(options)
if nil != err {
return err
}
return nil
}
//goland:noinspection GoUnusedExportedFunction,GoUnusedParameter
func EnrichContext(options common.Options) common.CaptureContext {
return common.CaptureContext{}
}
//goland:noinspection GoUnusedExportedFunction
func Log(level string, args ...interface{}) {
(*GetLogger()).Log(level, args...)
}
//goland:noinspection GoUnusedExportedFunction
func Error(args ...interface{}) {
(*GetLogger()).Error(args...)
}
//goland:noinspection GoUnusedExportedFunction
func Info(args ...interface{}) {
(*GetLogger()).Info(args...)
}
//goland:noinspection GoUnusedExportedFunction
func Warn(args ...interface{}) {
(*GetLogger()).Warn(args...)
}
//goland:noinspection GoUnusedExportedFunction
func Debug(args ...interface{}) {
(*GetLogger()).Debug(args...)
}
//goland:noinspection GoUnusedExportedFunction
func AddCaptureContext(ctx common.CaptureContext) {
(*GetProvider()).AddCaptureContext(ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureError(err error, ctx common.CaptureContext) {
(*GetProvider()).CaptureError(err, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureMessage(message string, ctx common.CaptureContext) {
(*GetProvider()).CaptureMessage(message, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureMessages(messages []string, ctx common.CaptureContext) {
(*GetProvider()).CaptureMessages(messages, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureProperty(typ string, data interface{}, ctx common.CaptureContext) {
(*GetProvider()).CaptureProperty(typ, data, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureData(bulkData map[string]interface{}, ctx common.CaptureContext) {
(*GetProvider()).CaptureData(bulkData, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureEvent(event interface{}, ctx common.CaptureContext) {
(*GetProvider()).CaptureEvent(event, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureTag(tag string, value interface{}, ctx common.CaptureContext) {
(*GetProvider()).CaptureTag(tag, value, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func CaptureTags(tags map[string]interface{}, ctx common.CaptureContext) {
(*GetProvider()).CaptureTags(tags, ctx)
}
//goland:noinspection GoUnusedExportedFunction
func RegisterProvider(name string, factory func() common.Provider) error {
return services.RegisterProvider(name, factory)
}
//goland:noinspection GoUnusedExportedFunction
func RegisterLogger(name string, factory func() common.Logger) error {
return services.RegisterLogger(name, factory)
}
//goland:noinspection GoUnusedExportedFunction
func RegisterEnricher(name string, factory func() common.Enricher) error {
return services.RegisterEnricher(name, factory)
}
//goland:noinspection GoUnusedExportedFunction
func Clean() {
(*GetProvider()).Clean()
}