-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.go
166 lines (146 loc) · 3.93 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package storage
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"strings"
"time"
"github.com/gunsluo/go-example/opentracing/pkg/internal"
identitypb "github.com/gunsluo/go-example/opentracing/pkg/proto/identity"
"github.com/gunsluo/go-example/opentracing/pkg/trace"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/sirupsen/logrus"
)
// Database simulates Customer repository implemented on top of an SQL Database
type Database struct {
tracer opentracing.Tracer
logger *logrus.Logger
accounts map[string]*internal.Account
identities map[string]*identitypb.Identity
}
func NewDatabase(logger *logrus.Logger) *Database {
tracer := trace.Init("postgres", logger, nil)
return &Database{
tracer: tracer,
logger: logger,
accounts: map[string]*internal.Account{
"123": {
Id: "123",
Name: "Rachel's Floral Designs",
Email: "rachel@test.com",
},
"567": {
Id: "567",
Name: "Amazing Coffee Roasters",
Email: "amazing@test.com",
},
"392": {
Id: "392",
Name: "Trom Chocolatier",
Email: "trom@test.com",
},
"731": {
Id: "731",
Name: "Japanese Desserts",
Email: "dess@test.com",
},
},
identities: map[string]*identitypb.Identity{
"123": {
Id: "123",
Name: "Rachel's Floral Designs",
CertId: "xxxx001",
},
"567": {
Id: "567",
Name: "Amazing Coffee Roasters",
CertId: "xxxx002",
},
"392": {
Id: "392",
Name: "Trom Chocolatier",
CertId: "xxxx003",
},
"731": {
Id: "731",
Name: "Japanese Desserts",
CertId: "xxxx004",
},
},
}
}
func (d *Database) GetAccount(ctx context.Context, id string) (*internal.Account, error) {
d.logger.WithField("account id", id).Info("Loading account")
sqlstr := "SELECT * FROM account WHERE id=?"
out := &internal.Account{}
if err := d.invokeQuery(ctx, sqlstr, id, out); err != nil {
return nil, fmt.Errorf("failed to query account id: %s, %w", id, err)
}
return out, nil
}
func (d *Database) GetIdentity(ctx context.Context, id string) (*identitypb.Identity, error) {
d.logger.WithField("user id", id).Info("Loading identity")
sqlstr := "SELECT * FROM identity WHERE id=?"
out := &identitypb.Identity{}
if err := d.invokeQuery(ctx, sqlstr, id, out); err != nil {
return nil, fmt.Errorf("failed to query identity id: %s, %w", id, err)
}
return out, nil
}
func (d *Database) invokeQuery(ctx context.Context, sql string, in, out interface{}) error {
if d.tracer == nil {
return d.invoke(ctx, sql, in, out)
}
// simulate opentracing instrumentation of an SQL query
if span := opentracing.SpanFromContext(ctx); span != nil {
span := d.tracer.StartSpan("SQL SELECT", opentracing.ChildOf(span.Context()))
ext.SpanKindRPCClient.Set(span)
ext.PeerService.Set(span, "postgres")
// #nosec
span.SetTag("sql.query", sql)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
}
err := d.invoke(ctx, sql, in, out)
if err != nil {
if span := opentracing.SpanFromContext(ctx); span != nil {
ext.Error.Set(span, true)
}
}
return err
}
func (d *Database) invoke(ctx context.Context, sql string, in, out interface{}) error {
// simulate RPC delay
delay := time.Duration(math.Max(1, rand.NormFloat64()*300+30) * float64(time.Millisecond))
time.Sleep(delay)
if strings.Index(sql, "account") > 0 {
if id, ok := in.(string); ok {
if a, ok := d.accounts[id]; ok {
if o, ok := out.(*internal.Account); ok {
o.Id = a.Id
o.Name = a.Name
o.Email = a.Email
return nil
}
}
}
return errors.New("invalid user ID")
}
if strings.Index(sql, "identity") > 0 {
if id, ok := in.(string); ok {
if a, ok := d.identities[id]; ok {
if o, ok := out.(*identitypb.Identity); ok {
o.Id = a.Id
o.Name = a.Name
o.CertId = a.CertId
return nil
}
}
}
return errors.New("invalid ID")
}
return errors.New("invalid sql")
}