Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for go-sql-driver #161

Merged
merged 22 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Sqlcommenter [In development]

Sqlcommenter is a plugin/middleware/wrapper to augment SQL statements from go libraries with comments that can be used later to correlate user code with SQL statements.

## Installation

### Install from source

* Clone the source
* In terminal go inside the client folder location where we need to import google-sqlcommenter package and enter the below commands

```shell
go mod edit -replace google.com/sqlcommenterGo=path/to/google/sqlcommenter/go
Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved

go mod tiny
```
### Install from github [To be added]
Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved

## Usages

### go-sql-driver
Use the sqlcommenter's db driver to perform query
Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved

```go
db, err := sqlcommenterGo.Open("<driver>", "<connectionString>", sqlcommenter.CommenterOptions{<tag>:<bool>})

Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved
```

#### Configuration

Tags to be appended can be configured by creating a variable of type sqlcommenter.CommenterOptions

```go
type CommenterOptions struct {
DBDriver bool
Traceparent bool
Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved
Route bool //applicable for web frameworks
Framework bool //applicable for web frameworks
Controller bool //applicable for web frameworks
Action bool //applicable for web frameworks
}
```
### net/http
Populate the request context with sqlcommenter.AddHttpRouterTags(r) function in a custom middleware.

#### Note
* <b>It needs to be used with drivers such as go-sql-orm </b>
* <b>ORM related tags are added to the driver only when the tags are enabled in the commenter's driver's config and also the request context should passed to the querying functions</b>

#### Example
```go
// middleware is used to intercept incoming HTTP calls and populate request context with commenter tags.
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := sqlcommenter.AddHttpRouterTags(r, next)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
```

## Options

With Go SqlCommenter, we have configuration to choose which tags to be appended to the comment.

| Options | Included by default? | go-sql-orm | net/http | Notes |
| --------------- | :------------------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---: |
| `DBDriver` | | [ go-sql-driver](https://pkg.go.dev/database/sql/driver) | |
| `Action` | | | [net/http handle](https://pkg.go.dev/net/http#Handle) | |
| `Route` | | | [net/http routing path](https://pkg.go.dev/github.com/gorilla/mux#Route.URLPath) | |
| `Framework` | | | [net/http](https://pkg.go.dev/net/http) | |
| `Opentelemetry` | | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | [W3C TraceContext.Traceparent](https://www.w3.org/TR/trace-context/#traceparent-field), [W3C TraceContext.Tracestate](https://www.w3.org/TR/trace-context/#tracestate-field) | |
155 changes: 155 additions & 0 deletions go/go-sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sqlcommenter

import (
"context"
"database/sql"
"fmt"
"net/http"
"net/url"
"reflect"
"runtime"
"strings"
)

type DB struct {
*sql.DB
CommenterOptions CommenterOptions
}

type CommenterOptions struct {
DBDriver bool
Route bool
Framework bool
Controller bool
Action bool
}

func Open(driverName string, dataSourceName string, commenterOptions CommenterOptions) (*DB, error) {
db, err := sql.Open(driverName, dataSourceName)
return &DB{DB: db, CommenterOptions: commenterOptions}, err
}

// ***** Query Functions *****

func (db *DB) Query(query string, args ...any) (*sql.Rows, error) {
return db.DB.Query(db.withComment(context.Background(), query), args...)
}

func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row {
return db.DB.QueryRow(db.withComment(context.Background(), query), args...)
}

func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return db.DB.QueryContext(ctx, db.withComment(ctx, query), args...)
}

func (db *DB) Exec(query string, args ...any) (sql.Result, error) {
return db.DB.Exec(db.withComment(context.Background(), query), args...)
}

func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
return db.DB.ExecContext(ctx, db.withComment(ctx, query), args...)
}

func (db *DB) Prepare(query string) (*sql.Stmt, error) {
return db.DB.Prepare(db.withComment(context.Background(), query))
}

func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return db.DB.PrepareContext(ctx, db.withComment(ctx, query))
}

// ***** Query Functions *****

// ***** Framework Functions *****

func AddHttpRouterTags(r *http.Request, next any) context.Context { // any type is set because we need to refrain from importing http-router package
ctx := context.Background()
ctx = context.WithValue(ctx, "route", r.URL.Path)
ctx = context.WithValue(ctx, "action", getFunctionName(next))
ctx = context.WithValue(ctx, "framework", "net/http")
return ctx
}

// ***** Framework Functions *****

// ***** Commenter Functions *****

func (db *DB) withComment(ctx context.Context, query string) string {

Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved
var finalCommentsMap = map[string]string{}
var finalCommentsStr string = ""
query = strings.TrimSpace(query)

// Sorted alphabetically
if db.CommenterOptions.Action && (ctx.Value("action") != nil) {
finalCommentsMap["action"] = ctx.Value("action").(string)
}

if db.CommenterOptions.DBDriver {
finalCommentsMap["driver"] = "go/sql"
}

Thiyagu55 marked this conversation as resolved.
Show resolved Hide resolved
if db.CommenterOptions.Framework && (ctx.Value("framework") != nil) {
finalCommentsMap["framework"] = ctx.Value("framework").(string)
}

if db.CommenterOptions.Route && (ctx.Value("route") != nil) {
finalCommentsMap["route"] = ctx.Value("route").(string)
}

if len(finalCommentsMap) > 0 { // Converts comments map to string and appends it to query
finalCommentsStr = fmt.Sprintf("/*%s*/", convertMapToComment(finalCommentsMap))
fmt.Println(finalCommentsStr)
}

if query[len(query)-1:] == ";" {
return fmt.Sprintf("%s%s;", strings.TrimSuffix(query, ";"), finalCommentsStr)
}
return fmt.Sprintf("%s%s", query, finalCommentsStr)

}

// ***** Commenter Functions *****

// ***** Util Functions *****

func encodeURL(k string) string {
return url.QueryEscape(string(k))
}

func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

func convertMapToComment(tags map[string]string) string {
var sb strings.Builder
i, sz := 0, len(tags)
for key, val := range tags {
if i == sz-1 {
sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(val)))
} else {
sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(val)))
}
i++
}
return sb.String()
}

// ***** Util Functions *****
3 changes: 3 additions & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module google.com/sqlcommenter

go 1.19
Empty file added go/go.sum
Empty file.