当前位置:   article > 正文

golang集成sentry: go-redis

golang集成sentry: go-redis

网上没有找到go-redis集成sentry的库, 所以我简单实现了一个

代码: https://github.com/Shujie-Tan/go-redis-sentry
使用方法:

import (
	redis_sentry "github.com/Shujie-Tan/go-redis-sentry"
)
rdb := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "",
		DB:       0,
	})
hook := redis_sentry.NewHook(rdb)
rdb.AddHook(hook)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

示例如下

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	redis_sentry "github.com/Shujie-Tan/go-redis-sentry"
	"github.com/getsentry/sentry-go"
	"github.com/go-redis/redis/v8"
)

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Debug:              true,
		Dsn:                "https://a5eac4fa3396cbfac8fb4baa6a9c03a3@o4504291071688704.ingest.sentry.io/4506715873804288",
		AttachStacktrace:   true,
		EnableTracing:      true,
		SampleRate:         1.0,
		TracesSampleRate:   1.0,
		ProfilesSampleRate: 1.0,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %s", err)
	}
	defer sentry.Flush(2 * time.Second)

	ctx := context.Background()
	rdb := initRedis(ctx)
	testSimpleCmd(rdb)
	testCmdPipeline(rdb)
	sentry.CaptureMessage("test")

}

func testSimpleCmd(rdb *redis.Client) {
	ctx := context.Background()
	tx := sentry.StartTransaction(ctx, "test_signle_cmd")
	defer tx.Finish()

	ctx = tx.Context()

	status := rdb.Set(ctx, "a", 1, 2*time.Second)
	// assert ctx
	fmt.Printf("status %v\n", status)
}

func testCmdPipeline(rdb *redis.Client) {
	ctx := context.Background()
	tx := sentry.StartTransaction(ctx, "test_pipeline")
	defer tx.Finish()
	ctx = tx.Context()

	pipe := rdb.Pipeline()

	incr := pipe.Incr(ctx, "pipeline_counter")
	pipe.Expire(ctx, "pipeline_counter", time.Hour)

	cmds, err := pipe.Exec(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Printf("cmds %v\n", cmds)

	// The value is available only after Exec is called.
	fmt.Println(incr.Val())
}

func initRedis(ctx context.Context) *redis.Client {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		Password: "",
		DB:       0,
	})

	pong, err := rdb.Ping(ctx).Result()
	if err != nil {
		fmt.Printf("connect to redis error")
	}
	fmt.Printf("connect to redis success, pong is %v\n", pong)

	hook := redis_sentry.NewHook(rdb)

	rdb.AddHook(hook)

	return rdb

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

效果如下图
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/102414
推荐阅读
相关标签
  

闽ICP备14008679号