/
resource.go
115 lines (92 loc) · 4.63 KB
/
resource.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
// Copyright (c) 2018 Palantir Technologies. All rights reserved.
//
// 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 wresource
import (
"net/http"
"github.com/palantir/pkg/metrics"
werror "github.com/palantir/witchcraft-go-error"
"github.com/palantir/witchcraft-go-server/wrouter"
)
const (
ResourceTagName = "service-name"
MethodTagName = "method"
EndpointTagName = "endpoint"
)
// Resource provides a way to register a collection of endpoints. All of the routes registered using Resource will be
// tagged with the "service-name" and "endpoint" metrics.
type Resource interface {
// Register performs the same operation as the Router.Register, but registers the route using the provided
// resourceName as the resource name that will be used as the tag in the recorded metric.
Register(endpointName, method, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Get is a shorthand for Register(endpointName, http.MethodGet, handler, params...)
Get(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Head is a shorthand for Register(endpointName, http.MethodHead, handler, params...)
Head(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Post is a shorthand for Register(endpointName, http.MethodPost, handler, params...)
Post(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Put is a shorthand for Register(endpointName, http.MethodPut, handler, params...)
Put(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Patch is a shorthand for Register(endpointName, http.MethodPatch, handler, params...)
Patch(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
// Delete is a shorthand for Register(endpointName, http.MethodDelete, handler, params...)
Delete(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error
}
func New(resourceName string, router wrouter.Router) Resource {
return &resourceImpl{
resourceName: resourceName,
router: router,
}
}
type resourceImpl struct {
// the name of the resource used for metric logging.
resourceName string
router wrouter.Router
}
func (r *resourceImpl) Register(endpointName, method, path string, handler http.Handler, params ...wrouter.RouteParam) error {
var tags metrics.Tags
resourceTag, err := metrics.NewTag(ResourceTagName, r.resourceName)
if err != nil {
return werror.Wrap(err, "failed to create metric resourceTag")
}
tags = append(tags, resourceTag)
methodTag, err := metrics.NewTag(MethodTagName, method)
if err != nil {
return werror.Wrap(err, "failed to create metric methodTag")
}
tags = append(tags, methodTag)
endpointTag, err := metrics.NewTag(EndpointTagName, endpointName)
if err != nil {
return werror.Wrap(err, "failed to create metric endpointTag")
}
tags = append(tags, endpointTag)
return r.router.Register(method, path, handler, append(params, wrouter.MetricTags(tags))...)
}
func (r *resourceImpl) Get(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodGet, path, handler, params...)
}
func (r *resourceImpl) Head(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodHead, path, handler, params...)
}
func (r *resourceImpl) Post(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodPost, path, handler, params...)
}
func (r *resourceImpl) Put(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodPut, path, handler, params...)
}
func (r *resourceImpl) Patch(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodPatch, path, handler, params...)
}
func (r *resourceImpl) Delete(endpointName, path string, handler http.Handler, params ...wrouter.RouteParam) error {
return r.Register(endpointName, http.MethodDelete, path, handler, params...)
}