From b29b610d675973672d38f33cf3bbe8d9dcc43a95 Mon Sep 17 00:00:00 2001 From: Thiyagu55 <64461612+Thiyagu55@users.noreply.github.com> Date: Fri, 30 Sep 2022 19:13:41 +0530 Subject: [PATCH] Go http net implementation (#164) Go HTTP initial implementation --- .gitignore | 3 +- go/net/http/README.md | 71 +++++++++++++++++++++++++++++++++++++++++++ go/net/http/go.mod | 10 ++++++ go/net/http/go.sum | 13 ++++++++ go/net/http/http.go | 42 +++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 go/net/http/README.md create mode 100644 go/net/http/go.mod create mode 100644 go/net/http/go.sum create mode 100644 go/net/http/http.go diff --git a/.gitignore b/.gitignore index cc3016a8..c792cffb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ php/sqlcommenter-php/packages/sqlcommenter-laravel/vendor/* -.idea/** \ No newline at end of file +.idea/** +.DS_Store diff --git a/go/net/http/README.md b/go/net/http/README.md new file mode 100644 index 00000000..6c148954 --- /dev/null +++ b/go/net/http/README.md @@ -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("", "", sqlcommenter.CommenterOptions{:}) +``` + +### 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. +* 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 +* The middleware implementing this sqlcommenter http-net module should be added at the last + +#### 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) | diff --git a/go/net/http/go.mod b/go/net/http/go.mod new file mode 100644 index 00000000..42d8dd79 --- /dev/null +++ b/go/net/http/go.mod @@ -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 +) diff --git a/go/net/http/go.sum b/go/net/http/go.sum new file mode 100644 index 00000000..c0518c2b --- /dev/null +++ b/go/net/http/go.sum @@ -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= diff --git a/go/net/http/http.go b/go/net/http/http.go new file mode 100644 index 00000000..5d762687 --- /dev/null +++ b/go/net/http/http.go @@ -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" +}