Skip to content

Commit

Permalink
Go http net implementation (#164)
Browse files Browse the repository at this point in the history
Go HTTP initial implementation
  • Loading branch information
Thiyagu55 committed Sep 30, 2022
1 parent a35d545 commit b29b610
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
php/sqlcommenter-php/packages/sqlcommenter-laravel/vendor/*
.idea/**
.idea/**
.DS_Store
71 changes: 71 additions & 0 deletions go/net/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# http-net [In development]

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.

## Installation

### Install from source

* Clone the source
* In terminal go inside the client folder location where we need to import sqlcommenter http/net module and enter the below commands

```shell
go mod edit -replace google.com/sqlcommenter=path/to/google/sqlcommenter/http-net

go mod tiny

go get google.com/sqlcommenter/http-net
```
### Install from github [To be added]

## Usage

Please use the sqlcommenter's default go-sql database driver to execute statements. \
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`.

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

### Configuration

Users are given control over what tags they want to append by using `core.CommenterOptions` struct.

```go
type CommenterOptions struct {
EnableDBDriver bool
EnableTraceparent bool // OpenTelemetry trace information
EnableRoute bool // applicable for web frameworks
EnableFramework bool // applicable for web frameworks
EnableController bool // applicable for web frameworks
EnableAction bool // applicable for web frameworks
}
```


#### Note
* We only support the `database/sql` driver and have provided an implementation for that.
* <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>
* <b>The middleware implementing this sqlcommenter http-net module should be added at the last</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 := httpnet.NewHttpNet(r, next).AddTags(r.Context())
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? | net/http |
| --------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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) |
10 changes: 10 additions & 0 deletions go/net/http/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/google/sqlcommenter/go/net/http

go 1.19

require github.com/google/sqlcommenter/go/core v0.0.1-beta

require (
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
)
13 changes: 13 additions & 0 deletions go/net/http/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/sqlcommenter/go/core v0.0.1-beta h1:IVszEHanWVeS7UcmP8C3SHa57CmfeqMBj0QUcJ8VZ9Q=
github.com/google/sqlcommenter/go/core v0.0.1-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
42 changes: 42 additions & 0 deletions go/net/http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 http

import (
"net/http"

"github.com/google/sqlcommenter/go/core"
)

type HTTPRequestExtractor struct {
r *http.Request
next any
}

func NewHTTPRequestExtractor(r *http.Request, next any) *HTTPRequestExtractor {
return &HTTPRequestExtractor{r, next}
}

func (h *HTTPRequestExtractor) Route() string {
return h.r.URL.Path
}

func (h *HTTPRequestExtractor) Action() string {
return core.GetFunctionName(h.next)
}

func (h *HTTPRequestExtractor) Framework() string {
return "net/http"
}

0 comments on commit b29b610

Please sign in to comment.