Skip to content

Commit 15f9f75

Browse files
committed
added basic middleware for primary server
1 parent 28b0b22 commit 15f9f75

File tree

8 files changed

+759
-665
lines changed

8 files changed

+759
-665
lines changed

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/alecthomas/kong v0.8.1
1010
github.com/go-chi/chi/v5 v5.0.10
1111
github.com/go-kit/kit v0.13.0
12-
github.com/gorilla/mux v1.8.0
12+
github.com/gorilla/mux v1.8.1
1313
github.com/goschtalt/goschtalt v0.22.1
1414
github.com/goschtalt/yaml-decoder v0.0.1
1515
github.com/goschtalt/yaml-encoder v0.0.3
@@ -27,6 +27,7 @@ require (
2727
github.com/xmidt-org/touchstone v0.1.3
2828
github.com/xmidt-org/webpa-common/v2 v2.2.2
2929
github.com/xmidt-org/wrp-go/v3 v3.2.1
30+
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1
3031
go.uber.org/fx v1.20.1
3132
go.uber.org/zap v1.26.0
3233
gopkg.in/dealancer/validate.v2 v2.1.0
@@ -41,6 +42,7 @@ require (
4142
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4243
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
4344
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
45+
github.com/felixge/httpsnoop v1.0.4 // indirect
4446
github.com/fsnotify/fsnotify v1.6.0 // indirect
4547
github.com/go-kit/log v0.2.1 // indirect
4648
github.com/go-logfmt/logfmt v0.6.0 // indirect
@@ -85,16 +87,16 @@ require (
8587
github.com/subosito/gotenv v1.6.0 // indirect
8688
github.com/ugorji/go/codec v1.2.11 // indirect
8789
github.com/xmidt-org/chronon v0.1.1 // indirect
88-
go.opentelemetry.io/otel v1.19.0 // indirect
90+
go.opentelemetry.io/otel v1.21.0 // indirect
8991
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
9092
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
9193
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
9294
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 // indirect
9395
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 // indirect
9496
go.opentelemetry.io/otel/exporters/zipkin v1.19.0 // indirect
95-
go.opentelemetry.io/otel/metric v1.19.0 // indirect
97+
go.opentelemetry.io/otel/metric v1.21.0 // indirect
9698
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
97-
go.opentelemetry.io/otel/trace v1.19.0 // indirect
99+
go.opentelemetry.io/otel/trace v1.21.0 // indirect
98100
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
99101
go.uber.org/dig v1.17.1 // indirect
100102
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,8 @@ github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8Wlg
815815
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
816816
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
817817
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
818+
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
819+
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
818820
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
819821
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
820822
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
@@ -1027,8 +1029,9 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
10271029
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
10281030
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
10291031
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1030-
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
10311032
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1033+
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
1034+
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
10321035
github.com/gorilla/schema v1.0.3-0.20180614150749-e0e4b92809ac/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
10331036
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
10341037
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
@@ -1763,6 +1766,8 @@ go.opentelemetry.io/contrib v0.19.0/go.mod h1:G/EtFaa6qaN7+LxqfIAT3GiZa7Wv5DTBUz
17631766
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.19.0/go.mod h1:ze4w2zyQP+FvZjaahHaUVD7h4razLhDOsZD3qFKXc3c=
17641767
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.38.0/go.mod h1:iUSPEXZM7sckWSTCtzog1lU42Qaiu9U2WY6vdqwFHDI=
17651768
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.40.0/go.mod h1:RK3vgddjxVcF1q7IBVppzG6k2cW/NBnZHQ3X4g+EYBQ=
1769+
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1 h1:Ifzy1lucGMQJh6wPRxusde8bWaDhYjSNOqDyn6Hb4TM=
1770+
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1/go.mod h1:YfFNem80G9UZ/mL5zd5GGXZSy95eXK+RhzIWBkLjLSc=
17661771
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.19.0/go.mod h1:7RDsakVbjb124lYDEjKuHTuzdqf04hLMEvPv/ufmqMs=
17671772
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.38.0/go.mod h1:w6xNm+kC506KNs5cknSHal6dtdRnc4uema0uN9GSQc0=
17681773
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0/go.mod h1:pcQ3MM3SWvrA71U4GDqv9UFDJ3HQsW7y5ZO3tDTlUdI=
@@ -1772,8 +1777,8 @@ go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9
17721777
go.opentelemetry.io/otel v1.12.0/go.mod h1:geaoz0L0r1BEOR81k7/n9W4TCXYCJ7bPO7K374jQHG0=
17731778
go.opentelemetry.io/otel v1.13.0/go.mod h1:FH3RtdZCzRkJYFTCsAKDy9l/XYjMdNv6QrkFFB8DvVg=
17741779
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
1775-
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
1776-
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=
1780+
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
1781+
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
17771782
go.opentelemetry.io/otel/exporters/jaeger v1.11.2/go.mod h1:nwcF/DK4Hk0auZ/a5vw20uMsaJSXbzeeimhN5f9d0Lc=
17781783
go.opentelemetry.io/otel/exporters/jaeger v1.14.0/go.mod h1:4Ay9kk5vELRrbg5z4cpP9EtmQRFap2Wb0woPG4lujZA=
17791784
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 h1:D7UpUy2Xc2wsi1Ras6V40q806WM07rqoCWzXu7Sqy+4=
@@ -1807,8 +1812,8 @@ go.opentelemetry.io/otel/exporters/zipkin v1.19.0/go.mod h1:JQgTGJP11yi3o4GHzIWY
18071812
go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2VmNNqIsx/uIwBSc=
18081813
go.opentelemetry.io/otel/metric v0.35.0/go.mod h1:qAcbhaTRFU6uG8QM7dDo7XvFsWcugziq/5YI065TokQ=
18091814
go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s=
1810-
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE=
1811-
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8=
1815+
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
1816+
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
18121817
go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA=
18131818
go.opentelemetry.io/otel/sdk v0.19.0/go.mod h1:ouO7auJYMivDjywCHA6bqTI7jJMVQV1HdKR5CmH8DGo=
18141819
go.opentelemetry.io/otel/sdk v1.11.2/go.mod h1:wZ1WxImwpq+lVRo4vsmSOxdd+xwoUJ6rqyLc3SyX9aU=
@@ -1824,8 +1829,8 @@ go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06O
18241829
go.opentelemetry.io/otel/trace v1.12.0/go.mod h1:pHlgBynn6s25qJ2szD+Bv+iwKJttjHSI3lUAyf0GNuQ=
18251830
go.opentelemetry.io/otel/trace v1.13.0/go.mod h1:muCvmmO9KKpvuXSf3KKAXXB2ygNYHQ+ZfI5X08d3tds=
18261831
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
1827-
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
1828-
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
1832+
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
1833+
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
18291834
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
18301835
go.opentelemetry.io/proto/otlp v0.15.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
18311836
go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=

http.go

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,55 @@ import (
88
"sync/atomic"
99
"time"
1010

11+
"go.uber.org/fx"
1112
"go.uber.org/zap"
1213

13-
"github.com/go-kit/kit/metrics"
14+
"github.com/prometheus/client_golang/prometheus"
1415
uuid "github.com/satori/go.uuid"
1516

1617
"github.com/xmidt-org/sallust"
1718
"github.com/xmidt-org/webpa-common/v2/adapter"
1819
"github.com/xmidt-org/wrp-go/v3"
1920
)
2021

22+
type ServerHandlerIn struct {
23+
fx.In
24+
Logger *zap.Logger
25+
Telemetry *HandlerTelemetry
26+
}
27+
28+
type ServerHandlerOut struct {
29+
fx.Out
30+
Handler *ServerHandler
31+
}
32+
2133
// Below is the struct that will implement our ServeHTTP method
2234
type ServerHandler struct {
23-
*zap.Logger
24-
caduceusHandler RequestHandler
25-
errorRequests metrics.Counter
26-
emptyRequests metrics.Counter
27-
invalidCount metrics.Counter
28-
incomingQueueDepthMetric metrics.Gauge
29-
modifiedWRPCount metrics.Counter
30-
incomingQueueDepth int64
31-
maxOutstanding int64
32-
incomingQueueLatency metrics.Histogram
33-
now func() time.Time
35+
log *zap.Logger
36+
// caduceusHandler RequestHandler
37+
telemetry *HandlerTelemetry
38+
incomingQueueDepth int64
39+
maxOutstanding int64
40+
41+
now func() time.Time
42+
}
43+
44+
type HandlerTelemetryIn struct {
45+
fx.In
46+
ErrorRequests prometheus.Counter `name:"error_request_body_counter"`
47+
EmptyRequests prometheus.Counter `name:"empty_request_boyd_counter"`
48+
InvalidCount prometheus.Counter `name:"drops_due_to_invalid_payload"`
49+
IncomingQueueDepthMetric prometheus.Gauge `name:"incoming_queue_depth"`
50+
ModifiedWRPCount prometheus.CounterVec `name:"modified_wrp_count"`
51+
IncomingQueueLatency prometheus.HistogramVec `name:"incoming_queue_latency_histogram_seconds"`
52+
}
53+
type HandlerTelemetry struct {
54+
errorRequests prometheus.Counter
55+
emptyRequests prometheus.Counter
56+
invalidCount prometheus.Counter
57+
incomingQueueDepthMetric prometheus.Gauge
58+
modifiedWRPCount prometheus.CounterVec
59+
incomingQueueLatency prometheus.HistogramVec
3460
}
3561

3662
func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
@@ -42,7 +68,7 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
4268

4369
logger := sallust.Get(request.Context())
4470
if logger == adapter.DefaultLogger().Logger {
45-
logger = sh.Logger
71+
logger = sh.log
4672
}
4773

4874
logger.Info("Receiving incoming request...")
@@ -66,19 +92,19 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
6692
return
6793
}
6894

69-
sh.incomingQueueDepthMetric.Add(1.0)
70-
defer sh.incomingQueueDepthMetric.Add(-1.0)
95+
sh.telemetry.incomingQueueDepthMetric.Add(1.0)
96+
defer sh.telemetry.incomingQueueDepthMetric.Add(-1.0)
7197

7298
payload, err := io.ReadAll(request.Body)
7399
if err != nil {
74-
sh.errorRequests.Add(1.0)
100+
sh.telemetry.errorRequests.Add(1.0)
75101
logger.Error("Unable to retrieve the request body.", zap.Error(err))
76102
response.WriteHeader(http.StatusBadRequest)
77103
return
78104
}
79105

80106
if len(payload) == 0 {
81-
sh.emptyRequests.Add(1.0)
107+
sh.telemetry.emptyRequests.Add(1.0)
82108
logger.Error("Empty payload.")
83109
response.WriteHeader(http.StatusBadRequest)
84110
response.Write([]byte("Empty payload.\n"))
@@ -91,7 +117,7 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
91117
err = decoder.Decode(msg)
92118
if err != nil || msg.MessageType() != 4 {
93119
// return a 400
94-
sh.invalidCount.Add(1.0)
120+
sh.telemetry.invalidCount.Add(1.0)
95121
response.WriteHeader(http.StatusBadRequest)
96122
if err != nil {
97123
response.Write([]byte("Invalid payload format.\n"))
@@ -106,15 +132,15 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
106132
err = wrp.UTF8(msg)
107133
if err != nil {
108134
// return a 400
109-
sh.invalidCount.Add(1.0)
135+
sh.telemetry.invalidCount.Add(1.0)
110136
response.WriteHeader(http.StatusBadRequest)
111137
response.Write([]byte("Strings must be UTF-8.\n"))
112138
logger.Debug("Strings must be UTF-8.")
113139
return
114140
}
115141
eventType = msg.FindEventStringSubMatch()
116142

117-
sh.caduceusHandler.HandleRequest(0, sh.fixWrp(msg))
143+
// sh.caduceusHandler.HandleRequest(0, sh.fixWrp(msg))
118144

119145
// return a 202
120146
response.WriteHeader(http.StatusAccepted)
@@ -125,7 +151,7 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
125151

126152
func (sh *ServerHandler) recordQueueLatencyToHistogram(startTime time.Time, eventType string) {
127153
endTime := sh.now()
128-
sh.incomingQueueLatency.With("event", eventType).Observe(endTime.Sub(startTime).Seconds())
154+
sh.telemetry.incomingQueueLatency.With(prometheus.Labels{"event": eventType}).Observe(float64(endTime.Sub(startTime).Seconds()))
129155
}
130156

131157
func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message {
@@ -134,13 +160,13 @@ func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message {
134160

135161
// Default to "application/json" if there is no content type, otherwise
136162
// use the one the source specified.
137-
if "" == msg.ContentType {
163+
if msg.ContentType == "" {
138164
msg.ContentType = wrp.MimeTypeJson
139165
reason = emptyContentTypeReason
140166
}
141167

142168
// Ensure there is a transaction id even if we make one up
143-
if "" == msg.TransactionUUID {
169+
if msg.TransactionUUID == "" {
144170
msg.TransactionUUID = uuid.NewV4().String()
145171
if reason == "" {
146172
reason = emptyUUIDReason
@@ -150,8 +176,40 @@ func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message {
150176
}
151177

152178
if reason != "" {
153-
sh.modifiedWRPCount.With("reason", reason).Add(1.0)
179+
sh.telemetry.modifiedWRPCount.With(prometheus.Labels{"reason": reason}).Add(1.0)
154180
}
155181

156182
return msg
157183
}
184+
185+
var HandlerModule = fx.Module("server",
186+
fx.Provide(
187+
func(in HandlerTelemetryIn) *HandlerTelemetry {
188+
return &HandlerTelemetry{
189+
errorRequests: in.ErrorRequests,
190+
emptyRequests: in.EmptyRequests,
191+
invalidCount: in.InvalidCount,
192+
incomingQueueDepthMetric: in.IncomingQueueDepthMetric,
193+
modifiedWRPCount: in.ModifiedWRPCount,
194+
incomingQueueLatency: in.IncomingQueueLatency,
195+
}
196+
}),
197+
fx.Provide(
198+
func(in ServerHandlerIn) (ServerHandlerOut, error) {
199+
//Hard coding maxOutstanding and incomingQueueDepth for now
200+
handler, err := New(in.Logger, in.Telemetry, 0.0, 0.0)
201+
return ServerHandlerOut{
202+
Handler: handler,
203+
}, err
204+
},
205+
),
206+
)
207+
208+
func New(log *zap.Logger, t *HandlerTelemetry, maxOutstanding, incomingQueueDepth int64) (*ServerHandler, error) {
209+
return &ServerHandler{
210+
log: log,
211+
telemetry: t,
212+
maxOutstanding: maxOutstanding,
213+
incomingQueueDepth: incomingQueueDepth,
214+
}, nil
215+
}

0 commit comments

Comments
 (0)