CollectedRequest runs a tracked request. It uses the given Collector to monitor requests. If `f` returns no error we log "200" as status code, otherwise "500". Pass in a function for `toStatusCode` to overwrite this behaviour. It will also emit an OpenTracing span if you have a global tracer config
(ctx context.Context, method string, col Collector, toStatusCode func(error) string, f func(context.Context) error)
| 155 | // for `toStatusCode` to overwrite this behaviour. It will also emit an OpenTracing span if |
| 156 | // you have a global tracer configured. |
| 157 | func CollectedRequest(ctx context.Context, method string, col Collector, toStatusCode func(error) string, f func(context.Context) error) error { |
| 158 | if toStatusCode == nil { |
| 159 | toStatusCode = ErrorCode |
| 160 | } |
| 161 | sp, newCtx := tracing.StartSpanFromContext(ctx, method, tracing.SpanKindRPCClient{}) |
| 162 | defer sp.Finish() |
| 163 | if userID, err := user.ExtractUserID(ctx); err == nil { |
| 164 | sp.SetTag("user", userID) |
| 165 | } |
| 166 | if orgID, err := user.ExtractOrgID(ctx); err == nil { |
| 167 | sp.SetTag("organization", orgID) |
| 168 | } |
| 169 | |
| 170 | start := time.Now() |
| 171 | col.Before(newCtx, method, start) |
| 172 | err := f(newCtx) |
| 173 | col.After(newCtx, method, toStatusCode(err), start) |
| 174 | |
| 175 | if err != nil { |
| 176 | if !grpcutil.IsCanceled(err) { |
| 177 | sp.SetError() |
| 178 | } |
| 179 | sp.LogError(err) |
| 180 | } |
| 181 | return err |
| 182 | } |
| 183 | |
| 184 | // ErrorCode converts an error into an HTTP status code |
| 185 | func ErrorCode(err error) string { |