Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/recover apm #613

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
run:
timeout: 3m
tests: false

issues:
exclude-use-default: false
exclude-rules:
- linters: [staticcheck]
text: 'SA1019: grpc.DialContext is deprecated: use NewClient instead'
11 changes: 4 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ func (d *Application) initConfig() error {
config.SetDefault("grpc_listen_port", 6000)
config.SetDefault("openapi_listen_host", "localhost")
config.SetDefault("openapi_listen_port", 3000)
config.SetDefault("otel_service_address", "otel-collector.guardian.svc.cluster.local:4317")

d.config = config

return nil
}

func (d *Application) setup() {
d.shutdown = observability.InitOtel(d.config)
d.shutdown = observability.Initialize()
}

func (d *Application) initExtensions() error {
Expand All @@ -137,11 +136,9 @@ func (d *Application) Close() error {
if err := d.closeExtensions(); err != nil {
return err
}
if d.shutdown != nil {
err := d.shutdown(context.Background())
if err != nil {
return err
}
err := d.shutdown(context.Background())
if err != nil {
return err
}
d.closed = true
return nil
Expand Down
2 changes: 0 additions & 2 deletions cmd/gobay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type _projConfig struct {
SkipSentry bool
SkipAsyncTask bool
SkipCache bool
SkipOtel bool
}

var (
Expand Down Expand Up @@ -77,7 +76,6 @@ func main() {
}
cmdNew.Flags().StringVar(&projConfig.Name, "name", "", "specific project name")
cmdNew.Flags().BoolVar(&projConfig.SkipSentry, "skip-sentry", false, "skip sentry")
cmdNew.Flags().BoolVar(&projConfig.SkipOtel, "skip-otel", false, "skip otel")
cmdNew.Flags().BoolVar(&projConfig.SkipCache, "skip-cache", false, "skip cache")
cmdNew.Flags().BoolVar(&projConfig.SkipAsyncTask, "skip-asynctask", false, "skip asynctask")

Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.5 (2024-12-16)

- Tracing 基础设施同时保留 APM 和 OpenTelemetry 并由从 config 中读取配置改为从环境变量中读取

# 1.2.4 (2024-9-27)

- otelsql 在过滤 Span 时增加对 TraceFlags 是否采样的判断
Expand Down
5 changes: 5 additions & 0 deletions extensions/cachext/backend/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/go-redis/redis"
"github.com/spf13/viper"
"go.elastic.co/apm/module/apmgoredis"

"github.com/shanbay/gobay/extensions/cachext"
"github.com/shanbay/gobay/observability"
)

func init() {
Expand All @@ -21,6 +23,9 @@ type redisBackend struct {
}

func (b *redisBackend) withContext(ctx context.Context) *redis.Client {
if observability.GetApmEnable() {
return apmgoredis.Wrap(b.client).WithContext(ctx).RedisClient()
}
return b.client.WithContext(ctx)
}

Expand Down
9 changes: 6 additions & 3 deletions extensions/cachext/backend/redis/v9/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.opentelemetry.io/otel"

"github.com/shanbay/gobay/extensions/cachext"
"github.com/shanbay/gobay/observability"
)

func init() {
Expand All @@ -32,9 +33,11 @@ func (b *redisBackend) Init(config *viper.Viper) error {
DB: dbNum,
})
b.client = redisClient
tp := otel.GetTracerProvider()
if err := redisotel.InstrumentTracing(redisClient, redisotel.WithTracerProvider(tp)); err != nil {
return err
if observability.GetOtelEnable() {
tp := otel.GetTracerProvider()
if err := redisotel.InstrumentTracing(redisClient, redisotel.WithTracerProvider(tp)); err != nil {
return err
}
}
_, err := redisClient.Ping(context.Background()).Result()
return err
Expand Down
7 changes: 6 additions & 1 deletion extensions/entext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
entsql "entgo.io/ent/dialect/sql"
"github.com/XSAM/otelsql"
"github.com/shanbay/gobay"
"github.com/shanbay/gobay/observability"
"go.elastic.co/apm/module/apmsql"
_ "go.elastic.co/apm/module/apmsql/mysql"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -53,7 +56,9 @@ func (d *EntExt) Init(app *gobay.Application) error {

var db *sql.DB
var err error
if app.Config().GetBool("otel_enable") {
if observability.GetApmEnable() {
db, err = apmsql.Open(dbDriver, dbURL)
} else if observability.GetOtelEnable() {
db, err = otelsql.Open(dbDriver, dbURL,
otelsql.WithSpanOptions(otelsql.SpanOptions{
DisableErrSkip: true,
Expand Down
20 changes: 15 additions & 5 deletions extensions/redisext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import (

"github.com/go-redis/redis"
"github.com/shanbay/gobay"
"github.com/shanbay/gobay/observability"
"go.elastic.co/apm/module/apmgoredis"
)

// RedisExt redis扩展,处理client的初始化工作
type RedisExt struct {
NS string
app *gobay.Application
prefix string
redisclient *redis.Client
NS string
app *gobay.Application
prefix string
redisclient *redis.Client
apmable bool
apmredisclient apmgoredis.Client
}

var _ gobay.Extension = (*RedisExt)(nil)

// Init
func (c *RedisExt) Init(app *gobay.Application) error {
if c.NS == "" {
return errors.New("lack of NS")
Expand All @@ -35,6 +38,10 @@ func (c *RedisExt) Init(app *gobay.Application) error {
}
c.prefix = config.GetString("prefix")
c.redisclient = redis.NewClient(&opt)
if observability.GetApmEnable() {
c.apmable = true
c.apmredisclient = apmgoredis.Wrap(c.redisclient)
}
_, err := c.redisclient.Ping().Result()
return err
}
Expand Down Expand Up @@ -89,6 +96,9 @@ func (c *RedisExt) Application() *gobay.Application {
}

func (c *RedisExt) Client(ctx context.Context) *redis.Client {
if c.apmable {
return c.apmredisclient.WithContext(ctx).RedisClient()
}
return c.redisclient.WithContext(ctx)
}

Expand Down
5 changes: 3 additions & 2 deletions extensions/redisext/v9/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/shanbay/gobay/observability"

"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
"github.com/shanbay/gobay"
Expand All @@ -24,7 +26,6 @@ type RedisExt struct {

var _ gobay.Extension = (*RedisExt)(nil)

// Init
func (c *RedisExt) Init(app *gobay.Application) error {
if c.NS == "" {
return errors.New("lack of NS")
Expand All @@ -37,7 +38,7 @@ func (c *RedisExt) Init(app *gobay.Application) error {
}
c.prefix = config.GetString("prefix")
c.redisClient = redis.NewClient(&opt)
if app.Config().GetBool("otel_enable") {
if observability.GetOtelEnable() {
tp := otel.GetTracerProvider()
if err := redisotel.InstrumentTracing(c.redisClient, redisotel.WithTracerProvider(tp)); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions extensions/stubext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"strconv"
"time"

"go.elastic.co/apm/module/apmgrpc"
"google.golang.org/grpc/status"

grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/shanbay/gobay"
"github.com/shanbay/gobay/observability"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -50,6 +52,7 @@ type StubExt struct {

Clients map[string]interface{}
conn *grpc.ClientConn
apmable bool
}

func (d *StubExt) Application() *gobay.Application { return d.app }
Expand Down Expand Up @@ -112,6 +115,7 @@ func (d *StubExt) Init(app *gobay.Application) error {
// init from config
d.app = app
config := app.Config()
d.apmable = observability.GetApmEnable()
config = gobay.GetConfigByPrefix(config, d.NS, true)
if err := config.Unmarshal(d); err != nil {
return err
Expand Down Expand Up @@ -165,6 +169,9 @@ func (d *StubExt) GetConn(userOpts ...grpc.DialOption) (*grpc.ClientConn, error)
if d.Authority != "" {
opts = append(opts, grpc.WithAuthority(d.Authority))
}
if d.apmable {
opts = append(opts, grpc.WithChainUnaryInterceptor(apmgrpc.NewUnaryClientInterceptor()))
}
// opts: user opts
opts = append(opts, userOpts...)
// opts: per call opts
Expand Down
Loading
Loading