https://github.com/ariga/sqlcomment
sqlcommenter support for ent
https://github.com/ariga/sqlcomment
Last synced: about 2 months ago
JSON representation
sqlcommenter support for ent
- Host: GitHub
- URL: https://github.com/ariga/sqlcomment
- Owner: ariga
- License: apache-2.0
- Created: 2021-10-12T14:22:38.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-08-16T12:36:08.000Z (almost 3 years ago)
- Last Synced: 2025-04-11T23:15:23.308Z (about 2 months ago)
- Language: Go
- Size: 132 KB
- Stars: 56
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlcomm**ent**
sqlcomm**ent** is an [ent](https://entgo.io) driver that adds SQL comments following [sqlcommenter specification](https://google.github.io/sqlcommenter/spec/).
sqlcomment includes support for OpenTelemetry and OpenCensus (see [examples](examples/)).# Installing
```bash
go install ariga.io/sqlcomment
```# Basic Usage
```go
// Create db driver.
// make sure to import "entgo.io/ent/dialect/sql" instead of "database/sql"
db, err := sql.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
// Create sqlcomment driver which wraps sqlite driver.
drv := sqlcomment.NewDriver(db,
sqlcomment.WithDriverVerTag(),
sqlcomment.WithTags(sqlcomment.Tags{
sqlcomment.KeyApplication: "my-app",
sqlcomment.KeyFramework: "net/http",
}),
)
// Create and configure ent client
client := ent.NewClient(ent.Driver(drv))
```# Adding context level tags
Suppose you have a REST API and you want to add a tag with the request URL (typically `route` tag). You can achieve that by using `sqlcomment.WithTag(ctx, key, val)` which adds the given tag to the context to later be serialized by sqlcomment driver. (see full example [here](examples/otel/example_test.go))
```go
// Add a middleware to your HTTP server which puts the `route` tag on the context for every request.
middleware := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := sqlcomment.WithTag(r.Context(), "route", r.URL.Path)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
```