Skip to content

Commit 5fb30e3

Browse files
authored
[doc] add instrumentations and minor improvement (#52)
* document instrumentation and some adjustment to instrumentation ops naming * rename doc tabs. main.go and main_test.go now Example and Mock respectively * fix import order * added missing test * update badges
1 parent 5b7a436 commit 5fb30e3

File tree

14 files changed

+177
-81
lines changed

14 files changed

+177
-81
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# REL
2-
[![GoDoc](https://godoc.org/github.com/Fs02/rel?status.svg)](https://godoc.org/github.com/Fs02/rel) [![Build Status](https://travis-ci.com/Fs02/rel.svg?branch=master)](https://travis-ci.com/Fs02/rel) [![Go Report Card](https://goreportcard.com/badge/github.com/Fs02/rel)](https://goreportcard.com/report/github.com/Fs02/rel) [![Maintainability](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/maintainability)](https://codeclimate.com/github/Fs02/rel/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/d487e2be0ed7b0b1fed1/test_coverage)](https://codeclimate.com/github/Fs02/rel/test_coverage)
2+
3+
[![GoDoc](https://godoc.org/github.com/Fs02/rel?status.svg)](https://godoc.org/github.com/Fs02/rel)
4+
[![Build Status](https://travis-ci.com/Fs02/rel.svg?branch=master)](https://travis-ci.com/Fs02/rel)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/Fs02/rel)](https://goreportcard.com/report/github.com/Fs02/rel)
6+
[![Maintainability](https://api.codeclimate.com/v1/badges/194611cc82f02edcda6e/maintainability)](https://codeclimate.com/github/Fs02/rel/maintainability)
7+
[![Test Coverage](https://api.codeclimate.com/v1/badges/194611cc82f02edcda6e/test_coverage)](https://codeclimate.com/github/Fs02/rel/test_coverage)
38

49
> Golang SQL Database Layer for Layered Architecture.
510

adapter/postgres/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (adapter *Adapter) query(ctx context.Context, statement string, args []inte
8989
rows *db.Rows
9090
)
9191

92-
finish := adapter.Instrument(ctx, "query", statement)
92+
finish := adapter.Instrument(ctx, "adapter-query", statement)
9393
if adapter.Tx != nil {
9494
rows, err = adapter.Tx.QueryContext(ctx, statement, args...)
9595
} else {

adapter/sql/adapter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (adapter *Adapter) Aggregate(ctx context.Context, query rel.Query, mode str
6363
statement, args = NewBuilder(adapter.Config).Aggregate(query, mode, field)
6464
)
6565

66-
finish := adapter.Instrument(ctx, "aggregate", statement)
66+
finish := adapter.Instrument(ctx, "adapter-aggregate", statement)
6767
if adapter.Tx != nil {
6868
err = adapter.Tx.QueryRowContext(ctx, statement, args...).Scan(&out)
6969
} else {
@@ -80,7 +80,7 @@ func (adapter *Adapter) Query(ctx context.Context, query rel.Query) (rel.Cursor,
8080
statement, args = NewBuilder(adapter.Config).Find(query)
8181
)
8282

83-
finish := adapter.Instrument(ctx, "query", statement)
83+
finish := adapter.Instrument(ctx, "adapter-query", statement)
8484
rows, err := adapter.query(ctx, statement, args)
8585
finish(err)
8686

@@ -97,7 +97,7 @@ func (adapter *Adapter) query(ctx context.Context, statement string, args []inte
9797

9898
// Exec performs exec operation.
9999
func (adapter *Adapter) Exec(ctx context.Context, statement string, args []interface{}) (int64, int64, error) {
100-
finish := adapter.Instrument(ctx, "exec", statement)
100+
finish := adapter.Instrument(ctx, "adapter-exec", statement)
101101
res, err := adapter.exec(ctx, statement, args)
102102
finish(err)
103103

@@ -186,7 +186,7 @@ func (adapter *Adapter) Begin(ctx context.Context) (rel.Adapter, error) {
186186
err error
187187
)
188188

189-
finish := adapter.Instrument(ctx, "transaction", "begin transaction")
189+
finish := adapter.Instrument(ctx, "adapter-begin", "begin transaction")
190190

191191
if adapter.Tx != nil {
192192
tx = adapter.Tx
@@ -209,7 +209,7 @@ func (adapter *Adapter) Begin(ctx context.Context) (rel.Adapter, error) {
209209
func (adapter *Adapter) Commit(ctx context.Context) error {
210210
var err error
211211

212-
finish := adapter.Instrument(ctx, "transaction", "commit transaction")
212+
finish := adapter.Instrument(ctx, "adapter-commit", "commit transaction")
213213

214214
if adapter.Tx == nil {
215215
err = errors.New("unable to commit outside transaction")
@@ -228,7 +228,7 @@ func (adapter *Adapter) Commit(ctx context.Context) error {
228228
func (adapter *Adapter) Rollback(ctx context.Context) error {
229229
var err error
230230

231-
finish := adapter.Instrument(ctx, "transaction", "rollback transaction")
231+
finish := adapter.Instrument(ctx, "adapter-rollback", "rollback transaction")
232232

233233
if adapter.Tx == nil {
234234
err = errors.New("unable to rollback outside transaction")

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
* [Modifying Association](association.md#modifying-association)
3333

3434
* [Transactions](transactions.md)
35+
* [Instrumentation](instrumentation.md)
3536
* [Adapters](adapters.md)
3637
* [Github](https://github.com/Fs02/rel)

docs/association.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Preload will load association to structs. To preload association, use `Preload`.
5353

5454
<!-- tabs:start -->
5555

56-
### **main.go**
56+
### **Example**
5757

5858
```go
5959
// preload transaction `belongs to` buyer association.
@@ -73,7 +73,7 @@ repo.Preload(ctx, &user, "transactions", where.Eq("paid", true))
7373
repo.Preload(ctx, &transactions, "buyer.address")
7474
```
7575

76-
### **main_test.go**
76+
### **Mock**
7777

7878
```go
7979
// preload transaction `belongs to` buyer association.
@@ -103,7 +103,7 @@ REL will try to create a new record for association if `ID` is a zero value.
103103

104104
<!-- tabs:start -->
105105

106-
### **main.go**
106+
### **Example**
107107

108108
```go
109109
user := User{
@@ -118,7 +118,7 @@ user := User{
118118
repo.Insert(ctx, &user)
119119
```
120120

121-
### **main_tesst.go**
121+
### **Mock**
122122

123123
```go
124124
repo.ExpectInsert().For(&user)
@@ -130,7 +130,7 @@ REL will try to update a new record for association if `ID` is a zero value. To
130130

131131
<!-- tabs:start -->
132132

133-
### **main.go**
133+
### **Example**
134134

135135
```go
136136
userId := 1
@@ -150,7 +150,7 @@ user := User{
150150
repo.Update(ctx, &user)
151151
```
152152

153-
### **main_tesst.go**
153+
### **Mock**
154154

155155
```go
156156
repo.ExpectUpdate().For(&user)
@@ -162,7 +162,7 @@ To selectively update only specific fields or association, `use rel.Map`.
162162

163163
<!-- tabs:start -->
164164

165-
### **main.go**
165+
### **Example**
166166

167167
```go
168168
modification := rel.Map{
@@ -175,7 +175,7 @@ modification := rel.Map{
175175
repo.Update(ctx, &user, modification)
176176
```
177177

178-
### **main_test.go**
178+
### **Mock**
179179

180180
```go
181181
modification := rel.Map{

docs/crud.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A new record can be inserted to database using a struct, map or set function. To
66

77
<!-- tabs:start -->
88

9-
### **main.go**
9+
### **Example**
1010

1111
```go
1212
book := Book{
@@ -20,7 +20,7 @@ if err := repo.Insert(ctx, &book); err != nil {
2020
}
2121
```
2222

23-
### **main_test.go**
23+
### **Mock**
2424

2525
> reltest.Repository will automatically sets any primary key value to be 1.
2626
@@ -50,7 +50,7 @@ To insert a new record using a map, simply pass a `rel.Map` as the second argume
5050

5151
<!-- tabs:start -->
5252

53-
### **main.go**
53+
### **Example**
5454

5555
```go
5656
var book Book
@@ -63,7 +63,7 @@ data := rel.Map{
6363
repo.Insert(ctx, &book, data)
6464
```
6565

66-
### **main_test.go**
66+
### **Mock**
6767

6868
> reltest.Repository will automatically populate record using value provided by map.
6969
@@ -81,14 +81,14 @@ It's also possible to insert a new record manually using `rel.Set`, which is a v
8181

8282
<!-- tabs:start -->
8383

84-
### **main.go**
84+
### **Example**
8585

8686
```go
8787
// Insert using set.
8888
repo.Insert(ctx, &book, rel.Set("title", "Rel for dummies"), rel.Set("category", "education"))
8989
```
9090

91-
### **main_test.go**
91+
### **Mock**
9292

9393
```go
9494
// Expect insertion with given modifier.
@@ -105,14 +105,14 @@ To inserts multiple records at once, use `InsertAll`.
105105

106106
<!-- tabs:start -->
107107

108-
### **main.go**
108+
### **Example**
109109

110110
```go
111111
// InsertAll books.
112112
repo.InsertAll(ctx, &books)
113113
```
114114

115-
### **main_test.go**
115+
### **Mock**
116116

117117
```go
118118
// Expect any insert all.
@@ -129,16 +129,17 @@ REL provides a powerful API for querying record from database. To query a single
129129

130130
<!-- tabs:start -->
131131

132-
### **main.go**
132+
### **Example**
133133

134134
```go
135+
// Retrieve a book with id 1
135136
repo.Find(ctx, &book, rel.Eq("id", 1))
136137

137138
// OR: with sugar alias
138139
repo.Find(ctx, &book, where.Eq("id", 1))
139140
```
140141

141-
### **main_test.go**
142+
### **Mock**
142143

143144
```go
144145
// Expect a find query and mock the result.
@@ -155,13 +156,13 @@ To query multiple records, use `FindAll` method.
155156

156157
<!-- tabs:start -->
157158

158-
### **main.go**
159+
### **Example**
159160

160161
```go
161162
repo.FindAll(ctx, &books, where.Like("title", "%dummies%").AndEq("category", "education"), rel.Limit(10))
162163
```
163164

164-
### **main_test.go**
165+
### **Mock**
165166

166167
```go
167168
// Expect a find all query and mock the result.
@@ -175,14 +176,14 @@ REL also support chainable query api for a more complex query use case.
175176

176177
<!-- tabs:start -->
177178

178-
### **main.go**
179+
### **Example**
179180

180181
```go
181182
query := rel.Select("title", "category").Where(where.Eq("category", "education")).SortAsc("title")
182183
repo.FindAll(ctx, &books, query)
183184
```
184185

185-
### **main_test.go**
186+
### **Mock**
186187

187188
```go
188189
// Expect a find all query and mock the result.
@@ -200,14 +201,14 @@ Similar to create, updating a record in REL can also be done using struct, map o
200201
201202
<!-- tabs:start -->
202203

203-
### **main.go**
204+
### **Example**
204205

205206
```go
206207
// Update directly using struct.
207208
repo.Update(ctx, &book)
208209
```
209210

210-
### **main_test.go**
211+
### **Mock**
211212

212213
```go
213214
// Expect any update is called.
@@ -220,14 +221,14 @@ Besides struct, map and set function. There's also increment and decrement modif
220221

221222
<!-- tabs:start -->
222223

223-
### **main.go**
224+
### **Example**
224225

225226
```go
226227
// Update directly using struct.
227228
repo.Update(ctx, &book, rel.Inc("views"))
228229
```
229230

230-
### **main_test.go**
231+
### **Mock**
231232

232233
```go
233234
// Expect any update is called.
@@ -244,14 +245,14 @@ To delete a record in rel, simply pass the record to be deleted.
244245
245246
<!-- tabs:start -->
246247

247-
### **main.go**
248+
### **Example**
248249

249250
```go
250251
// Delete a record.
251252
repo.Delete(ctx, &book)
252253
```
253254

254-
### **main_test.go**
255+
### **Mock**
255256

256257
```go
257258
// Expect book to be deleted.
@@ -265,14 +266,14 @@ Deleting multiple records is possible using `DeleteAll`.
265266

266267
<!-- tabs:start -->
267268

268-
### **main.go**
269+
### **Example**
269270

270271
```go
271272
// We have manually define the table here.
272273
repo.DeleteAll(ctx, rel.From("books").Where(where.Eq("id", 1)))
273274
```
274275

275-
### **main_test.go**
276+
### **Mock**
276277

277278
```go
278279
// Expect books to be deleted.

docs/instrumentation.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"time"
7+
8+
"github.com/Fs02/rel"
9+
)
10+
11+
// Instrumentation docs example.
12+
func Instrumentation(ctx context.Context, repo rel.Repository) {
13+
/// [instrumentation]
14+
repo.Instrumentation(func(ctx context.Context, op string, message string) func(err error) {
15+
t := time.Now()
16+
17+
return func(err error) {
18+
duration := time.Since(t)
19+
log.Print("[duration: ", duration, " op: ", op, "] ", message, " - ", err)
20+
}
21+
})
22+
/// [instrumentation]
23+
}

0 commit comments

Comments
 (0)