FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
go-githubapp/githubapp/middleware.go at develop · git-connected/go-githubapp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
git-connected
/
go-githubapp
Public
forked from
smooth80/go-githubapp
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Issues
0
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
go-githubapp
/
githubapp
/
middleware.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
151 lines (129 loc) · 4.53 KB
Breadcrumbs
go-githubapp
/
githubapp
/
middleware.go
Copy path
File metadata and controls
151 lines (129 loc) · 4.53 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright 2018 Palantir Technologies, Inc.
//
// 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
githubapp
import
(
"fmt"
"net/http"
"strconv"
"time"
"github.com/gregjones/httpcache"
"github.com/rcrowley/go-metrics"
"github.com/rs/zerolog"
)
const
(
MetricsKeyRequests
=
"github.requests"
MetricsKeyRequests2xx
=
"github.requests.2xx"
MetricsKeyRequests3xx
=
"github.requests.3xx"
MetricsKeyRequests4xx
=
"github.requests.4xx"
MetricsKeyRequests5xx
=
"github.requests.5xx"
MetricsKeyRequestsCached
=
"github.requests.cached"
MetricsKeyRateLimit
=
"github.rate.limit"
MetricsKeyRateLimitRemaining
=
"github.rate.remaining"
)
// ClientMetrics creates client middleware that records metrics about all
// requests. It also defines the metrics in the provided registry.
func
ClientMetrics
(
registry
metrics.
Registry
)
ClientMiddleware
{
for
_
,
key
:=
range
[]
string
{
MetricsKeyRequests
,
MetricsKeyRequests2xx
,
MetricsKeyRequests3xx
,
MetricsKeyRequests4xx
,
MetricsKeyRequests5xx
,
MetricsKeyRequestsCached
,
} {
// Use GetOrRegister for thread-safety when creating multiple
// RoundTrippers that share the same registry
metrics
.
GetOrRegisterCounter
(
key
,
registry
)
}
return
func
(
next
http.
RoundTripper
) http.
RoundTripper
{
return
roundTripperFunc
(
func
(
r
*
http.
Request
) (
*
http.
Response
,
error
) {
installationID
,
ok
:=
r
.
Context
().
Value
(
installationKey
).(
int64
)
if
!
ok
{
installationID
=
0
}
res
,
err
:=
next
.
RoundTrip
(
r
)
if
res
!=
nil
{
registry
.
Get
(
MetricsKeyRequests
).(metrics.
Counter
).
Inc
(
1
)
if
key
:=
bucketStatus
(
res
.
StatusCode
);
key
!=
""
{
registry
.
Get
(
key
).(metrics.
Counter
).
Inc
(
1
)
}
if
res
.
Header
.
Get
(
httpcache
.
XFromCache
)
!=
""
{
registry
.
Get
(
MetricsKeyRequestsCached
).(metrics.
Counter
).
Inc
(
1
)
}
limitMetric
:=
fmt
.
Sprintf
(
"%s[installation:%d]"
,
MetricsKeyRateLimit
,
installationID
)
remainingMetric
:=
fmt
.
Sprintf
(
"%s[installation:%d]"
,
MetricsKeyRateLimitRemaining
,
installationID
)
// Headers from https://developer.github.com/v3/#rate-limiting
updateRegistryForHeader
(
res
.
Header
,
"X-RateLimit-Limit"
,
metrics
.
GetOrRegisterGauge
(
limitMetric
,
registry
))
updateRegistryForHeader
(
res
.
Header
,
"X-RateLimit-Remaining"
,
metrics
.
GetOrRegisterGauge
(
remainingMetric
,
registry
))
}
return
res
,
err
})
}
}
func
updateRegistryForHeader
(
headers
http.
Header
,
header
string
,
metric
metrics.
Gauge
) {
headerString
:=
headers
.
Get
(
header
)
if
headerString
!=
""
{
headerVal
,
err
:=
strconv
.
ParseInt
(
headerString
,
10
,
64
)
if
err
==
nil
{
metric
.
Update
(
headerVal
)
}
}
}
func
bucketStatus
(
status
int
)
string
{
switch
{
case
status
>=
200
&&
status
<
300
:
return
MetricsKeyRequests2xx
case
status
>=
300
&&
status
<
400
:
return
MetricsKeyRequests3xx
case
status
>=
400
&&
status
<
500
:
return
MetricsKeyRequests4xx
case
status
>=
500
&&
status
<
600
:
return
MetricsKeyRequests5xx
}
return
""
}
// ClientLogging creates client middleware that logs request and response
// information at the given level. If the request fails without creating a
// response, it is logged with a status code of -1. The middleware uses a
// logger from the request context.
func
ClientLogging
(
lvl
zerolog.
Level
)
ClientMiddleware
{
return
func
(
next
http.
RoundTripper
) http.
RoundTripper
{
return
roundTripperFunc
(
func
(
r
*
http.
Request
) (
*
http.
Response
,
error
) {
start
:=
time
.
Now
()
res
,
err
:=
next
.
RoundTrip
(
r
)
elapsed
:=
time
.
Now
().
Sub
(
start
)
evt
:=
zerolog
.
Ctx
(
r
.
Context
()).
WithLevel
(
lvl
).
Str
(
"method"
,
r
.
Method
).
Str
(
"path"
,
r
.
URL
.
String
()).
Dur
(
"elapsed"
,
elapsed
)
if
res
!=
nil
{
cached
:=
res
.
Header
.
Get
(
httpcache
.
XFromCache
)
!=
""
evt
.
Int
(
"status"
,
res
.
StatusCode
).
Int64
(
"size"
,
res
.
ContentLength
).
Bool
(
"cached"
,
cached
)
}
else
{
evt
.
Int
(
"status"
,
-
1
).
Int64
(
"size"
,
-
1
).
Bool
(
"cached"
,
false
)
}
evt
.
Msg
(
"github_request"
)
return
res
,
err
})
}
}
type
roundTripperFunc
func
(
*
http.
Request
) (
*
http.
Response
,
error
)
func
(
fn
roundTripperFunc
)
RoundTrip
(
r
*
http.
Request
) (
*
http.
Response
,
error
) {
return
fn
(
r
)
}
Back
|
FazBrowse Home
|
New Git URL