首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golang,redis事务处理,未在新的文物面板中显示

Golang,redis事务处理,未在新的文物面板中显示
EN

Stack Overflow用户
提问于 2022-06-16 15:44:06
回答 1查看 274关注 0票数 0

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

1-处理事务的功能之一

代码语言:javascript
复制
   // 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

}

2-红花单例

代码语言:javascript
复制
   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("GO_STATEMACHINE_REDIS_HOST") + ":" + os.Getenv("GO_STATEMACHINE_REDIS_PORT"),
    DB:       1,
    Password: os.Getenv("GO_STATEMACHINE_REDIS_PASSWORD"),
}

var Client *redis.Client = nil
var Ctx context.Context = nil

func getTransaction() *newrelic.Transaction {
    redisTransaction := newrelic.FromContext(context.Background())
    return redisTransaction
}

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-新遗物单身人士

代码语言:javascript
复制
   package tools

import (
    "context"
    "fmt"
    "log"
    "os"

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

var RelicApplication newrelic.Application = nil
var CurrentTransaction newrelic.Transaction = nil

func init() {

    fmt.Println("INIT RELIC CREATING AT RUNTIME")
    cfg := newrelic.NewConfig("go-vozyengine-statemachine-service", os.Getenv("NRELIC_KEY"))
    cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    cfg.BrowserMonitoring.Attributes.Enabled = true
    cfg.TransactionTracer.Enabled = true
    cfg.TransactionEvents.Enabled = true
    cfg.DistributedTracer.Enabled = true
    cfg.CustomInsightsEvents.Enabled = true
    cfg.ErrorCollector.Enabled = true
    cfg.ErrorCollector.CaptureEvents = true

    var err error
    RelicApplication, err = newrelic.NewApplication(cfg)

    if err != nil {
        log.Fatalf("failed to create newrelic.NewApplication: %v", err)
    }

    fmt.Println("INIT RELIC = RETURNING")

}

func SetSubTransaction(txn newrelic.Transaction) {
    //set new current transaction
    CurrentTransaction = txn
}

func SetSubTransactionByContext(ctx context.Context) {
    fmt.Println("SETTING SUB TRANSACTION VIA CONTEXT - default set")
    txn := newrelic.FromContext(ctx)
    SetSubTransaction(txn)
}

func InitRelic() (newrelic.Application, error) {

    fmt.Println("INIT RELIC REBUG")
    if RelicApplication == nil {

        fmt.Println("INIT RELIC = NOT INIT CREATING")
        cfg := newrelic.NewConfig("go-vozyengine-statemachine-service", "29827623658187f9e25fdde2f2fee06da906NRAL")
        cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
        cfg.BrowserMonitoring.Attributes.Enabled = true

        // Enabling distributed tracing will disable the cross application tracing feature. Distributed tracing is
        //  an improved version of cross-application tracing and only one can be enabled at a time.
        cfg.DistributedTracer.Enabled = true

        RelicApplication, err := newrelic.NewApplication(cfg)

        if err != nil {
            log.Fatalf("failed to create newrelic.NewApplication: %v", err)
        }

        fmt.Println("INIT RELIC = RETURNING")

        return RelicApplication, err

    } else {

        fmt.Println("INIT RELIC = ALREADY INIT RETURNING")
        return RelicApplication, nil
    }

}

任何帮助都是感激的,非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-23 22:02:52

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

还可以更改redis事务所使用的上下文。

这里是最后的代码

1-函数用法示例

代码语言:javascript
复制
dbTransaction := newrelic.FromContext(ctx)
    newRelicContext := newrelic.NewContext(context.Background(), dbTransaction)

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

2- Redis Singleton

代码语言:javascript
复制
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单例

代码语言:javascript
复制
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
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72648524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档