-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation_database.go
238 lines (210 loc) · 7.4 KB
/
instrumentation_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Database instrumentation
- This is an implementation of Database that accepts a logging monitor and another implementor
of Database. All this does is pass any calls to the database it wraps & passes
back the result(s). In the process, every action is logged to the given monitor.
*/
package main
import (
wyc "github.com/voidshard/wysteria/common"
wyd "github.com/voidshard/wysteria/server/database"
wsi "github.com/voidshard/wysteria/server/instrumentation"
"time"
)
func newDatabaseMonitor(db wyd.Database, monitor *wsi.Monitor) wyd.Database {
return &DatabaseMonitor{database: db, monitor: monitor}
}
type DatabaseMonitor struct {
database wyd.Database
monitor *wsi.Monitor
}
// Call on our monitor to do the actual logging business
//
func (s *DatabaseMonitor) log(err error, t int64, opts ...wsi.Opt) {
area := "database"
go func() {
opts = append(opts, wsi.Time(time.Now().UnixNano()-t))
if err != nil {
s.monitor.Err(err, area, opts...)
return
}
s.monitor.Log(area, opts...)
}()
}
// Set given version id as published
func (d *DatabaseMonitor) SetPublished(in string) error {
ts := time.Now().UnixNano()
err := d.database.SetPublished(in)
d.log(
err, ts,
wsi.IsPublish(), wsi.TargetVersion(),
wsi.Note(in),
)
return err
}
// Given an Item ID, return the ID of the current PublishedVersion version (if any)
func (d *DatabaseMonitor) Published(in string) (*wyc.Version, error) {
ts := time.Now().UnixNano()
result, err := d.database.Published(in)
d.log(
err, ts,
wsi.IsPublished(), wsi.TargetVersion(),
wsi.Note(in),
)
return result, err
}
// Insert a collection into the db, return created Id
func (d *DatabaseMonitor) InsertCollection(in *wyc.Collection) (string, error) {
ts := time.Now().UnixNano()
result, err := d.database.InsertCollection(in)
d.log(
err, ts,
wsi.IsCreate(), wsi.TargetCollection(),
wsi.Note(in.Parent, in.Id, in.Name),
)
return result, err
}
// Insert an item into the db, return created Id
func (d *DatabaseMonitor) InsertItem(in *wyc.Item) (string, error) {
ts := time.Now().UnixNano()
result, err := d.database.InsertItem(in)
d.log(err, ts,
wsi.IsCreate(), wsi.TargetItem(),
wsi.Note(in.Parent, in.Id, in.ItemType, in.Variant),
)
return result, err
}
// Insert a Version into the db, return created Id
func (d *DatabaseMonitor) InsertNextVersion(in *wyc.Version) (string, int32, error) {
ts := time.Now().UnixNano()
resultId, resultVer, err := d.database.InsertNextVersion(in)
d.log(
err, ts,
wsi.IsCreate(), wsi.TargetVersion(),
wsi.Note(in.Parent, in.Id, in.Number),
)
return resultId, resultVer, err
}
// Insert resource into the db, return created Id
func (d *DatabaseMonitor) InsertResource(in *wyc.Resource) (string, error) {
ts := time.Now().UnixNano()
result, err := d.database.InsertResource(in)
d.log(err, ts, wsi.IsCreate(), wsi.TargetResource(), wsi.Note(in.Parent, in.Id, in.Name))
return result, err
}
// Insert link into the db, return created Id
func (d *DatabaseMonitor) InsertLink(in *wyc.Link) (string, error) {
ts := time.Now().UnixNano()
result, err := d.database.InsertLink(in)
d.log(err, ts, wsi.IsCreate(), wsi.TargetLink(), wsi.Note(in.Id, in.Src, in.Dst))
return result, err
}
// Retrieve collections indicated by the given Id(s) from the db
func (d *DatabaseMonitor) RetrieveCollection(in ...string) ([]*wyc.Collection, error) {
ts := time.Now().UnixNano()
results, err := d.database.RetrieveCollection(in...)
d.log(err, ts, wsi.IsFind(), wsi.TargetCollection(), wsi.Note(len(results)))
return results, err
}
// Retrieve items indicated by the given Id(s) from the db
func (d *DatabaseMonitor) RetrieveItem(in ...string) ([]*wyc.Item, error) {
ts := time.Now().UnixNano()
results, err := d.database.RetrieveItem(in...)
d.log(err, ts, wsi.IsFind(), wsi.TargetItem(), wsi.Note(len(results)))
return results, err
}
// Retrieve versions indicated by the given Id(s) from the db
func (d *DatabaseMonitor) RetrieveVersion(in ...string) ([]*wyc.Version, error) {
ts := time.Now().UnixNano()
results, err := d.database.RetrieveVersion(in...)
d.log(err, ts, wsi.IsFind(), wsi.TargetVersion(), wsi.Note(len(results)))
return results, err
}
// Retrieve resources indicated by the given Id(s) from the db
func (d *DatabaseMonitor) RetrieveResource(in ...string) ([]*wyc.Resource, error) {
ts := time.Now().UnixNano()
results, err := d.database.RetrieveResource(in...)
d.log(err, ts, wsi.IsFind(), wsi.TargetResource(), wsi.Note(len(results)))
return results, err
}
// Retrieve links indicated by the given Id(s) from the db
func (d *DatabaseMonitor) RetrieveLink(in ...string) ([]*wyc.Link, error) {
ts := time.Now().UnixNano()
results, err := d.database.RetrieveLink(in...)
d.log(err, ts, wsi.IsFind(), wsi.TargetLink(), wsi.Note(len(results)))
return results, err
}
// Save the updated facets on the given version
func (d *DatabaseMonitor) UpdateItem(id string, in *wyc.Item) error {
ts := time.Now().UnixNano()
err := d.database.UpdateItem(id, in)
d.log(err, ts, wsi.IsUpdate(), wsi.TargetItem(), wsi.Note(id))
return err
}
// Save the updated facets on the given item
func (d *DatabaseMonitor) UpdateVersion(id string, in *wyc.Version) error {
ts := time.Now().UnixNano()
err := d.database.UpdateVersion(id, in)
d.log(err, ts, wsi.IsUpdate(), wsi.TargetVersion(), wsi.Note(id))
return err
}
// Save the updated facets on the given collection
func (d *DatabaseMonitor) UpdateCollection(id string, in *wyc.Collection) error {
ts := time.Now().UnixNano()
err := d.database.UpdateCollection(id, in)
d.log(err, ts, wsi.IsUpdate(), wsi.TargetCollection(), wsi.Note(id))
return err
}
// Save the updated facets on the given resource
func (d *DatabaseMonitor) UpdateResource(id string, in *wyc.Resource) error {
ts := time.Now().UnixNano()
err := d.database.UpdateResource(id, in)
d.log(err, ts, wsi.IsUpdate(), wsi.TargetResource(), wsi.Note(id))
return err
}
// Save the updated facets on the given link
func (d *DatabaseMonitor) UpdateLink(id string, in *wyc.Link) error {
ts := time.Now().UnixNano()
err := d.database.UpdateLink(id, in)
d.log(err, ts, wsi.IsUpdate(), wsi.TargetLink(), wsi.Note(id))
return err
}
// Delete collection(s) with the given Id(s)
func (d *DatabaseMonitor) DeleteCollection(in ...string) error {
ts := time.Now().UnixNano()
err := d.database.DeleteCollection(in...)
d.log(err, ts, wsi.IsDelete(), wsi.TargetCollection(), wsi.Note(in))
return err
}
// Delete item(s) with the given Id(s)
func (d *DatabaseMonitor) DeleteItem(in ...string) error {
ts := time.Now().UnixNano()
err := d.database.DeleteItem(in...)
d.log(err, ts, wsi.IsDelete(), wsi.TargetItem(), wsi.Note(in))
return err
}
// Delete version(s) with the given Id(s)
func (d *DatabaseMonitor) DeleteVersion(in ...string) error {
ts := time.Now().UnixNano()
err := d.database.DeleteVersion(in...)
d.log(err, ts, wsi.IsDelete(), wsi.TargetVersion(), wsi.Note(in))
return err
}
// Delete resource(s) with the given Id(s)
func (d *DatabaseMonitor) DeleteResource(in ...string) error {
ts := time.Now().UnixNano()
err := d.database.DeleteResource(in...)
d.log(err, ts, wsi.IsDelete(), wsi.TargetResource(), wsi.Note(in))
return err
}
// Delete link(s) with the given Id(s)
func (d *DatabaseMonitor) DeleteLink(in ...string) error {
ts := time.Now().UnixNano()
err := d.database.DeleteLink(in...)
d.log(err, ts, wsi.IsDelete(), wsi.TargetLink(), wsi.Note(in))
return err
}
// kill connection to db
func (d *DatabaseMonitor) Close() error {
return d.database.Close()
}