-
Notifications
You must be signed in to change notification settings - Fork 4
/
opencensus.go
118 lines (100 loc) · 3.12 KB
/
opencensus.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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware
import (
"net/http"
"github.com/labstack/echo/v4"
mw "github.com/labstack/echo/v4/middleware"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
var (
// OpenCensusRequestCount counts the HTTP requests started.
OpenCensusRequestCount = &view.View{
Name: "request_count",
Description: "Count of HTTP request started",
Measure: ochttp.ServerRequestCount,
Aggregation: view.Count(),
}
// OpenCensusRequestCountByMethod counts the HTTP requests by method.
OpenCensusRequestCountByMethod = &view.View{
Name: "request_count_by_method",
Description: "Server request count by HTTP method",
TagKeys: []tag.Key{ochttp.Method},
Measure: ochttp.ServerRequestCount,
Aggregation: view.Count(),
}
// OpenCensusRequestCountByPath counts the HTTP requests by path.
OpenCensusRequestCountByPath = &view.View{
Name: "request_count_by_path",
Description: "Server request count by HTTP path",
TagKeys: []tag.Key{ochttp.Path},
Measure: ochttp.ServerRequestCount,
Aggregation: view.Count(),
}
// OpenCensusResponseCountByStatusCode counts the HTTP requests by status code.
OpenCensusResponseCountByStatusCode = &view.View{
Name: "response_count_by_status_code",
Description: "Server response count by status code",
TagKeys: []tag.Key{ochttp.StatusCode},
Measure: ochttp.ServerLatency,
Aggregation: view.Count(),
}
)
// OpenCensusConfig defines the config for OpenCensus middleware.
type OpenCensusConfig struct {
// View it is a OpenCensus Views list.
Views []*view.View
// Skipper defines a function to skip middleware.
Skipper mw.Skipper
}
// DefaultOpenCensusConfig is the default OpenCensus middleware config.
var DefaultOpenCensusConfig = OpenCensusConfig{
Views: []*view.View{
OpenCensusRequestCount,
OpenCensusRequestCountByMethod,
OpenCensusRequestCountByPath,
OpenCensusResponseCountByStatusCode,
},
Skipper: mw.DefaultSkipper,
}
// OpenCensus returns a middleware that collect HTTP requests and response
// metrics.
func OpenCensus() echo.MiddlewareFunc {
return OpenCensusWithConfig(DefaultOpenCensusConfig)
}
// OpenCensusWithConfig returns a OpenCensus middleware with config.
// See: `OpenCensus()`.
func OpenCensusWithConfig(cfg OpenCensusConfig) echo.MiddlewareFunc {
// Defaults
if cfg.Skipper == nil {
cfg.Skipper = DefaultOpenCensusConfig.Skipper
}
if len(cfg.Views) == 0 {
cfg.Views = DefaultOpenCensusConfig.Views
}
if err := view.Register(cfg.Views...); err != nil {
panic("echo: opencensus middleware register views failed")
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) (err error) {
if cfg.Skipper(ec) {
return next(ec)
}
handler := &ochttp.Handler{
Handler: http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ec.SetRequest(r)
ec.SetResponse(echo.NewResponse(w, ec.Echo()))
err = next(ec)
},
),
}
handler.ServeHTTP(ec.Response(), ec.Request())
return
}
}
}