-
Notifications
You must be signed in to change notification settings - Fork 8
/
routes.go
157 lines (139 loc) · 4.31 KB
/
routes.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
/**
* Copyright 2020 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/argus/store"
"github.com/xmidt-org/themis/xhealth"
"github.com/xmidt-org/themis/xhttp/xhttpserver"
"github.com/xmidt-org/themis/xmetrics"
"github.com/xmidt-org/themis/xmetrics/xmetricshttp"
"go.uber.org/fx"
)
type ServerChainIn struct {
fx.In
RequestCount *prometheus.CounterVec `name:"server_request_count"`
RequestDuration *prometheus.HistogramVec `name:"server_request_duration_ms"`
RequestsInFlight *prometheus.GaugeVec `name:"server_requests_in_flight"`
AuthChain *alice.Chain `name:"auth_chain"`
}
func provideServerChainFactory(in ServerChainIn) xhttpserver.ChainFactory {
return xhttpserver.ChainFactoryFunc(func(name string, o xhttpserver.Options) (alice.Chain, error) {
var (
curryLabel = prometheus.Labels{
ServerLabel: name,
}
serverLabellers = xmetricshttp.NewServerLabellers(
xmetricshttp.CodeLabeller{},
xmetricshttp.MethodLabeller{},
)
)
requestCount, err := in.RequestCount.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
requestDuration, err := in.RequestDuration.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
requestsInFlight, err := in.RequestsInFlight.CurryWith(curryLabel)
if err != nil {
return alice.Chain{}, err
}
if name == "servers.primary" {
return in.AuthChain.Append(
xmetricshttp.HandlerCounter{
Metric: xmetrics.LabelledCounterVec{CounterVec: requestCount},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerDuration{
Metric: xmetrics.LabelledObserverVec{ObserverVec: requestDuration},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerInFlight{
Metric: xmetrics.LabelledGaugeVec{GaugeVec: requestsInFlight},
}.Then,
), nil
}
return alice.New(
xmetricshttp.HandlerCounter{
Metric: xmetrics.LabelledCounterVec{CounterVec: requestCount},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerDuration{
Metric: xmetrics.LabelledObserverVec{ObserverVec: requestDuration},
Labeller: serverLabellers,
}.Then,
xmetricshttp.HandlerInFlight{
Metric: xmetrics.LabelledGaugeVec{GaugeVec: requestsInFlight},
}.Then,
), nil
})
}
type PrimaryRouter struct {
fx.In
Router *mux.Router `name:"servers.primary"`
Handler store.Handler `name:"setHandler"`
}
type SetRoutesIn struct {
fx.In
Handler store.Handler `name:"setHandler"`
}
type GetRoutesIn struct {
fx.In
Handler store.Handler `name:"getHandler"`
}
type GetAllRoutesIn struct {
fx.In
Handler store.Handler `name:"getAllHandler"`
}
func BuildPrimaryRoutes(router PrimaryRouter, sin SetRoutesIn, gin GetRoutesIn, gain GetAllRoutesIn) {
if router.Handler != nil {
if sin.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), sin.Handler).Methods("PUT")
}
if gin.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}/{key}", apiBase), gin.Handler).Methods("GET", "DELETE")
}
if gain.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), gain.Handler).Methods("GET")
}
}
}
type MetricsRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.metrics"`
Handler xmetricshttp.Handler
}
func BuildMetricsRoutes(in MetricsRoutesIn) {
if in.Router != nil && in.Handler != nil {
in.Router.Handle("/metrics", in.Handler).Methods("GET")
}
}
type HealthRoutesIn struct {
fx.In
Router *mux.Router `name:"servers.health"`
Handler xhealth.Handler
}
func BuildHealthRoutes(in HealthRoutesIn) {
if in.Router != nil && in.Handler != nil {
in.Router.Handle("/health", in.Handler).Methods("GET")
}
}