Skip to content

Commit 0c4995c

Browse files
committed
Add service and refactor Recorder instance methods arguments
Signed-off-by: Xabier Larrakoetxea <[email protected]>
1 parent 2f3176a commit 0c4995c

File tree

5 files changed

+316
-275
lines changed

5 files changed

+316
-275
lines changed

metrics/metrics.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,47 @@ import (
55
"time"
66
)
77

8+
// HTTPReqProperties are the metric properties for the metrics based
9+
// on client request.
10+
type HTTPReqProperties struct {
11+
// Service is the service that has served the request.
12+
Service string
13+
// ID is the id of the request handler.
14+
ID string
15+
// Method is the method of the request.
16+
Method string
17+
// Code is the response of the request.
18+
Code string
19+
}
20+
21+
// HTTPProperties are the metric properties for the global server metrics.
22+
type HTTPProperties struct {
23+
// Service is the service that has served the request.
24+
Service string
25+
// ID is the id of the request handler.
26+
ID string
27+
}
28+
829
// Recorder knows how to record and measure the metrics. This
930
// Interface has the required methods to be used with the HTTP
1031
// middlewares.
1132
type Recorder interface {
1233
// ObserveHTTPRequestDuration measures the duration of an HTTP request.
13-
ObserveHTTPRequestDuration(ctx context.Context, id string, duration time.Duration, method, code string)
34+
ObserveHTTPRequestDuration(ctx context.Context, props HTTPReqProperties, duration time.Duration)
1435
// ObserveHTTPResponseSize measures the size of an HTTP response in bytes.
15-
ObserveHTTPResponseSize(ctx context.Context, id string, sizeBytes int64, method, code string)
36+
ObserveHTTPResponseSize(ctx context.Context, props HTTPReqProperties, sizeBytes int64)
1637
// AddInflightRequests increments and decrements the number of inflight request being
1738
// processed.
18-
AddInflightRequests(ctx context.Context, id string, quantity int)
39+
AddInflightRequests(ctx context.Context, props HTTPProperties, quantity int)
1940
}
2041

2142
// Dummy is a dummy recorder.
22-
var Dummy = &dummy{}
43+
const Dummy = dummy(0)
2344

24-
type dummy struct{}
45+
type dummy int
2546

26-
func (dummy) ObserveHTTPRequestDuration(ctx context.Context, id string, duration time.Duration, method, code string) {
27-
}
28-
func (dummy) ObserveHTTPResponseSize(ctx context.Context, id string, sizeBytes int64, method, code string) {
29-
}
30-
func (dummy) AddInflightRequests(ctx context.Context, id string, quantity int) {}
47+
func (dummy) ObserveHTTPRequestDuration(_ context.Context, _ HTTPReqProperties, _ time.Duration) {}
48+
func (dummy) ObserveHTTPResponseSize(_ context.Context, _ HTTPReqProperties, _ int64) {}
49+
func (dummy) AddInflightRequests(_ context.Context, _ HTTPProperties, _ int) {}
50+
51+
var _ Recorder = Dummy

metrics/opencensus/opencensus.go

