Golang,redis 交易并且没有在新的遗迹面板中显示

所以......我已经坚持了几天了,我已经遵循了文档和梨的建议但似乎没有用,我正在使用 Golang 和 GRPC 并在其中实施一个新的遗物,以跟踪事务和整体性能,我设法声明事务和段,但就数据库事务而言,它们没有出现在数据库部分,我试图在执行事务时将新的遗留上下文传递到 Redis 客户端,也尝试使用背景上下文和当前事务上下文,但似乎不起作用。进行了查询但没有报告任何数据这是我所拥有的示例


1 - 进行交易的功能之一


   // updateCache ...

func updateCached(data *statemachinepkgv1.StateMachine, sessionID string, ctx context.Context) (*statemachinepkgv1.StateMachine, error) {


    //Transaction and segment logic

    

    //this does not create a brand new relic application, just gets the main instance

    relic, err := tools.InitRelic()


    if err != nil {

        log.Fatalf("failed to create/instace newrelic.NewApplication: %v", err)

    }


    txn := relic.StartTransaction("redis", nil, nil)


    defer newrelic.StartSegment(txn, "updateCached").End()

    defer newrelic.StartSegment(tools.CurrentTransaction, "updateCached").End() //tools.CurrentTransaction has the context of main web transaction stored with singleton design


    defer txn.End()


    dataParse, err := json.Marshal(data)

    if err != nil {

        return data, err

    }


    duration, err := time.ParseDuration(os.Getenv("GO_STATEMACHINE_REDIS_TIMELIFE"))

    if err != nil {

        return data, err

    }


    //REDIS LOGIC

    dbTransaction := newrelic.FromContext(ctx)

    newRelicContext := newrelic.NewContext(context.Background(), dbTransaction)


    err = masterClient.Set(newRelicContext, sessionID, string(dataParse), duration).Err()


    if err != nil {

        return data, err

    }


    return data, nil


}


呼啦一阵风
浏览 133回答 1
1回答

海绵宝宝撒

最后通过更新所有依赖项来修复,必须升级 GRPC 版本及其各自的依赖项。还更改了 redis 事务使用的上下文。这是最终代码1 - 函数使用示例dbTransaction := newrelic.FromContext(ctx)    newRelicContext := newrelic.NewContext(context.Background(), dbTransaction)    data, err := tools.Client.Get(newRelicContext, id).Result()    2 - Redis单例package toolsimport (    "context"    "os"    "github.com/go-redis/redis/v8"    nrredis "github.com/newrelic/go-agent/v3/integrations/nrredis-v8"    newrelic "github.com/newrelic/go-agent/v3/newrelic")var redisOpt = &redis.Options{    Addr:     os.Getenv("****************") + ":" + os.Getenv("*****************"),    DB:       1,    Password: os.Getenv("******************"),}var Client *redis.Client = nilvar Ctx context.Context = nilfunc getTransaction() *newrelic.Transaction { return nil }func init() {    Client = redis.NewClient(redisOpt)    Client.AddHook(nrredis.NewHook(redisOpt))    txn := getTransaction()    Ctx = newrelic.NewContext(context.Background(), txn)}func GetRedis() (*redis.Client, context.Context) {    if Client == nil {        Client = redis.NewClient(redisOpt)        Client.AddHook(nrredis.NewHook(redisOpt))        txn := getTransaction()        Ctx = newrelic.NewContext(context.Background(), txn)    }    return Client, Ctx}3 - newRelic 单例package toolsimport (    "context"    "log"    "os"    newrelic "github.com/newrelic/go-agent/v3/newrelic")var RelicApplication *newrelic.Applicationvar CurrentTransaction *newrelic.Transactionfunc init() {    log.Println("INIT RELIC CREATING AT RUNTIME")    var err error    RelicApplication, err = newrelic.NewApplication(        newrelic.ConfigAppName("**********************"),        newrelic.ConfigLicense(os.Getenv("NRELIC_KEY")),        newrelic.ConfigDebugLogger(os.Stdout),        newrelic.ConfigDistributedTracerEnabled(true),        func(config *newrelic.Config) {            config.Enabled = true        },    )    if err != nil {        log.Println("ERROR INITING NEW RELIC: ", err)    }    log.Println("INIT RELIC = RETURNING")}func SetSubTransaction(txn *newrelic.Transaction) {    //set new current transaction    CurrentTransaction = txn}func SetSubTransactionByContext(ctx context.Context) {    txn := newrelic.FromContext(ctx)    SetSubTransaction(txn)}func InitRelic() (*newrelic.Application, error) {    if RelicApplication == nil {        var err error        writer, err := os.OpenFile("log_file", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)        if err != nil {            log.Println("ERROR OPENING LOG FILE: ", err)            return nil, err        }        RelicApplication, err = newrelic.NewApplication(            newrelic.ConfigAppName("**********************"),            newrelic.ConfigLicense(os.Getenv("NRELIC_KEY")),            newrelic.ConfigDebugLogger(os.Stdout),            newrelic.ConfigInfoLogger(writer),            newrelic.ConfigDistributedTracerEnabled(true),            func(config *newrelic.Config) {                config.Enabled = false            },        )        if err != nil {            log.Println("ERROR INITING NEW RELIC: ", err)        }        return RelicApplication, err    } else {        return RelicApplication, nil    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go