@@ -22,20 +22,23 @@ import (
22
22
"runtime"
23
23
"sort"
24
24
"strings"
25
-
26
- "go.opentelemetry.io/otel/propagation"
27
25
)
28
26
27
+ // Constants used as key string for tags.
28
+ // It is not necessary that all SQLCommenter frameworks/ORMs will contain all these keys i.e.
29
+ // it is on best-effort basis.
29
30
const (
30
31
Route string = "route"
31
- Controller string = "controller"
32
- Action string = "action"
33
- Framework string = "framework"
34
- Driver string = "db_driver"
35
- Traceparent string = "traceparent"
36
- Application string = "application"
32
+ Controller = "controller"
33
+ Action = "action"
34
+ Framework = "framework"
35
+ Driver = "db_driver"
36
+ Traceparent = "traceparent"
37
+ Application = "application"
37
38
)
38
39
40
+ // CommenterConfig contains configurations for SQLCommenter library.
41
+ // We can enable and disable certain tags by enabling these configurations.
39
42
type CommenterConfig struct {
40
43
EnableDBDriver bool
41
44
EnableRoute bool
@@ -46,11 +49,15 @@ type CommenterConfig struct {
46
49
EnableApplication bool
47
50
}
48
51
52
+ // StaticTags are few tags that can be set by the application and will be constant
53
+ // for every API call.
49
54
type StaticTags struct {
50
55
Application string
51
56
DriverName string
52
57
}
53
58
59
+ // CommenterOptions contains all options regarding SQLCommenter library.
60
+ // This includes the configurations as well as any static tags.
54
61
type CommenterOptions struct {
55
62
Config CommenterConfig
56
63
Tags StaticTags
@@ -60,13 +67,19 @@ func encodeURL(k string) string {
60
67
return url .QueryEscape (k )
61
68
}
62
69
63
- func GetFunctionName (i interface {}) string {
70
+ // GetFunctionName returns the name of the function passed.
71
+ func GetFunctionName (i any ) string {
64
72
if i == nil {
65
73
return ""
66
74
}
67
75
return runtime .FuncForPC (reflect .ValueOf (i ).Pointer ()).Name ()
68
76
}
69
77
78
+ // ConvertMapToComment returns a comment string given a map of key-value pairs of tags.
79
+ // There are few steps involved here:
80
+ // - Sorting the tags by key string
81
+ // - url encoding the key value pairs
82
+ // - Formatting the key value pairs as "key1=value1,key2=value2" format.
70
83
func ConvertMapToComment (tags map [string ]string ) string {
71
84
var sb strings.Builder
72
85
i , sz := 0 , len (tags )
@@ -89,6 +102,7 @@ func ConvertMapToComment(tags map[string]string) string {
89
102
return sb .String ()
90
103
}
91
104
105
+ // ExtractTraceparent extracts the traceparent field using OpenTelemetry library.
92
106
func ExtractTraceparent (ctx context.Context ) propagation.MapCarrier {
93
107
// Serialize the context into carrier
94
108
textMapPropogator := propagation .NewCompositeTextMapPropagator (propagation.TraceContext {}, propagation.Baggage {})
@@ -97,12 +111,15 @@ func ExtractTraceparent(ctx context.Context) propagation.MapCarrier {
97
111
return carrier
98
112
}
99
113
114
+ // RequestTagsProvider adds a basic interface for other libraries like gorilla/mux to implement.
100
115
type RequestTagsProvider interface {
101
116
Route () string
102
117
Action () string
103
118
Framework () string
104
119
}
105
120
121
+ // ContextInject injects the tags key-value pairs into context,
122
+ // which can be later passed into drivers/ORMs to finally inject them into SQL queries.
106
123
func ContextInject (ctx context.Context , h RequestTagsProvider ) context.Context {
107
124
ctx = context .WithValue (ctx , Route , h .Route ())
108
125
ctx = context .WithValue (ctx , Action , h .Action ())
0 commit comments