@@ -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
6369type 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
133146func (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