forked from airbrake/gobrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_metric.go
67 lines (58 loc) · 1.13 KB
/
route_metric.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
package gobrake
import (
"context"
"strings"
)
type RouteMetric struct {
metric
Method string
Route string
StatusCode int
ContentType string
root Span
}
var _ Metric = (*RouteMetric)(nil)
func NewRouteMetric(c context.Context, method, route string) (context.Context, *RouteMetric) {
t := &RouteMetric{
Method: method,
Route: route,
}
t.metric.init()
if c != nil {
c = withMetric(c, t)
}
c, t.root = t.Start(c, "http.handler")
return c, t
}
func ContextRouteMetric(c context.Context) *RouteMetric {
if c == nil {
return nil
}
t, _ := c.Value(metricCtxKey).(*RouteMetric)
return t
}
func (t *RouteMetric) Start(c context.Context, name string) (context.Context, Span) {
if t == nil {
return c, noopSpan{}
}
return t.metric.Start(c, name)
}
func (t *RouteMetric) finish() {
t.root.Finish()
t.metric.finish()
}
func (t *RouteMetric) respType() string {
if t.StatusCode >= 500 {
return "5xx"
} else if t.StatusCode >= 400 {
return "4xx"
}
if t.ContentType == "" {
return ""
}
ind := strings.LastIndexByte(t.ContentType, '/')
if ind != -1 {
return t.ContentType[ind+1:]
}
return t.ContentType
}