Skip to content

Commit

Permalink
feat: add otel
Browse files Browse the repository at this point in the history
fix: fix lint
style: opt code
feat: add sample config
fix: remove ctx
feat: update cache support ctx

style: opt code
  • Loading branch information
Han-Ya-Jun committed Jun 27, 2023
1 parent 1920d74 commit 6251958
Show file tree
Hide file tree
Showing 65 changed files with 638 additions and 262 deletions.
16 changes: 15 additions & 1 deletion src/core-api/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"core/pkg/database"
"core/pkg/logging"
"core/pkg/metric"
"core/pkg/trace"

"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
Expand Down Expand Up @@ -60,7 +61,7 @@ func initDatabase() {
panic("database apigateway should be configured")
}

database.InitDBClients(&defaultDBConfig)
database.InitDBClients(&defaultDBConfig, globalConfig.Trace)

logging.GetLogger().Info("init Database success")
}
Expand All @@ -86,3 +87,16 @@ func initMetrics() {
metric.InitMetrics()
logging.GetLogger().Info("init Metrics success")
}

func initTrace() {
if !globalConfig.Trace.Enable {
logging.GetLogger().Info("trace is not enabled, will not init it")
return
}
logging.GetLogger().Info("trace is enabled")
err := trace.InitTrace(globalConfig.Trace)
if err != nil {
logging.GetLogger().Errorf("init trace fail: %+v", err)
return
}
}
3 changes: 2 additions & 1 deletion src/core-api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func Start() {
}
initConfig()

// 1.
// 1. init components
initTrace()
initLogger()
initSentry()
initMetrics()
Expand Down
12 changes: 12 additions & 0 deletions src/core-api/config.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ logger:
writer: file
buffered: true
settings: {name: core_api.log, size: 100, backups: 10, age: 7, path: ./}

## config for trace
trace:
enable: false
endpoint: "127.0.0.1:4318"
type: "http"
sampler: "always_on"
token: "blueking"
serviceName: "apigateway-core-api"
instrument:
ginApi: true
dbApi: true
31 changes: 24 additions & 7 deletions src/core-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.20

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/TencentBlueKing/gopkg v1.1.0
github.com/TencentBlueKing/gopkg v1.2.0
github.com/agiledragon/gomonkey/v2 v2.10.1
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/getsentry/sentry-go v0.20.0
github.com/gin-gonic/gin v1.8.1
github.com/gin-gonic/gin v1.8.2
github.com/go-playground/validator/v10 v10.11.1
github.com/go-sql-driver/mysql v1.7.0
github.com/gofrs/uuid v4.4.0+incompatible
Expand All @@ -20,25 +20,37 @@ require (
github.com/prometheus/client_golang v1.15.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.3
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.2
github.com/uptrace/opentelemetry-go-extra/otelsqlx v0.2.2
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.42.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.uber.org/zap v1.24.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand All @@ -59,7 +71,10 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
Expand All @@ -68,6 +83,8 @@ require (
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 6251958

Please sign in to comment.