Skip to content

Commit 967415b

Browse files
committed
examples: add logging to basic price oracle service
1 parent 27625e1 commit 967415b

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

docs/examples/basic-price-oracle/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ replace github.com/lightninglabs/taproot-assets => ../../../
88

99
require (
1010
github.com/lightninglabs/taproot-assets v0.0.0
11+
github.com/sirupsen/logrus v1.9.2
1112
google.golang.org/grpc v1.59.0
1213
)
1314

docs/examples/basic-price-oracle/go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
25
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
36
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
@@ -6,8 +9,17 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
69
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
710
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms=
811
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
12+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
13+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14+
github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
15+
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
16+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
18+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
19+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
920
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
1021
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
22+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1123
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
1224
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1325
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
@@ -22,3 +34,7 @@ google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
2234
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
2335
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
2436
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
37+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
40+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

docs/examples/basic-price-oracle/main.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ import (
1515
"crypto/x509/pkix"
1616
"encoding/pem"
1717
"fmt"
18+
"io"
1819
"log"
1920
"math/big"
2021
"net"
22+
"os"
2123
"time"
2224

25+
"github.com/sirupsen/logrus"
26+
2327
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
2428
"google.golang.org/grpc"
2529
"google.golang.org/grpc/credentials"
@@ -30,6 +34,30 @@ const (
3034
serviceListenAddress = "localhost:8095"
3135
)
3236

37+
// setupLogger sets up the logger to write logs to a file.
38+
func setupLogger() {
39+
// Create a log file.
40+
flags := os.O_CREATE | os.O_WRONLY | os.O_APPEND
41+
file, err := os.OpenFile("basic-price-oracle-example.log", flags, 0666)
42+
if err != nil {
43+
logrus.Fatalf("Failed to open log file: %v", err)
44+
}
45+
46+
// Create a multi-writer to write to both stdout and the file.
47+
multiWriter := io.MultiWriter(os.Stdout, file)
48+
49+
// Set the output of logrus to the multi-writer.
50+
logrus.SetOutput(multiWriter)
51+
52+
// Set the log level (optional).
53+
logrus.SetLevel(logrus.DebugLevel)
54+
55+
// Set the log format (optional).
56+
logrus.SetFormatter(&logrus.TextFormatter{
57+
FullTimestamp: true,
58+
})
59+
}
60+
3361
// RpcPriceOracleServer is a basic example RPC price oracle server.
3462
type RpcPriceOracleServer struct {
3563
oraclerpc.UnimplementedPriceOracleServer
@@ -115,6 +143,8 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
115143
// Ensure that the payment asset is BTC. We only support BTC as the
116144
// payment asset in this example.
117145
if !oraclerpc.IsAssetBtc(req.PaymentAsset) {
146+
logrus.Infof("Payment asset is not BTC: %v", req.PaymentAsset)
147+
118148
return &oraclerpc.QueryRateTickResponse{
119149
Result: &oraclerpc.QueryRateTickResponse_Error{
120150
Error: &oraclerpc.QueryRateTickErrResponse{
@@ -127,11 +157,15 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
127157

128158
// Ensure that the subject asset is set.
129159
if req.SubjectAsset == nil {
160+
logrus.Info("Subject asset is not set")
130161
return nil, fmt.Errorf("subject asset is not set")
131162
}
132163

133164
// Ensure that the subject asset is supported.
134165
if !isSupportedSubjectAsset(req.SubjectAsset) {
166+
logrus.Infof("Unsupported subject asset ID str: %v\n",
167+
req.SubjectAsset)
168+
135169
return &oraclerpc.QueryRateTickResponse{
136170
Result: &oraclerpc.QueryRateTickResponse_Error{
137171
Error: &oraclerpc.QueryRateTickErrResponse{
@@ -148,16 +182,24 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
148182
// If a rate tick hint is provided, return it as the rate tick.
149183
// In doing so, we effectively accept the rate tick proposed by
150184
// our peer.
185+
logrus.Info("Suggested asset to BTC rate provided, " +
186+
"returning rate as accepted rate")
187+
151188
rateTick.Rate = req.RateTickHint.Rate
152189
rateTick.ExpiryTimestamp = req.RateTickHint.ExpiryTimestamp
153190
} else {
154191
// If a rate tick hint is not provided, fetch a rate tick from
155192
// our internal system.
193+
logrus.Info("Suggested asset to BTC rate not provided, " +
194+
"querying internal system for rate")
195+
156196
rateTick = getRateTick(
157197
req.TransactionType, req.SubjectAssetMaxAmount,
158198
)
159199
}
160200

201+
logrus.Infof("QueryRateTick returning rate: %v", rateTick.Rate)
202+
161203
return &oraclerpc.QueryRateTickResponse{
162204
Result: &oraclerpc.QueryRateTickResponse_Success{
163205
Success: &oraclerpc.QueryRateTickSuccessResponse{
@@ -171,7 +213,8 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
171213
// shut down.
172214
func startService(grpcServer *grpc.Server) error {
173215
serviceAddr := fmt.Sprintf("rfqrpc://%s", serviceListenAddress)
174-
println("Starting RPC price oracle service at address: ", serviceAddr)
216+
logrus.Infof("Starting RPC price oracle service at address: %s\n",
217+
serviceAddr)
175218

176219
server := RpcPriceOracleServer{}
177220
oraclerpc.RegisterPriceOracleServer(grpcServer, &server)
@@ -234,6 +277,8 @@ func generateSelfSignedCert() (tls.Certificate, error) {
234277
}
235278

236279
func main() {
280+
setupLogger()
281+
237282
// Start the mock RPC price oracle service.
238283
//
239284
// Generate self-signed certificate. This allows us to use TLS for the

0 commit comments

Comments
 (0)