Skip to content

Commit b29b610

Browse files
authored
Go http net implementation (#164)
Go HTTP initial implementation
1 parent a35d545 commit b29b610

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
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

go/net/http/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# http-net [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+
## Installation
6+
7+
### Install from source
8+
9+
* Clone the source
10+
* In terminal go inside the client folder location where we need to import sqlcommenter http/net module and enter the below commands
11+
12+
```shell
13+
go mod edit -replace google.com/sqlcommenter=path/to/google/sqlcommenter/http-net
14+
15+
go mod tiny
16+
17+
go get google.com/sqlcommenter/http-net
18+
```
19+
### Install from github [To be added]
20+
21+
## Usage
22+
23+
Please use the sqlcommenter's default go-sql database driver to execute statements. \
24+
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`.
25+
26+
```go
27+
db, err := gosql.Open("<driver>", "<connectionString>", sqlcommenter.CommenterOptions{<tag>:<bool>})
28+
```
29+
30+
### Configuration
31+
32+
Users are given control over what tags they want to append by using `core.CommenterOptions` struct.
33+
34+
```go
35+
type CommenterOptions struct {
36+
EnableDBDriver bool
37+
EnableTraceparent bool // OpenTelemetry trace information
38+
EnableRoute bool // applicable for web frameworks
39+
EnableFramework bool // applicable for web frameworks
40+
EnableController bool // applicable for web frameworks
41+
EnableAction bool // applicable for web frameworks
42+
}
43+
```
44+
45+
46+
#### Note
47+
* We only support the `database/sql` driver and have provided an implementation for that.
48+
* <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>
49+
* <b>The middleware implementing this sqlcommenter http-net module should be added at the last</b>
50+
51+
#### Example
52+
```go
53+
// middleware is used to intercept incoming HTTP calls and populate request context with commenter tags.
54+
func middleware(next http.Handler) http.Handler {
55+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56+
ctx := httpnet.NewHttpNet(r, next).AddTags(r.Context())
57+
next.ServeHTTP(w, r.WithContext(ctx))
58+
})
59+
}
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? | net/http |
67+
| --------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
68+
| `Action` | | [net/http handle](https://pkg.go.dev/net/http#Handle) |
69+
| `Route` | | [net/http routing path](https://pkg.go.dev/github.com/gorilla/mux#Route.URLPath) |
70+
| `Framework` | | [net/http](https://pkg.go.dev/net/http) |
71+
| `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) |

go/net/http/go.mod

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

go/net/http/go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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/google/sqlcommenter/go/core v0.0.1-beta h1:IVszEHanWVeS7UcmP8C3SHa57CmfeqMBj0QUcJ8VZ9Q=
6+
github.com/google/sqlcommenter/go/core v0.0.1-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
9+
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
10+
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
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+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

go/net/http/http.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 http
16+
17+
import (
18+
"net/http"
19+
20+
"github.com/google/sqlcommenter/go/core"
21+
)
22+
23+
type HTTPRequestExtractor struct {
24+
r *http.Request
25+
next any
26+
}
27+
28+
func NewHTTPRequestExtractor(r *http.Request, next any) *HTTPRequestExtractor {
29+
return &HTTPRequestExtractor{r, next}
30+
}
31+
32+
func (h *HTTPRequestExtractor) Route() string {
33+
return h.r.URL.Path
34+
}
35+
36+
func (h *HTTPRequestExtractor) Action() string {
37+
return core.GetFunctionName(h.next)
38+
}
39+
40+
func (h *HTTPRequestExtractor) Framework() string {
41+
return "net/http"
42+
}

0 commit comments

Comments
 (0)