forked from shanbay/ent
/
stats.go
117 lines (103 loc) · 3.46 KB
/
stats.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
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package ocgremlin
import (
"context"
"strconv"
"time"
"github.com/apocelipes/ent/dialect/gremlin"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)
// The following measures are supported for use in custom views.
var (
RequestCount = stats.Int64(
"gremlin/request_count",
"Number of Gremlin requests started",
stats.UnitDimensionless,
)
ResponseBytes = stats.Int64(
"gremlin/response_bytes",
"Total number of bytes in response data",
stats.UnitBytes,
)
RoundTripLatency = stats.Float64(
"gremlin/roundtrip_latency",
"End-to-end latency",
stats.UnitMilliseconds,
)
)
// The following tags are applied to stats recorded by this package.
var (
// StatusCode is the numeric Gremlin response status code,
// or "error" if a transport error occurred and no status code was read.
StatusCode, _ = tag.NewKey("gremlin_status_code")
)
// Default distributions used by views in this package.
var (
DefaultSizeDistribution = view.Distribution(32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576)
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, 50000, 100000)
)
// Package ocgremlin provides some convenience views for measures.
// You still need to register these views for data to actually be collected.
var (
RequestCountView = &view.View{
Name: "gremlin/request_count",
Measure: RequestCount,
Aggregation: view.Count(),
Description: "Count of Gremlin requests started",
}
ResponseCountView = &view.View{
Name: "gremlin/response_count",
Measure: RoundTripLatency,
Aggregation: view.Count(),
Description: "Count of responses received, by response status",
TagKeys: []tag.Key{StatusCode},
}
ResponseBytesView = &view.View{
Name: "gremlin/response_bytes",
Measure: ResponseBytes,
Aggregation: DefaultSizeDistribution,
Description: "Total number of bytes in response data",
}
RoundTripLatencyView = &view.View{
Name: "gremlin/roundtrip_latency",
Measure: RoundTripLatency,
Aggregation: DefaultLatencyDistribution,
Description: "End-to-end latency, by response code",
TagKeys: []tag.Key{StatusCode},
}
)
// Views are the default views provided by this package.
func Views() []*view.View {
return []*view.View{
RequestCountView,
ResponseCountView,
ResponseBytesView,
RoundTripLatencyView,
}
}
// statsTransport is an gremlin.RoundTripper that collects stats for the outgoing requests.
type statsTransport struct {
base gremlin.RoundTripper
}
func (t statsTransport) RoundTrip(ctx context.Context, req *gremlin.Request) (*gremlin.Response, error) {
stats.Record(ctx, RequestCount.M(1))
start := time.Now()
rsp, err := t.base.RoundTrip(ctx, req)
latency := float64(time.Since(start)) / float64(time.Millisecond)
var (
tags = make([]tag.Mutator, 1)
ms = []stats.Measurement{RoundTripLatency.M(latency)}
)
if err == nil {
tags[0] = tag.Upsert(StatusCode, strconv.Itoa(rsp.Status.Code))
ms = append(ms, ResponseBytes.M(int64(len(rsp.Result.Data))))
} else {
tags[0] = tag.Upsert(StatusCode, "error")
}
_ = stats.RecordWithTags(ctx, tags, ms...)
return rsp, err
}