Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit 6ceb44d

Browse files
author
Zack Lin
committed
Use relay/http/* as metric name prefix
1 parent b045f2d commit 6ceb44d

File tree

2 files changed

+101
-43
lines changed

2 files changed

+101
-43
lines changed

Diff for: metric/metric.go

+6-43
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@ import (
99
"github.com/prometheus/client_golang/prometheus"
1010
"go.opencensus.io/plugin/ochttp"
1111
"go.opencensus.io/stats/view"
12-
"go.opencensus.io/tag"
1312
)
1413

1514
const (
1615
defaultAddr = ":8088"
1716
defaultShutdownTimeoutMs = 5000
1817
)
1918

19+
// Server represents a metric server serves /metrics endpoint
2020
type Server struct {
2121
h *http.Server
2222
pe *ocprom.Exporter
2323
}
2424

25-
func init() {
26-
registerDefaultServerViews()
27-
registerDefaultClientViews()
28-
}
29-
25+
// Run launchs the metric server
3026
func (s *Server) Run() error {
3127
mux := http.NewServeMux()
3228
mux.Handle("/metrics", s.pe)
@@ -38,12 +34,14 @@ func (s *Server) Run() error {
3834
return nil
3935
}
4036

37+
// Stop stops the metric server gracefully
4138
func (s *Server) Stop() error {
4239
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*defaultShutdownTimeoutMs)
4340
defer cancel()
4441
return s.h.Shutdown(ctx)
4542
}
4643

44+
// NewServer returns a metric server
4745
func NewServer() (*Server, error) {
4846
promExporter, err := ocprom.NewExporter(
4947
ocprom.Options{
@@ -61,51 +59,16 @@ func NewServer() (*Server, error) {
6159
}, nil
6260
}
6361

62+
// HTTPHandler returns a http.Handler wrapper that instruments given HTTP server's handler
6463
func HTTPHandler(h http.Handler) http.Handler {
6564
return &ochttp.Handler{
6665
Handler: h,
6766
}
6867
}
6968

69+
// HTTPTransport returns a http.RoundTripper wrapper that instruments all outgoing requests from base
7070
func HTTPTransport(base *http.Transport) http.RoundTripper {
7171
return &ochttp.Transport{
7272
Base: base,
7373
}
7474
}
75-
76-
func registerDefaultServerViews() {
77-
views := []*view.View{
78-
ochttp.ServerRequestCountView,
79-
ochttp.ServerRequestBytesView,
80-
ochttp.ServerResponseBytesView,
81-
ochttp.ServerLatencyView,
82-
ochttp.ServerRequestCountByMethod,
83-
ochttp.ServerResponseCountByStatusCode,
84-
}
85-
for _, view := range views {
86-
view.TagKeys = []tag.Key{
87-
ochttp.Path,
88-
ochttp.Method,
89-
ochttp.StatusCode,
90-
}
91-
}
92-
view.Register(views...)
93-
}
94-
95-
func registerDefaultClientViews() {
96-
views := []*view.View{
97-
ochttp.ClientSentBytesDistribution,
98-
ochttp.ClientReceivedBytesDistribution,
99-
ochttp.ClientRoundtripLatencyDistribution,
100-
ochttp.ClientCompletedCount,
101-
}
102-
for _, view := range views {
103-
view.TagKeys = []tag.Key{
104-
ochttp.KeyClientHost,
105-
ochttp.KeyClientPath,
106-
ochttp.KeyClientMethod,
107-
ochttp.KeyClientStatus,
108-
}
109-
}
110-
view.Register(views...)
111-
}

Diff for: metric/view.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package metric
2+
3+
import (
4+
"go.opencensus.io/plugin/ochttp"
5+
"go.opencensus.io/stats/view"
6+
"go.opencensus.io/tag"
7+
)
8+
9+
var (
10+
defaultSizeDistribution = view.Distribution(1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296)
11+
defaultLatencyDistribution = view.Distribution(1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000)
12+
)
13+
14+
var (
15+
httpViews = []*view.View{
16+
&view.View{
17+
Name: "relay/http/request_count",
18+
Description: "Count of HTTP requests started",
19+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
20+
Measure: ochttp.ServerRequestCount,
21+
Aggregation: view.Count(),
22+
},
23+
&view.View{
24+
Name: "relay/http/request_bytes",
25+
Description: "Size distribution of HTTP request body",
26+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
27+
Measure: ochttp.ServerRequestBytes,
28+
Aggregation: defaultSizeDistribution,
29+
},
30+
&view.View{
31+
Name: "relay/http/response_bytes",
32+
Description: "Size distribution of HTTP response body",
33+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
34+
Measure: ochttp.ServerResponseBytes,
35+
Aggregation: defaultSizeDistribution,
36+
},
37+
&view.View{
38+
Name: "relay/http/latency",
39+
Description: "Latency distribution of HTTP requests",
40+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
41+
Measure: ochttp.ServerLatency,
42+
Aggregation: defaultLatencyDistribution,
43+
},
44+
&view.View{
45+
Name: "relay/http/request_count_by_method",
46+
Description: "Server request count by HTTP method",
47+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
48+
Measure: ochttp.ServerRequestCount,
49+
Aggregation: view.Count(),
50+
},
51+
&view.View{
52+
Name: "relay/http/response_count_by_status_code",
53+
Description: "Server response count by status code",
54+
TagKeys: []tag.Key{ochttp.Path, ochttp.Method, ochttp.StatusCode},
55+
Measure: ochttp.ServerLatency,
56+
Aggregation: view.Count(),
57+
},
58+
}
59+
60+
httpOutputViews = []*view.View{
61+
&view.View{
62+
Name: "relay/http/output/sent_bytes",
63+
Description: "Total bytes sent in request body (not including headers), by HTTP method and response status",
64+
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientPath, ochttp.KeyClientMethod, ochttp.KeyClientStatus},
65+
Measure: ochttp.ClientSentBytes,
66+
Aggregation: defaultSizeDistribution,
67+
},
68+
&view.View{
69+
Name: "relay/http/output/received_bytes",
70+
Description: "Total bytes received in response bodies (not including headers but including error responses with bodies), by HTTP method and response status",
71+
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientPath, ochttp.KeyClientMethod, ochttp.KeyClientStatus},
72+
Measure: ochttp.ClientReceivedBytes,
73+
Aggregation: defaultSizeDistribution,
74+
},
75+
&view.View{
76+
Name: "relay/http/output/latency",
77+
Description: "End-to-end latency, by HTTP method and response status",
78+
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientPath, ochttp.KeyClientMethod, ochttp.KeyClientStatus},
79+
Measure: ochttp.ClientRoundtripLatency,
80+
Aggregation: defaultSizeDistribution,
81+
},
82+
&view.View{
83+
Name: "relay/http/output/completed_count",
84+
Description: "Count of completed requests, by HTTP method and response status",
85+
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientPath, ochttp.KeyClientMethod, ochttp.KeyClientStatus},
86+
Measure: ochttp.ClientRoundtripLatency,
87+
Aggregation: view.Count(),
88+
},
89+
}
90+
)
91+
92+
func init() {
93+
view.Register(httpViews...)
94+
view.Register(httpOutputViews...)
95+
}

0 commit comments

Comments
 (0)