Skip to content

Commit 99e2e2c

Browse files
committed
Merge branch 'improve-error-logging' into dev
2 parents 17f1173 + 6654703 commit 99e2e2c

File tree

18 files changed

+173
-152
lines changed

18 files changed

+173
-152
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ on:
99
permissions:
1010
contents: read
1111
# Optional: allow read access to pull request. Use with `only-new-issues` option.
12-
# pull-requests: read
12+
pull-requests: read
1313

1414
jobs:
1515
golangci:
1616
name: lint
1717
runs-on: ubuntu-latest
1818
steps:
19-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
2020
with:
2121
fetch-depth: 0
22-
- uses: actions/setup-go@v4
22+
- uses: actions/setup-go@v5
2323
with:
24-
go-version: '1.22.x'
24+
go-version: '1.23'
2525
cache: false
2626
- name: golangci-lint
27-
uses: golangci/golangci-lint-action@v3
27+
uses: golangci/golangci-lint-action@v6
2828
with:
29-
version: 'v1.56.2'
30-
# only-new-issues: true
29+
version: 'v1.60'
30+
only-new-issues: true
3131
- id: govulncheck
3232
uses: golang/govulncheck-action@v1

.github/workflows/stale.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.github/workflows/test-release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ jobs:
1515
name: test
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919
with:
2020
fetch-depth: 0
2121
- uses: FedericoCarboni/setup-ffmpeg@v2
22-
- uses: actions/setup-go@v4
22+
- uses: actions/setup-go@v5
2323
with:
24-
go-version: '1.22.x'
24+
go-version: '1.23'
2525
id: go
2626

2727
- name: Retrieve release details
@@ -78,12 +78,12 @@ jobs:
7878
runs-on: ubuntu-latest
7979

8080
steps:
81-
- uses: actions/checkout@v3
81+
- uses: actions/checkout@v4
8282
with:
8383
fetch-depth: 0
84-
- uses: actions/setup-go@v4
84+
- uses: actions/setup-go@v5
8585
with:
86-
go-version: '1.22.x'
86+
go-version: '1.23'
8787
id: go
8888

8989
- run: git branch

Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ prepare_test:
1919
$(go) install $($(go) list -tags tools -f '{{range $_, $p := .Imports}}{{$p}} {{end}}')
2020
$(go) run . db_migrate_up
2121

22-
.PHONY: test_circleci
23-
test_circleci:
24-
scripts/wait_for_wallet.sh
25-
cd tools &&\
26-
$(go) install $($(go) list -tags tools -f '{{range $_, $p := .Imports}}{{$p}} {{end}}')
27-
$(go) run . db_migrate_up
28-
$(go) test -covermode=count -coverprofile=coverage.out ./...
29-
goveralls -coverprofile=coverage.out -service=circle-ci -ignore=models/ -repotoken $(COVERALLS_TOKEN)
30-
3122
.PHONY: clean
3223
clean:
3324
rm -rf ./dist

app/arweave/arweave_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestReplaceAssetUrl(t *testing.T) {
5353
}
5454

