Skip to content

Commit 33d65cb

Browse files
authored
Merge branch 'master' into python-publish-workflow
2 parents b3a27bc + 36ce7d3 commit 33d65cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1958
-24
lines changed

Diff for: .github/workflows/gh-pages.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: github pages
2+
3+
on:
4+
push:
5+
branches:
6+
- gh-pages # Set a branch that will trigger a deployment
7+
pull_request:
8+
branches:
9+
- gh-pages
10+
workflow_dispatch:
11+
12+
jobs:
13+
deploy:
14+
runs-on: ubuntu-22.04
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
ref: gh-pages
19+
submodules: true # Fetch Hugo themes (true OR recursive)
20+
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
21+
22+
- name: Setup Hugo
23+
uses: peaceiris/actions-hugo@v2
24+
with:
25+
hugo-version: '0.79.1'
26+
# extended: true
27+
28+
- name: Build
29+
run: cd src && hugo --minify && cd ..
30+
31+
- name: Deploy
32+
uses: peaceiris/actions-gh-pages@v3
33+
if: github.ref == 'refs/heads/gh-pages'
34+
with:
35+
github_token: ${{ secrets.GITHUB_TOKEN }}
36+
publish_dir: .

Diff for: .github/workflows/go-sql-tests.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: go packages test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- go/**
9+
pull_request:
10+
paths:
11+
- go/**
12+
13+
jobs:
14+
unittests:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v3
21+
with:
22+
go-version: 1.19
23+
24+
- name: Build
25+
run: go build -v ./...
26+
working-directory: ./go/database/sql
27+
28+
- name: Test go-sql
29+
run: go test -v ./...
30+
working-directory: ./go/database/sql
31+
32+
gofmt:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v3
36+
37+
- name: Set up Go
38+
uses: actions/setup-go@v3
39+
with:
40+
go-version: 1.19
41+
42+
# Verify go fmt standards are used
43+
- name: Format
44+
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
45+

Diff for: .github/workflows/unit-tests.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ on:
99
- php/sqlcommenter-php/samples/sqlcommenter-laravel/**
1010
- python/sqlcommenter-python/**
1111
- nodejs/**
12+
- go/**
1213
pull_request:
1314
paths-ignore:
1415
- php/sqlcommenter-php/packages/sqlcommenter-laravel/**
1516
- php/sqlcommenter-php/samples/sqlcommenter-laravel/**
1617
- python/sqlcommenter-python/**
1718
- nodejs/**
19+
- go/**
1820

1921
jobs:
2022
unittests:

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
php/sqlcommenter-php/packages/sqlcommenter-laravel/vendor/*
2-
.idea/**
2+
.idea/**
3+
.DS_Store

Diff for: go/core/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.CommenterConfig` struct.
16+
17+
```go
18+
type CommenterConfig struct {
19+
EnableDBDriver bool
20+
EnableRoute bool
21+
EnableFramework bool
22+
EnableController bool
23+
EnableAction bool
24+
EnableTraceparent bool
25+
EnableApplication bool
26+
}
27+
```
28+
29+
Users can also set the values for some static tags by using `core.StaticTags` struct.
30+
31+
```go
32+
type StaticTags struct {
33+
Application string
34+
DriverName string
35+
}
36+
```
37+
38+
These two structs together form the `core.CommenterOptions` struct, which is used by [sqlcommenter/go/database/sql](../database/sql/README.md).
39+
40+
```go
41+
type CommenterOptions struct {
42+
Config CommenterConfig
43+
Tags StaticTags
44+
}
45+
```

Diff for: go/core/core.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 = "db_driver"
35+
Traceparent string = "traceparent"
36+
Application string = "application"
37+
)
38+
39+
type CommenterConfig struct {
40+
EnableDBDriver bool
41+
EnableRoute bool
42+
EnableFramework bool
43+
EnableController bool
44+
EnableAction bool
45+
EnableTraceparent bool
46+
EnableApplication bool
47+
}
48+
49+
type StaticTags struct {
50+
Application string
51+
DriverName string
52+
}
53+
54+
type CommenterOptions struct {
55+
Config CommenterConfig
56+
Tags StaticTags
57+
}
58+
59+
func encodeURL(k string) string {
60+
return url.QueryEscape(string(k))
61+
}
62+
63+
func GetFunctionName(i interface{}) string {
64+
if i == nil {
65+
return ""
66+
}
67+
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
68+
}
69+
70+
func ConvertMapToComment(tags map[string]string) string {
71+
var sb strings.Builder
72+
i, sz := 0, len(tags)
73+
74+
//sort by keys
75+
sortedKeys := make([]string, 0, len(tags))
76+
for k := range tags {
77+
sortedKeys = append(sortedKeys, k)
78+
}
79+
sort.Strings(sortedKeys)
80+
81+
for _, key := range sortedKeys {
82+
if i == sz-1 {
83+
sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(tags[key])))
84+
} else {
85+
sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(tags[key])))
86+
}
87+
i++
88+
}
89+
return sb.String()
90+
}
91+
92+
func ExtractTraceparent(ctx context.Context) propagation.MapCarrier {
93+
// Serialize the context into carrier
94+
textMapPropogator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
95+
carrier := propagation.MapCarrier{}
96+
textMapPropogator.Inject(ctx, carrier)
97+
return carrier
98+
}
99+
100+
type RequestTagsProvider interface {
101+
Route() string
102+
Action() string
103+
Framework() string
104+
}
105+
106+
func ContextInject(ctx context.Context, h RequestTagsProvider) context.Context {
107+
ctx = context.WithValue(ctx, Route, h.Route())
108+
ctx = context.WithValue(ctx, Action, h.Action())
109+
ctx = context.WithValue(ctx, Framework, h.Framework())
110+
return ctx
111+
}

Diff for: go/core/go.mod

+7
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.11.1
6+
7+
require go.opentelemetry.io/otel/trace v1.11.1 // indirect

Diff for: go/core/go.sum

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
10+
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
11+
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
12+
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
13+
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
14+
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

Diff for: go/database/sql/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# go-sql-driver
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+
## Installation
6+
7+
```shell
8+
go get -u github.com/google/sqlcommenter/go/database/sql
9+
```
10+
11+
## Usage
12+
13+
Please use the sqlcommenter's default go-sql database driver to execute statements.
14+
Due to inherent nature of Go, the safer way to pass information from framework to database driver is via `context`. So, it is recommended to use the context based methods of `DB` interface like `QueryContext`, `ExecContext` and `PrepareContext`.
15+
16+
```go
17+
import (
18+
gosql "github.com/google/sqlcommenter/go/database/sql"
19+
sqlcommentercore "github.com/google/sqlcommenter/go/core"
20+
_ "github.com/lib/pq" // or any other database driver
21+
)
22+
23+
db, err := gosql.Open("<driver>", "<connectionString>", sqlcommentercore.CommenterOptions{
24+
Config: sqlcommentercore.CommenterConfig{<flag>:bool}
25+
Tags : sqlcommentercore.StaticTags{<tag>: string} // optional
26+
})
27+
```
28+
29+
### Configuration
30+
31+
Users are given control over what tags they want to append by using `core.CommenterOptions` struct.
32+
33+
```go
34+
type CommenterOptions struct {
35+
EnableDBDriver bool
36+
EnableTraceparent bool // OpenTelemetry trace information
37+
EnableRoute bool // applicable for web frameworks
38+
EnableFramework bool // applicable for web frameworks
39+
EnableController bool // applicable for web frameworks
40+
EnableAction bool // applicable for web frameworks
41+
EnableApplication bool // applicable for web frameworks
42+
}
43+
```
44+
45+
Users can also provide static tags they want to use by using `core.StaticTags` struct. These tags are optional. If not provided, the `Application` tag will get auto-populated from the user-project's module-name in `go.mod`. `DriverName` is set to the driver-name provided in `gosql.Open(driverName, ...)` call.
46+
47+
```go
48+
type StaticTags struct {
49+
Application string
50+
DriverName string
51+
}
52+
```
53+
54+
The driver will try to use the module-name from the project's `go.mod` as the application name if `EnableApplication` is `true` and no `Application` string is provided (works correctly for compiled go applications).
55+
56+
57+
### Framework Supported
58+
* [http/net](../../net/http/README.md) - basic support
59+
* [gorrila/mux](../../gorrila//mux/README.md) - proper support
60+
61+
62+
## Options
63+
64+
With Go SqlCommenter, we have configuration to choose which tags to be appended to the comment.
65+
66+
| Options | Included by default? | go-sql-driver |
67+
| ---------- | -------------------- | -------------------------------------------------------- |
68+
| `DBDriver` | | [ go-sql-driver](https://pkg.go.dev/database/sql/driver) |

0 commit comments

Comments
 (0)