MCPcopy
hub / github.com/grafana/dskit / CollectedRequest

Function CollectedRequest

instrument/instrument.go:157–182  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

155// for `toStatusCode` to overwrite this behaviour. It will also emit an OpenTracing span if
156// you have a global tracer configured.
157func 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
185func ErrorCode(err error) string {

Callers 15

ListMethod · 0.92
GetMethod · 0.92
DeleteMethod · 0.92
CASMethod · 0.92
WatchKeyMethod · 0.92
WatchPrefixMethod · 0.92
CASMethod · 0.92
GetMethod · 0.92
ListMethod · 0.92
DeleteMethod · 0.92
PutMethod · 0.92
PutMethod · 0.92

Calls 10

StartSpanFromContextFunction · 0.92
ExtractUserIDFunction · 0.92
ExtractOrgIDFunction · 0.92
IsCanceledFunction · 0.92
LogErrorMethod · 0.80
BeforeMethod · 0.65
AfterMethod · 0.65
FinishMethod · 0.45
SetTagMethod · 0.45
SetErrorMethod · 0.45

Tested by 2

TestCollectedRequestFunction · 0.74