@@ -22,20 +22,23 @@ import (
2222 "runtime"
2323 "sort"
2424 "strings"
25-
26- "go.opentelemetry.io/otel/propagation"
2725)
2826
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.
2930const (
3031 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"
3738)
3839
40+ // CommenterConfig contains configurations for SQLCommenter library.
41+ // We can enable and disable certain tags by enabling these configurations.
3942type CommenterConfig struct {
4043 EnableDBDriver bool
4144 EnableRoute bool
@@ -46,11 +49,15 @@ type CommenterConfig struct {
4649 EnableApplication bool
4750}
4851
52+ // StaticTags are few tags that can be set by the application and will be constant
53+ // for every API call.
4954type StaticTags struct {
5055 Application string
5156 DriverName string
5257}
5358
59+ // CommenterOptions contains all options regarding SQLCommenter library.
60+ // This includes the configurations as well as any static tags.
5461type CommenterOptions struct {
5562 Config CommenterConfig
5663 Tags StaticTags
@@ -60,13 +67,19 @@ func encodeURL(k string) string {
6067 return url .QueryEscape (k )
6168}
6269
63- func GetFunctionName (i interface {}) string {
70+ // GetFunctionName returns the name of the function passed.
71+ func GetFunctionName (i any ) string {
6472 if i == nil {
6573 return ""
6674 }
6775 return runtime .FuncForPC (reflect .ValueOf (i ).Pointer ()).Name ()
6876}
6977
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.
7083func ConvertMapToComment (tags map [string ]string ) string {
7184 var sb strings.Builder
7285 i , sz := 0 , len (tags )
@@ -89,6 +102,7 @@ func ConvertMapToComment(tags map[string]string) string {
89102 return sb .String ()
90103}
91104
105+ // ExtractTraceparent extracts the traceparent field using OpenTelemetry library.
92106func ExtractTraceparent (ctx context.Context ) propagation.MapCarrier {
93107 // Serialize the context into carrier
94108 textMapPropogator := propagation .NewCompositeTextMapPropagator (propagation.TraceContext {}, propagation.Baggage {})
@@ -97,12 +111,15 @@ func ExtractTraceparent(ctx context.Context) propagation.MapCarrier {
97111 return carrier
98112}
99113
114+ // RequestTagsProvider adds a basic interface for other libraries like gorilla/mux to implement.
100115type RequestTagsProvider interface {
101116 Route () string
102117 Action () string
103118 Framework () string
104119}
105120
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.
106123func ContextInject (ctx context.Context , h RequestTagsProvider ) context.Context {
107124 ctx = context .WithValue (ctx , Route , h .Route ())
108125 ctx = context .WithValue (ctx , Action , h .Action ())
0 commit comments