...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package trace
16
17 import (
18 "context"
19
20 "go.opencensus.io/trace"
21 "google.golang.org/api/googleapi"
22 "google.golang.org/genproto/googleapis/rpc/code"
23 "google.golang.org/grpc/status"
24 )
25
26
27 type EndSpanFunc func(error)
28
29
30 type Attribute struct {
31 key string
32 value interface{}
33 }
34
35 func (a Attribute) traceAttr() trace.Attribute {
36
37
38 return trace.StringAttribute(a.key, a.value.(string))
39 }
40
41
42 func AddInstanceName(name string) Attribute {
43 return Attribute{key: "/cloudsql/instance", value: name}
44 }
45
46
47 func AddDialerID(dialerID string) Attribute {
48 return Attribute{key: "/cloudsql/dialer_id", value: dialerID}
49 }
50
51
52
53 func StartSpan(ctx context.Context, name string, attrs ...Attribute) (context.Context, EndSpanFunc) {
54 var span *trace.Span
55 ctx, span = trace.StartSpan(ctx, name)
56 as := make([]trace.Attribute, 0, len(attrs))
57 for _, a := range attrs {
58 as = append(as, a.traceAttr())
59 }
60 span.AddAttributes(as...)
61 return ctx, func(err error) {
62 if err != nil {
63 span.SetStatus(toStatus(err))
64 }
65 span.End()
66 }
67 }
68
69
70
71
72
73 func toStatus(err error) trace.Status {
74 if err2, ok := err.(*googleapi.Error); ok {
75 return trace.Status{Code: httpStatusCodeToOCCode(err2.Code), Message: err2.Message}
76 }
77 if s, ok := status.FromError(err); ok {
78 return trace.Status{Code: int32(s.Code()), Message: s.Message()}
79 }
80 return trace.Status{Code: int32(code.Code_UNKNOWN), Message: err.Error()}
81 }
82
83
84 func httpStatusCodeToOCCode(httpStatusCode int) int32 {
85 switch httpStatusCode {
86 case 200:
87 return int32(code.Code_OK)
88 case 499:
89 return int32(code.Code_CANCELLED)
90 case 500:
91 return int32(code.Code_UNKNOWN)
92 case 400:
93 return int32(code.Code_INVALID_ARGUMENT)
94 case 504:
95 return int32(code.Code_DEADLINE_EXCEEDED)
96 case 404:
97 return int32(code.Code_NOT_FOUND)
98 case 409:
99 return int32(code.Code_ALREADY_EXISTS)
100 case 403:
101 return int32(code.Code_PERMISSION_DENIED)
102 case 401:
103 return int32(code.Code_UNAUTHENTICATED)
104 case 429:
105 return int32(code.Code_RESOURCE_EXHAUSTED)
106 case 501:
107 return int32(code.Code_UNIMPLEMENTED)
108 case 503:
109 return int32(code.Code_UNAVAILABLE)
110 default:
111 return int32(code.Code_UNKNOWN)
112 }
113 }
114
View as plain text