-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move metrics tracking to caller, add observation to asynquery failures
- Loading branch information
Showing
8 changed files
with
107 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package query | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/ybbus/jsonrpc" | ||
) | ||
|
||
type contextKey string | ||
|
||
var ( | ||
contextKeyQuery = contextKey("query") | ||
contextKeyResponse = contextKey("response") | ||
contextKeyLogEntry = contextKey("log-entry") | ||
contextKeyOrigin = contextKey("origin") | ||
) | ||
|
||
func AttachQuery(ctx context.Context, query *Query) context.Context { | ||
return context.WithValue(ctx, contextKeyQuery, query) | ||
} | ||
|
||
func QueryFromContext(ctx context.Context) *Query { | ||
return ctx.Value(contextKeyQuery).(*Query) | ||
} | ||
|
||
func AttachResponse(ctx context.Context, response *jsonrpc.RPCResponse) context.Context { | ||
return context.WithValue(ctx, contextKeyResponse, response) | ||
} | ||
|
||
func ResponseFromContext(ctx context.Context) *jsonrpc.RPCResponse { | ||
return ctx.Value(contextKeyResponse).(*jsonrpc.RPCResponse) | ||
} | ||
|
||
func AttachLogEntry(ctx context.Context, entry *logrus.Entry) context.Context { | ||
return context.WithValue(ctx, contextKeyLogEntry, entry) | ||
} | ||
|
||
// WithLogField injects additional data into default post-query log entry | ||
func WithLogField(ctx context.Context, key string, value interface{}) { | ||
e := ctx.Value(contextKeyLogEntry).(*logrus.Entry) | ||
e.Data[key] = value | ||
} | ||
|
||
func AttachOrigin(ctx context.Context, origin string) context.Context { | ||
return context.WithValue(ctx, contextKeyOrigin, origin) | ||
} | ||
|
||
func OriginFromContext(ctx context.Context) string { | ||
if origin, ok := ctx.Value(contextKeyOrigin).(string); ok { | ||
return origin | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters