亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號