Skip to content

Commit 85b3986

Browse files
authored
feat: add otel for gorm (#1)
* feat: init repository * feat: opentelemetry * doc: fix readme * doc: fix readme * style: remove useless comment * chore: add license * fix: provider name && example * feat: add pr check * feat: add example and fix README * feat: optimize example * feat: add metrics example * doc: add pic to readme * doc: fix readme * doc: optimize readme * fix: linter * fix: ci fail_on_error * feat: add ci * doc: optimize example/demo/readme * style: run gofumpt * doc: optimize readme * style: remove space line * style: optimize doc and add space * style: doc * doc: optimize readme * doc: change pic && optimize readme * test: optimize logging test * doc: add example * style: gofumpt * style: add space * style: add space * style: add space * style: add space * feat: optimize dockerfile * style: remove useless code
1 parent ba8b6db commit 85b3986

38 files changed

+3245
-0
lines changed

.github/workflows/pr-check.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Pull Request Check
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
staticcheck:
7+
runs-on: ubuntu-18.04
8+
steps:
9+
- uses: actions/checkout@v3
10+
11+
- name: Set up Go
12+
uses: actions/setup-go@v3
13+
with:
14+
go-version: 1.18
15+
16+
- uses: actions/cache@v3
17+
with:
18+
path: ~/go/pkg/mod
19+
key: reviewdog-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
20+
restore-keys: |
21+
reviewdog-${{ runner.os }}-go-
22+
23+
- uses: reviewdog/action-staticcheck@v1
24+
with:
25+
github_token: ${{ secrets.github_token }}
26+
# Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
27+
reporter: github-pr-review
28+
# Report all results.
29+
filter_mode: nofilter
30+
# Exit with 1 when it find at least one finding.
31+
fail_on_error: true
32+
# Set staticcheck flags
33+
staticcheck_flags: -checks=inherit,-SA1029

.github/workflows/release-check.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release Check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Check Source Branch
15+
run: python2 -c "exit(0 if '${{ github.head_ref }}'.startswith('release') or '${{ github.head_ref }}'.startswith('hotfix') else 1)"
16+
17+
- name: Check Version
18+
run: |
19+
# get version code, runner not support grep -E here
20+
SOURCE_VERSION=`grep 'Version\s*=\s*\"v[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\"' *.go | awk -F '\"' '{print $(NF-1)}'`
21+
git checkout main
22+
MASTER_VERSION=`grep 'Version\s*=\s*\"v[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\"' *.go | awk -F '\"' '{print $(NF-1)}'`
23+
git checkout ${{Head.SHA}}
24+
# check version update
25+
python2 -c "exit(0 if list(map(int,'${SOURCE_VERSION#v}'.split('.')[:3])) > list(map(int,'${MASTER_VERSION#v}'.split('.')[:3])) else 1)"

.github/workflows/tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
lint-and-ut:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
11+
- name: Set up Go
12+
uses: actions/setup-go@v3
13+
with:
14+
go-version: 1.18
15+
16+
- uses: actions/cache@v3
17+
with:
18+
path: ~/go/pkg/mod
19+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
20+
restore-keys: |
21+
${{ runner.os }}-go-
22+
23+
- name: Lint
24+
run: |
25+
go vet -stdmethods=false $(go list ./...)
26+
go install mvdan.cc/[email protected]
27+
test -z "$(gofumpt -l -extra .)"
28+
29+
- name: Unit Test
30+
run: go test -race -covermode=atomic -coverprofile=coverage.out ./...

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
metrics/test.db
3+
logging/logrus/test.db
4+
.DS_Store

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
11
# opentelemetry
2+
[Opentelemetry](https://opentelemetry.io/) for [gorm](https://github.com/go-gorm/gorm)
3+
## Feature
4+
### Tracing
5+
- support tracing gorm by Hook `Create` `Query` `Delete` `Update` `Row` `Raw`
6+
### Metrics
7+
- Collect DB Status
8+
### Logging
9+
- Use logrus replace gorm default logger
10+
- Use hook to report span message
11+
### Provider
12+
- Out-of-the-box default opentelemetry provider
13+
- Support setting via environment variables
14+
15+
## How to Use ?
16+
### Set logger
17+
~~~go
18+
package main
19+
20+
import(
21+
"gorm.io/gorm/logger"
22+
"gorm.io/plugin/opentelemetry/logging/logrus"
23+
)
24+
25+
func init(){
26+
logger := logger.New(
27+
logrus.NewWriter(),
28+
logger.Config{
29+
SlowThreshold: time.Millisecond,
30+
LogLevel: logger.Warn,
31+
Colorful: false,
32+
},
33+
)
34+
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"),&gorm.Config{Logger: logger})
35+
}
36+
~~~
37+
### Set tracing and metrics
38+
39+
~~~go
40+
package main
41+
42+
import(
43+
"gorm.io/plugin/opentelemetry/tracing"
44+
)
45+
46+
func init(){
47+
48+
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
if err := db.Use(tracing.NewPlugin()); err != nil {
54+
panic(err)
55+
}
56+
}
57+
~~~
58+
59+
### Set only tracing
60+
~~~go
61+
package main
62+
63+
import(
64+
"gorm.io/plugin/opentelemetry/tracing"
65+
)
66+
67+
func init(){
68+
69+
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
if err := db.Use(tracing.NewPlugin(tracing.WithoutMetrics())); err != nil {
75+
panic(err)
76+
}
77+
}
78+
~~~
79+
80+
81+
82+
### More info
83+
See [examples](examples/)

examples/demo/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Tracing example
2+
3+
## Case 1: Stdout exporter (default):
4+
5+
#### 1. Run demo
6+
7+
```shell
8+
cd examples/demo
9+
go run main.go
10+
```
11+
12+
#### Screenshots
13+
14+
![stdout.png](static/stdout.png)
15+
16+
---
17+
18+
## Case 2: Jaeger exporter:
19+
20+
#### 1.Setup Basic Dependence
21+
22+
```shell
23+
docker-compose up -d
24+
```
25+
26+
#### 2. Set environment variables
27+
28+
```shell
29+
export OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14268/api/traces
30+
```
31+
32+
#### 3. Run demo
33+
34+
```shell
35+
cd examples/demo
36+
go run main.go
37+
```
38+
39+
### View Trace
40+
41+
You can then navigate to http://localhost:16686 to access the Jaeger UI.
42+
43+
#### Screenshots
44+
45+
![static/jeager.png](static/jeager.png)
46+

examples/demo/docker-compose.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "3.7"
2+
services:
3+
4+
# Jaeger
5+
jaeger-all-in-one:
6+
image: jaegertracing/all-in-one:latest
7+
environment:
8+
- COLLECTOR_OTLP_ENABLED=true
9+
ports:
10+
- "16686:16686"
11+
- "14268:14268"
12+
- "14250:14250"
13+
- "6831:6831"
14+
15+
# Grafana
16+
grafana:
17+
image: grafana/grafana:latest
18+
environment:
19+
- GF_AUTH_ANONYMOUS_ENABLED=true
20+
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
21+
- GF_AUTH_DISABLE_LOGIN_FORM=true
22+
ports:
23+
- "3000:3000"

examples/demo/go.mod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module gorm.io/plugin/example
2+
3+
go 1.18
4+
5+
replace gorm.io/plugin/opentelemetry => ./../..
6+
7+
require (
8+
go.opentelemetry.io/otel v1.8.0
9+
go.opentelemetry.io/otel/exporters/jaeger v1.7.0
10+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0
11+
go.opentelemetry.io/otel/sdk v1.8.0
12+
go.opentelemetry.io/otel/trace v1.8.0
13+
gorm.io/driver/sqlite v1.3.4
14+
gorm.io/gorm v1.23.6
15+
gorm.io/plugin/opentelemetry v0.0.0-00010101000000-000000000000
16+
)
17+
18+
require (
19+
github.com/go-logr/logr v1.2.3 // indirect
20+
github.com/go-logr/stdr v1.2.2 // indirect
21+
github.com/jinzhu/inflection v1.0.0 // indirect
22+
github.com/jinzhu/now v1.1.5 // indirect
23+
github.com/mattn/go-sqlite3 v1.14.12 // indirect
24+
github.com/sirupsen/logrus v1.8.1 // indirect
25+
go.opentelemetry.io/otel/metric v0.31.0 // indirect
26+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
27+
)

examples/demo/go.sum

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
5+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
6+
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
7+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
8+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
9+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
10+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
11+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
12+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
13+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
14+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
15+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
16+
github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
17+
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
18+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
19+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
21+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
22+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
23+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
24+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
25+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
26+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
27+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
28+
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
29+
go.opentelemetry.io/otel v1.8.0 h1:zcvBFizPbpa1q7FehvFiHbQwGzmPILebO0tyqIR5Djg=
30+
go.opentelemetry.io/otel v1.8.0/go.mod h1:2pkj+iMj0o03Y+cW6/m8Y4WkRdYN3AvCXCnzRMp9yvM=
31+
go.opentelemetry.io/otel/exporters/jaeger v1.7.0 h1:wXgjiRldljksZkZrldGVe6XrG9u3kYDyQmkZwmm5dI0=
32+
go.opentelemetry.io/otel/exporters/jaeger v1.7.0/go.mod h1:PwQAOqBgqbLQRKlj466DuD2qyMjbtcPpfPfj+AqbSBs=
33+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0 h1:8hPcgCg0rUJiKE6VWahRvjgLUrNl7rW2hffUEPKXVEM=
34+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.7.0/go.mod h1:K4GDXPY6TjUiwbOh+DkKaEdCF8y+lvMoM6SeAPyfCCM=
35+
go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9spDTqUs=
36+
go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A=
37+
go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe374q6cZwUU=
38+
go.opentelemetry.io/otel/sdk v1.8.0 h1:xwu69/fNuwbSHWe/0PGS888RmjWY181OmcXDQKu7ZQk=
39+
go.opentelemetry.io/otel/sdk v1.8.0/go.mod h1:uPSfc+yfDH2StDM/Rm35WE8gXSNdvCg023J6HeGNO0c=
40+
go.opentelemetry.io/otel/trace v1.7.0/go.mod h1:fzLSB9nqR2eXzxPXb2JW9IKE+ScyXA48yyE4TNvoHqU=
41+
go.opentelemetry.io/otel/trace v1.8.0 h1:cSy0DF9eGI5WIfNwZ1q2iUyGj00tGzP24dE1lOlHrfY=
42+
go.opentelemetry.io/otel/trace v1.8.0/go.mod h1:0Bt3PXY8w+3pheS3hQUt+wow8b1ojPaTBoTCh2zIFI4=
43+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
44+
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
46+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
48+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
49+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
50+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
51+
gorm.io/driver/sqlite v1.3.4 h1:NnFOPVfzi4CPsJPH4wXr6rMkPb4ElHEqKMvrsx9c9Fk=
52+
gorm.io/driver/sqlite v1.3.4/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U=
53+
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
54+
gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0=
55+
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

0 commit comments

Comments
 (0)