|
1 | | -# http-net [In development] |
| 1 | +# SQLCommenter http-net [In development] |
2 | 2 |
|
3 | 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 | 4 |
|
5 | 5 | ## Installation |
6 | 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 |
| 7 | +```bash |
| 8 | +go get -u github.com/google/sqlcommenter/go/net/http |
18 | 9 | ``` |
19 | | -### Install from github [To be added] |
20 | 10 |
|
21 | 11 | ## Usage |
22 | 12 |
|
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. |
| 13 | +This is a low-level package that can be used to prepare SQLCommeneterTags out of an http request. The core package can then be used to inject these tags into a context |
33 | 14 |
|
34 | 15 | ```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 | | - |
| 16 | +import ( |
| 17 | + sqlcommenterhttp "github.com/google/sqlcommenter/go/net/http" |
| 18 | +) |
45 | 19 |
|
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 | | -} |
| 20 | +requestTags := sqlcommenterhttp.NewHTTPRequestTags(framework string, route string, action string) |
| 21 | +ctx := core.ContextInject(request.Context(), requestTags) |
| 22 | +requestWithTags := request.WithContext(ctx) |
60 | 23 | ``` |
61 | 24 |
|
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) | |
| 25 | +This package can be used to instrument SQLCommenter for various frameworks. |
0 commit comments