为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
呼啦一阵风 2022-12-19 21:43:50
所以......我已经坚持了几天了,我已经遵循了文档和梨的建议但似乎没有用,我正在使用 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}
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

最后通过更新所有依赖项来修复,必须升级 GRPC 版本及其各自的依赖项。


还更改了 redis 事务使用的上下文。


这是最终代码


1 - 函数使用示例


dbTransaction := newrelic.FromContext(ctx)

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


    data, err := tools.Client.Get(newRelicContext, id).Result()

    

2 - Redis单例


package tools


import (

    "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 = nil

var Ctx context.Context = nil


func 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 tools


import (

    "context"

    "log"

    "os"


    newrelic "github.com/newrelic/go-agent/v3/newrelic"

)


var RelicApplication *newrelic.Application

var CurrentTransaction *newrelic.Transaction


func 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

    }


}



查看完整回答
反对 回复 2022-12-19
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信