5555
func TestGetClaimUrl(t *testing.T) {
56-
// t.Skip("skipping this in automated mode as it requires extra setup on arfleet")
56+
t.Skip("skipping this in automated mode as it requires extra setup on arfleet")
5757

5858
require := require.New(t)
5959
assert := assert.New(t)

app/asynquery/asynquery.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/OdyseeTeam/odysee-api/app/geopublish/metrics"
1515
"github.com/OdyseeTeam/odysee-api/app/query"
16+
"github.com/OdyseeTeam/odysee-api/internal/monitor"
1617
"github.com/OdyseeTeam/odysee-api/internal/tasks"
1718
"github.com/OdyseeTeam/odysee-api/models"
1819
"github.com/OdyseeTeam/odysee-api/pkg/logging"
@@ -232,8 +233,9 @@ func (m *CallManager) robustCall(ctx context.Context, aq *models.Asynquery, patc
232233
log.Info("error getting sdk address for user")
233234
return asynq.SkipRetry
234235
}
236+
sdkAddress := u.R.LbrynetServer.Address
235237

236-
caller := query.NewCaller(u.R.LbrynetServer.Address, aq.UserID)
238+
caller := query.NewCaller(sdkAddress, aq.UserID)
237239

238240
t := time.Now()
239241

@@ -261,18 +263,23 @@ func (m *CallManager) robustCall(ctx context.Context, aq *models.Asynquery, patc
261263
}
262264
delete(pp, "file_path")
263265

264-
res, err := caller.Call(context.TODO(), request)
266+
ctx = query.AttachOrigin(ctx, "asynquery")
267+
res, err := caller.Call(ctx, request)
265268
metrics.ProcessingTime.WithLabelValues(metrics.LabelProcessingQuery).Observe(float64(time.Since(t)))
266-
267269
QueriesSent.Inc()
270+
268271
if err != nil {
269272
QueriesFailed.Inc()
270-
log.Warn(sdkNetError.Error(), "err", err)
271-
resErrMsg := err.Error()
272-
log.Warn("asynquery failed", "err", resErrMsg)
273-
m.finalizeQueryRecord(ctx, aq.ID, nil, err.Error())
273+
log.Warn("asynquery request failed", "err", err)
274+
monitor.ErrorToSentry(fmt.Errorf("asynquery request failed: %w", err), map[string]string{
275+
"user_id": fmt.Sprintf("%d", u.ID),
276+
"endpoint": sdkAddress,
277+
"method": request.Method,
278+
})
279+
280+
err := m.finalizeQueryRecord(ctx, aq.ID, nil, err.Error())
274281
if err != nil {
275-
log.Warn("failed to finalize query record", "err", err)
282+
log.Warn("failed to finalize asynquery record", "err", err)
276283
}
277284
return sdkNetError
278285
}

app/geopublish/geopublish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func getCaller(sdkAddress, filename string, userID int, qCache *cache.Cache) *qu
426426
c := query.NewCaller(sdkAddress, userID)
427427
c.Cache = qCache
428428
c.AddPreflightHook(query.AllMethodsHook, func(_ *query.Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) {
429-
q := query.GetFromContext(ctx)
429+
q := query.QueryFromContext(ctx)
430430
params := q.ParamsAsMap()
431431
params[fileNameParam] = filename
432432
q.Request.Params = params

app/proxy/proxy.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ func Handle(w http.ResponseWriter, r *http.Request) {
136136

137137
c.Cache = qCache
138138

139-
metrics.ProxyCallCounter.WithLabelValues(rpcReq.Method, c.Endpoint(), origin).Inc()
140139
rpcRes, err := c.Call(r.Context(), rpcReq)
141-
metrics.ProxyCallDurations.WithLabelValues(rpcReq.Method, c.Endpoint(), origin).Observe(c.Duration)
142140

143141
if err != nil {
144142
writeResponse(w, rpcerrors.ToJSON(err))
@@ -154,8 +152,6 @@ func Handle(w http.ResponseWriter, r *http.Request) {
154152
}
155153
monitor.ErrorToSentry(err, map[string]string{"request": fmt.Sprintf("%+v", rpcReq), "response": fmt.Sprintf("%+v", rpcRes)})
156154
observeFailure(metrics.GetDuration(r), rpcReq.Method, metrics.FailureKindNet)
157-
metrics.ProxyCallFailedDurations.WithLabelValues(rpcReq.Method, c.Endpoint(), origin, metrics.FailureKindNet).Observe(c.Duration)
158-
metrics.ProxyCallFailedCounter.WithLabelValues(rpcReq.Method, c.Endpoint(), origin, metrics.FailureKindNet).Inc()
159155
logger.Log().Errorf("error calling lbrynet: %v, request: %+v", err, rpcReq)
160156
return
161157
}

app/publish/publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func getCaller(sdkAddress, filename string, userID int, qCache *cache.Cache) *qu
227227
c := query.NewCaller(sdkAddress, userID)
228228
c.Cache = qCache
229229
c.AddPreflightHook(query.AllMethodsHook, func(_ *query.Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) {
230-
q := query.GetFromContext(ctx)
230+
q := query.QueryFromContext(ctx)
231231
params := q.ParamsAsMap()
232232
params[fileNameParam] = filename
233233
q.Request.Params = params

app/query/caller.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ const (
3333
AllMethodsHook = ""
3434
)
3535

36-
type contextKey string
37-
38-
var (
39-
contextKeyQuery = contextKey("query")
40-
contextKeyResponse = contextKey("response")
41-
contextKeyLogEntry = contextKey("log-entry")
42-
)
43-
4436
type HTTPRequester interface {
4537
Do(req *http.Request) (res *http.Response, err error)
4638
}
@@ -50,38 +42,13 @@ type HTTPRequester interface {
5042
// Hooks can modify both query and response, as well as perform additional queries via supplied Caller.
5143
// If nil is returned instead of *jsonrpc.RPCResponse, original response is returned.
5244
type Hook func(*Caller, context.Context) (*jsonrpc.RPCResponse, error)
45+
5346
type hookEntry struct {
5447
method string
5548
function Hook
5649
name string
5750
}
5851

59-
func AttachToContext(ctx context.Context, query *Query) context.Context {
60-
return context.WithValue(ctx, contextKeyQuery, query)
61-
}
62-
63-
func GetFromContext(ctx context.Context) *Query {
64-
return ctx.Value(contextKeyQuery).(*Query)
65-
}
66-
67-
func WithResponse(ctx context.Context, response *jsonrpc.RPCResponse) context.Context {
68-
return context.WithValue(ctx, contextKeyResponse, response)
69-
}
70-
71-
func GetResponse(ctx context.Context) *jsonrpc.RPCResponse {
72-
return ctx.Value(contextKeyResponse).(*jsonrpc.RPCResponse)
73-
}
74-
75-
func WithLogEntry(ctx context.Context, entry *logrus.Entry) context.Context {
76-
return context.WithValue(ctx, contextKeyLogEntry, entry)
77-
}
78-
79-
// WithLogField injects additional data into default post-query log entry
80-
func WithLogField(ctx context.Context, key string, value interface{}) {
81-
e := ctx.Value(contextKeyLogEntry).(*logrus.Entry)
82-
e.Data[key] = value
83-
}
84-
8552
// Caller patches through JSON-RPC requests from clients, doing pre/post-processing,
8653
// account processing and validation.
8754
type Caller struct {
@@ -159,8 +126,10 @@ func (c *Caller) addDefaultHooks() {
159126
c.AddPreflightHook(MethodGet, preflightHookGet, builtinHookName)
160127
c.AddPreflightHook(MethodClaimSearch, preflightHookClaimSearch, builtinHookName)
161128

162-
c.AddPostflightHook(MethodClaimSearch, postClaimSearchArfleetThumbs, builtinHookName)
163-
c.AddPostflightHook(MethodResolve, postResolveArfleetThumbs, builtinHookName)
129+
if config.GetArfleetEnabled() {
130+
c.AddPostflightHook(MethodClaimSearch, postClaimSearchArfleetThumbs, builtinHookName)
131+
c.AddPostflightHook(MethodResolve, postResolveArfleetThumbs, builtinHookName)
132+
}
164133
}
165134

166135
func (c *Caller) CloneWithoutHook(endpoint, method, name string) *Caller {
@@ -187,6 +156,20 @@ func (c *Caller) Endpoint() string {
187156
// Call method forwards a JSON-RPC request to the lbrynet server.
188157
// It returns a response that is ready to be sent back to the JSON-RPC client as is.
189158
func (c *Caller) Call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RPCResponse, error) {
159+
origin := OriginFromContext(ctx)
160+
metrics.ProxyCallCounter.WithLabelValues(req.Method, c.Endpoint(), origin).Inc()
161+
res, err := c.call(ctx, req)
162+
metrics.ProxyCallDurations.WithLabelValues(req.Method, c.Endpoint(), origin).Observe(c.Duration)
163+
if err != nil {
164+
metrics.ProxyCallFailedDurations.WithLabelValues(req.Method, c.Endpoint(), origin, metrics.FailureKindNet).Observe(c.Duration)
165+
metrics.ProxyCallFailedCounter.WithLabelValues(req.Method, c.Endpoint(), origin, metrics.FailureKindNet).Inc()
166+
}
167+
return res, err
168+
}
169+
170+
// Call method forwards a JSON-RPC request to the lbrynet server.
171+
// It returns a response that is ready to be sent back to the JSON-RPC client as is.
172+
func (c *Caller) call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RPCResponse, error) {
190173
if c.endpoint == "" {
191174
return nil, errors.Err("cannot call blank endpoint")
192175
}
@@ -203,7 +186,7 @@ func (c *Caller) Call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RP
203186

204187
// Applying preflight hooks
205188
var res *jsonrpc.RPCResponse
206-
ctx = AttachToContext(ctx, q)
189+
ctx = AttachQuery(ctx, q)
207190
for _, hook := range c.preflightHooks {
208191
if isMatchingHook(q.Method(), hook) {
209192
res, err = hook.function(c, ctx)
@@ -251,7 +234,7 @@ func (c *Caller) SendQuery(ctx context.Context, q *Query) (*jsonrpc.RPCResponse,
251234
start := time.Now()
252235
client := c.getRPCClient(q.Method())
253236
r, err = client.CallRaw(q.Request)
254-
c.Duration = time.Since(start).Seconds()
237+
c.Duration += time.Since(start).Seconds()
255238
logger.Log().Debugf("sent request: %s %+v (%.2fs)", q.Method(), q.Params(), c.Duration)
256239

257240
// Generally a HTTP transport failure (connect error etc)
@@ -303,8 +286,8 @@ func (c *Caller) SendQuery(ctx context.Context, q *Query) (*jsonrpc.RPCResponse,
303286

304287
// Applying postflight hooks
305288
var hookResp *jsonrpc.RPCResponse
306-
ctx = WithLogEntry(ctx, logEntry)
307-
ctx = WithResponse(ctx, r)
289+
ctx = AttachLogEntry(ctx, logEntry)
290+
ctx = AttachResponse(ctx, r)
308291
for _, hook := range c.postflightHooks {
309292
if isMatchingHook(q.Method(), hook) {
310293
hookResp, err = hook.function(c, ctx)

0 commit comments

Comments
 (0)