-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
58 lines (49 loc) · 1.12 KB
/
utils.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
package main
import (
"github.com/nu7hatch/gouuid"
"fmt"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/net/context"
metadata "google.golang.org/grpc/metadata"
)
// TODO(labkode) set collation for table and column to utf8. The default is swedish
type record struct {
Path string
ETag string `redis:"etag"`
MTime uint32 `redis:"mtime"`
}
func (r *record) String() string {
return fmt.Sprintf("path=%s etag=%s mtime=%d",
r.Path, r.ETag, r.MTime)
}
func newGRPCTraceContext(ctx context.Context, trace string) context.Context {
md := metadata.Pairs("trace", trace)
ctx = metadata.NewContext(ctx, md)
return ctx
}
func getGRPCTraceID(ctx context.Context) (string, error) {
md, ok := metadata.FromContext(ctx)
if !ok {
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}
tokens := md["trace"]
if len(tokens) == 0 {
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}
if tokens[0] != "" {
return tokens[0], nil
}
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}