Skip to content

Commit a35d545

Browse files
authored
Added go core implementation (#163)
1 parent 89887ed commit a35d545

File tree

5 files changed

+149
-4
lines changed

5 files changed

+149
-4
lines changed

.github/workflows/go-sql-tests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
name: go-sql package test
1+
name: go packages test
22

33
on:
44
push:
55
branches:
66
- master
77
paths:
8-
- go/go-sql/**
8+
- go/**
99
pull_request:
1010
paths:
11-
- go/go-sql/**
11+
- go/**
1212

1313
jobs:
1414
unittests:
@@ -25,7 +25,7 @@ jobs:
2525
run: go build -v ./...
2626
working-directory: ./go/go-sql
2727

28-
- name: Test
28+
- name: Test go-sql
2929
run: go test -v ./...
3030
working-directory: ./go/go-sql
3131

go/core/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SQLCommenter Core [In development]
2+
3+
SQLcommenter is a plugin/middleware/wrapper to augment application related information/tags with SQL Statements that can be used later to correlate user code with SQL statements.
4+
5+
This package contains configuration options, framework interface and support functions for all the sqlcommenter go modules
6+
7+
## Installation
8+
9+
This is a support package and will be installed indirectly by other go sqlcommenter packages
10+
11+
## Usages
12+
13+
### Configuration
14+
15+
Users are given control over what tags they want to append by using `core.CommenterOptions` struct.
16+
17+
```go
18+
type CommenterOptions struct {
19+
EnableDBDriver bool
20+
EnableTraceparent bool // OpenTelemetry trace information
21+
EnableRoute bool // applicable for web frameworks
22+
EnableFramework bool // applicable for web frameworks
23+
EnableController bool // applicable for web frameworks
24+
EnableAction bool // applicable for web frameworks
25+
}
26+
```
27+
28+

go/core/core.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package core
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"net/url"
21+
"reflect"
22+
"runtime"
23+
"sort"
24+
"strings"
25+
26+
"go.opentelemetry.io/otel/propagation"
27+
)
28+
29+
const (
30+
Route string = "route"
31+
Controller string = "controller"
32+
Action string = "action"
33+
Framework string = "framework"
34+
Driver string = "driver"
35+
Traceparent string = "traceparent"
36+
)
37+
38+
type CommenterOptions struct {
39+
EnableDBDriver bool
40+
EnableRoute bool
41+
EnableFramework bool
42+
EnableController bool
43+
EnableAction bool
44+
EnableTraceparent bool
45+
}
46+
47+
func encodeURL(k string) string {
48+
return url.QueryEscape(string(k))
49+
}
50+
51+
func GetFunctionName(i interface{}) string {
52+
if i == nil {
53+
return ""
54+
}
55+
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
56+
}
57+
58+
func ConvertMapToComment(tags map[string]string) string {
59+
var sb strings.Builder
60+
i, sz := 0, len(tags)
61+
62+
//sort by keys
63+
sortedKeys := make([]string, 0, len(tags))
64+
for k := range tags {
65+
sortedKeys = append(sortedKeys, k)
66+
}
67+
sort.Strings(sortedKeys)
68+
69+
for _, key := range sortedKeys {
70+
if i == sz-1 {
71+
sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(tags[key])))
72+
} else {
73+
sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(tags[key])))
74+
}
75+
i++
76+
}
77+
return sb.String()
78+
}
79+
80+
func ExtractTraceparent(ctx context.Context) propagation.MapCarrier {
81+
// Serialize the context into carrier
82+
textMapPropogator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
83+
carrier := propagation.MapCarrier{}
84+
textMapPropogator.Inject(ctx, carrier)
85+
return carrier
86+
}
87+
88+
type RequestExtractor interface {
89+
Route() string
90+
Action() string
91+
Framework() string
92+
}
93+
94+
func ContextInject(ctx context.Context, h RequestExtractor) context.Context {
95+
ctx = context.WithValue(ctx, Route, h.Route())
96+
ctx = context.WithValue(ctx, Action, h.Action())
97+
ctx = context.WithValue(ctx, Framework, h.Framework())
98+
return ctx
99+
}

go/core/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/google/sqlcommenter/go/core
2+
3+
go 1.19
4+
5+
require go.opentelemetry.io/otel v1.10.0
6+
7+
require go.opentelemetry.io/otel/trace v1.10.0 // indirect

go/core/go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
3+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
4+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
7+
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
8+
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
9+
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
10+
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

0 commit comments

Comments
 (0)