/
handlers.go
129 lines (109 loc) · 3.11 KB
/
handlers.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
package database
import (
"context"
"database/sql"
"github.com/opentracing/opentracing-go"
)
func GetProducts(db *sql.DB, start, count int, ctx context.Context) ([]*Product, error) {
parentSpan := opentracing.SpanFromContext(ctx)
var parentCtx opentracing.SpanContext
if parentSpan != nil {
parentCtx = parentSpan.Context()
}
span := opentracing.StartSpan(
"get-products-db",
opentracing.ChildOf(parentCtx),
)
defer span.Finish()
span.SetTag("query", getProductsQuery)
span.SetTag("count", count)
span.SetTag("start", start)
span.LogKV(
"query", getProductsQuery,
"count", count,
"start", start,
)
rows, err := db.QueryContext(ctx, getProductsQuery, count, start)
if err != nil {
return nil, err
}
defer rows.Close()
products := make([]*Product, 0)
for rows.Next() {
var prod Product
if err := rows.Scan(&prod.ID, &prod.Name, &prod.Price); err != nil {
return nil, err
}
products = append(products, &prod)
}
span.SetTag("products-found", len(products))
span.LogKV("products-found", len(products))
return products, nil
}
func GetProduct(db *sql.DB, product *Product, ctx context.Context) error {
parentSpan := opentracing.SpanFromContext(ctx)
var parentCtx opentracing.SpanContext
if parentSpan != nil {
parentCtx = parentSpan.Context()
}
span := opentracing.StartSpan(
"get-product-db",
opentracing.ChildOf(parentCtx),
)
defer span.Finish()
span.SetTag("product-id", product.ID)
span.LogKV("product-id", product.ID)
return db.QueryRowContext(ctx, getProductQuery, product.ID).
Scan(&product.Name, &product.Price)
}
func CreateProduct(db *sql.DB, product *Product, ctx context.Context) error {
parentSpan := opentracing.SpanFromContext(ctx)
var parentCtx opentracing.SpanContext
if parentSpan != nil {
parentCtx = parentSpan.Context()
}
span := opentracing.StartSpan(
"create-product-db",
opentracing.ChildOf(parentCtx),
)
defer span.Finish()
span.SetTag("product", product.String())
span.LogKV("product", product.String())
err := db.QueryRowContext(ctx, createProductQuery, product.Name, product.Price).Scan(&product.ID)
if err != nil {
return err
}
return nil
}
func UpdateProduct(db *sql.DB, product *Product, ctx context.Context) error {
parentSpan := opentracing.SpanFromContext(ctx)
var parentCtx opentracing.SpanContext
if parentSpan != nil {
parentCtx = parentSpan.Context()
}
span := opentracing.StartSpan(
"update-product-db",
opentracing.ChildOf(parentCtx),
)
defer span.Finish()
span.SetTag("product", product.String())
span.LogKV("product", product.String())
_, err := db.ExecContext(ctx, updateProductQuery, product.Name, product.Price, product.ID)
return err
}
func DeleteProduct(db *sql.DB, productId int, ctx context.Context) error {
parentSpan := opentracing.SpanFromContext(ctx)
var parentCtx opentracing.SpanContext
if parentSpan != nil {
parentCtx = parentSpan.Context()
}
span := opentracing.StartSpan(
"delete-product-db",
opentracing.ChildOf(parentCtx),
)
defer span.Finish()
span.SetTag("product-id", productId)
span.LogKV("product-id", productId)
_, err := db.ExecContext(ctx, deleteProductQuery, productId)
return err
}