chore(example/blog): add ent tracing, and print the original SQL (#1163)

pull/1166/head
包子 3 years ago committed by GitHub
parent 14d383bc6f
commit 6e9ce7af22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      examples/blog/internal/data/data.go

@ -2,14 +2,20 @@ package data
import ( import (
"context" "context"
"fmt"
"github.com/go-kratos/kratos/examples/blog/internal/conf" "github.com/go-kratos/kratos/examples/blog/internal/conf"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent" "github.com/go-kratos/kratos/examples/blog/internal/data/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-redis/redis/extra/redisotel" "github.com/go-redis/redis/extra/redisotel"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"github.com/google/wire" "github.com/google/wire"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
// init mysql driver // init mysql driver
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -26,10 +32,24 @@ type Data struct {
// NewData . // NewData .
func NewData(conf *conf.Data, logger log.Logger) (*Data, func(), error) { func NewData(conf *conf.Data, logger log.Logger) (*Data, func(), error) {
log := log.NewHelper(logger) log := log.NewHelper(logger)
client, err := ent.Open( drv, err := sql.Open(
conf.Database.Driver, conf.Database.Driver,
conf.Database.Source, conf.Database.Source,
) )
sqlDrv := dialect.DebugWithContext(drv, func(ctx context.Context, i ...interface{}) {
log.WithContext(ctx).Info(i...)
tracer := otel.Tracer("ent.")
kind := trace.SpanKindServer
_, span := tracer.Start(ctx,
"Query",
trace.WithAttributes(
attribute.String("sql", fmt.Sprint(i...)),
),
trace.WithSpanKind(kind),
)
span.End()
})
client := ent.NewClient(ent.Driver(sqlDrv))
if err != nil { if err != nil {
log.Errorf("failed opening connection to sqlite: %v", err) log.Errorf("failed opening connection to sqlite: %v", err)
return nil, nil, err return nil, nil, err

Loading…
Cancel
Save