forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
91 lines (72 loc) · 1.81 KB
/
client.go
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
91
package main
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/uptrace/uptrace-go/uptrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
)
var tracer = otel.Tracer("github.com/redis/go-redis/example/otel")
func main() {
ctx := context.Background()
uptrace.ConfigureOpentelemetry(
// copy your project DSN here or use UPTRACE_DSN env var
uptrace.WithDSN("http://project2_secret_token@localhost:14317/2"),
uptrace.WithServiceName("myservice"),
uptrace.WithServiceVersion("v1.0.0"),
)
defer uptrace.Shutdown(ctx)
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
if err := redisotel.InstrumentTracing(rdb); err != nil {
panic(err)
}
if err := redisotel.InstrumentMetrics(rdb); err != nil {
panic(err)
}
for i := 0; i < 1e6; i++ {
ctx, rootSpan := tracer.Start(ctx, "handleRequest")
if err := handleRequest(ctx, rdb); err != nil {
rootSpan.RecordError(err)
rootSpan.SetStatus(codes.Error, err.Error())
}
rootSpan.End()
if i == 0 {
fmt.Printf("view trace: %s\n", uptrace.TraceURL(rootSpan))
}
time.Sleep(time.Second)
}
}
func handleRequest(ctx context.Context, rdb *redis.Client) error {
if err := rdb.Set(ctx, "First value", "value_1", 0).Err(); err != nil {
return err
}
if err := rdb.Set(ctx, "Second value", "value_2", 0).Err(); err != nil {
return err
}
var group sync.WaitGroup
for i := 0; i < 20; i++ {
group.Add(1)
go func() {
defer group.Done()
val := rdb.Get(ctx, "Second value").Val()
if val != "value_2" {
log.Printf("%q != %q", val, "value_2")
}
}()
}
group.Wait()
if err := rdb.Del(ctx, "First value").Err(); err != nil {
return err
}
if err := rdb.Del(ctx, "Second value").Err(); err != nil {
return err
}
return nil
}