+40-22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Config struct {
3030
StatusCodeLabel string
3131
// MethodLabel is the name that will be set to the method label, by default is `method`.
3232
MethodLabel string
33+
// ServiceLabel is the name that will be set to the service label, by default is `service`.
34+
ServiceLabel string
3335
// UnregisterViewsBeforeRegister will unregister the previous Recorder views before registering
3436
// again. This is required on cases where multiple instances of recorder will be made due to how
3537
// Opencensus is implemented (everything is at global state). Sadly this option is a kind of hack
@@ -58,13 +60,18 @@ func (c *Config) defaults() {
5860
if c.MethodLabel == "" {
5961
c.MethodLabel = "method"
6062
}
63+
64+
if c.ServiceLabel == "" {
65+
c.ServiceLabel = "service"
66+
}
6167
}
6268

6369
type recorder struct {
6470
// Keys.
6571
codeKey tag.Key
6672
methodKey tag.Key
6773
handlerKey tag.Key
74+
serviceKey tag.Key
6875

6976
// Measures.
7077
latencySecs *stats.Float64Measure
@@ -112,6 +119,12 @@ func (r *recorder) createKeys(cfg Config) error {
112119
}
113120
r.handlerKey = handler
114121

122+
service, err := tag.NewKey(cfg.ServiceLabel)
123+
if err != nil {
124+
return err
125+
}
126+
r.serviceKey = service
127+
115128
return nil
116129
}
117130

@@ -132,25 +145,25 @@ func (r *recorder) createMeasurements() {
132145

133146
func (r recorder) registerViews(cfg Config) error {
134147

135-
// OpenCensus uses global states, sadly we can't have view insta
148+
// OpenCensus uses global states, sadly we can't have view instance.
136149
durationView := &view.View{
137150
Name: "http_request_duration_seconds",
138151
Description: "The latency of the HTTP requests",
139-
TagKeys: []tag.Key{r.handlerKey, r.methodKey, r.codeKey},
152+
TagKeys: []tag.Key{r.serviceKey, r.handlerKey, r.methodKey, r.codeKey},
140153
Measure: r.latencySecs,
141154
Aggregation: view.Distribution(cfg.DurationBuckets...),
142155
}
143156
sizeView := &view.View{
144157
Name: "http_response_size_bytes",
145158
Description: "The size of the HTTP responses",
146-
TagKeys: []tag.Key{r.handlerKey, r.methodKey, r.codeKey},
159+
TagKeys: []tag.Key{r.serviceKey, r.handlerKey, r.methodKey, r.codeKey},
147160
Measure: r.sizeBytes,
148161
Aggregation: view.Distribution(cfg.SizeBuckets...),
149162
}
150163
inflightView := &view.View{
151164
Name: "http_requests_inflight",
152165
Description: "The number of inflight requests being handled at the same time",
153-
TagKeys: []tag.Key{r.handlerKey},
166+
TagKeys: []tag.Key{r.serviceKey, r.handlerKey},
154167
Measure: r.inflightCount,
155168
Aggregation: view.Sum(),
156169
}
@@ -168,30 +181,35 @@ func (r recorder) registerViews(cfg Config) error {
168181
return nil
169182
}
170183

171-
func (r recorder) ObserveHTTPRequestDuration(ctx context.Context, id string, duration time.Duration, method, code string) {
172-
ctx, _ = tag.New(ctx,
173-
tag.Upsert(r.handlerKey, id),
174-
tag.Upsert(r.methodKey, method),
175-
tag.Upsert(r.codeKey, code),
176-
)
177-
184+
func (r recorder) ObserveHTTPRequestDuration(ctx context.Context, p metrics.HTTPReqProperties, duration time.Duration) {
185+
ctx = r.ctxWithTagFromHTTPReqProperties(ctx, p)
178186
stats.Record(ctx, r.latencySecs.M(duration.Seconds()))
179187
}
180188

181-
func (r recorder) ObserveHTTPResponseSize(ctx context.Context, id string, sizeBytes int64, method, code string) {
182-
ctx, _ = tag.New(ctx,
183-
tag.Upsert(r.handlerKey, id),
184-
tag.Upsert(r.methodKey, method),
185-
tag.Upsert(r.codeKey, code),
186-
)
187-
189+
func (r recorder) ObserveHTTPResponseSize(ctx context.Context, p metrics.HTTPReqProperties, sizeBytes int64) {
190+
ctx = r.ctxWithTagFromHTTPReqProperties(ctx, p)
188191
stats.Record(ctx, r.sizeBytes.M(sizeBytes))
189192
}
190193

191-
func (r recorder) AddInflightRequests(ctx context.Context, id string, quantity int) {
192-
ctx, _ = tag.New(ctx,
193-
tag.Upsert(r.handlerKey, id),
194+
func (r recorder) AddInflightRequests(ctx context.Context, p metrics.HTTPProperties, quantity int) {
195+
ctx = r.ctxWithTagFromHTTPProperties(ctx, p)
196+
stats.Record(ctx, r.inflightCount.M(int64(quantity)))
197+
}
198+
199+
func (r recorder) ctxWithTagFromHTTPReqProperties(ctx context.Context, p metrics.HTTPReqProperties) context.Context {
200+
newCtx, _ := tag.New(ctx,
201+
tag.Upsert(r.serviceKey, p.Service),
202+
tag.Upsert(r.handlerKey, p.ID),
203+
tag.Upsert(r.methodKey, p.Method),
204+
tag.Upsert(r.codeKey, p.Code),
194205
)
206+
return newCtx
207+
}
195208

196-
stats.Record(ctx, r.inflightCount.M(int64(quantity)))
209+
func (r recorder) ctxWithTagFromHTTPProperties(ctx context.Context, p metrics.HTTPProperties) context.Context {
210+
newCtx, _ := tag.New(ctx,
211+
tag.Upsert(r.serviceKey, p.Service),
212+
tag.Upsert(r.handlerKey, p.ID),
213+
)
214+
return newCtx
197215
}

0 commit comments

Comments
 (0)