@@ -30,6 +30,8 @@ type Config struct {
30
30
StatusCodeLabel string
31
31
// MethodLabel is the name that will be set to the method label, by default is `method`.
32
32
MethodLabel string
33
+ // ServiceLabel is the name that will be set to the service label, by default is `service`.
34
+ ServiceLabel string
33
35
// UnregisterViewsBeforeRegister will unregister the previous Recorder views before registering
34
36
// again. This is required on cases where multiple instances of recorder will be made due to how
35
37
// Opencensus is implemented (everything is at global state). Sadly this option is a kind of hack
@@ -58,13 +60,18 @@ func (c *Config) defaults() {
58
60
if c .MethodLabel == "" {
59
61
c .MethodLabel = "method"
60
62
}
63
+
64
+ if c .ServiceLabel == "" {
65
+ c .ServiceLabel = "service"
66
+ }
61
67
}
62
68
63
69
type recorder struct {
64
70
// Keys.
65
71
codeKey tag.Key
66
72
methodKey tag.Key
67
73
handlerKey tag.Key
74
+ serviceKey tag.Key
68
75
69
76
// Measures.
70
77
latencySecs * stats.Float64Measure
@@ -112,6 +119,12 @@ func (r *recorder) createKeys(cfg Config) error {
112
119
}
113
120
r .handlerKey = handler
114
121
122
+ service , err := tag .NewKey (cfg .ServiceLabel )
123
+ if err != nil {
124
+ return err
125
+ }
126
+ r .serviceKey = service
127
+
115
128
return nil
116
129
}
117
130
@@ -132,25 +145,25 @@ func (r *recorder) createMeasurements() {
132
145
133
146
func (r recorder ) registerViews (cfg Config ) error {
134
147
135
- // OpenCensus uses global states, sadly we can't have view insta
148
+ // OpenCensus uses global states, sadly we can't have view instance.
136
149
durationView := & view.View {
137
150
Name : "http_request_duration_seconds" ,
138
151
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 },
140
153
Measure : r .latencySecs ,
141
154
Aggregation : view .Distribution (cfg .DurationBuckets ... ),
142
155
}
143
156
sizeView := & view.View {
144
157
Name : "http_response_size_bytes" ,
145
158
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 },
147
160
Measure : r .sizeBytes ,
148
161
Aggregation : view .Distribution (cfg .SizeBuckets ... ),
149
162
}
150
163
inflightView := & view.View {
151
164
Name : "http_requests_inflight" ,
152
165
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 },
154
167
Measure : r .inflightCount ,
155
168
Aggregation : view .Sum (),
156
169
}
@@ -168,30 +181,35 @@ func (r recorder) registerViews(cfg Config) error {
168
181
return nil
169
182
}
170
183
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 )
178
186
stats .Record (ctx , r .latencySecs .M (duration .Seconds ()))
179
187
}
180
188
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 )
188
191
stats .Record (ctx , r .sizeBytes .M (sizeBytes ))
189
192
}
190
193
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 ),
194
205
)
206
+ return newCtx
207
+ }
195
208
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
197
215
}
0 commit comments