2020-07-15 12:07:07 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-16 11:11:54 +03:00
|
|
|
"fmt"
|
2021-09-23 16:31:21 +03:00
|
|
|
"log"
|
2020-07-16 09:52:07 +03:00
|
|
|
"sync"
|
2022-10-16 11:11:54 +03:00
|
|
|
"time"
|
2020-07-16 09:52:07 +03:00
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
"github.com/uptrace/uptrace-go/uptrace"
|
2020-11-21 10:56:52 +03:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-09-08 15:54:10 +03:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2021-09-08 16:00:52 +03:00
|
|
|
|
2022-06-04 17:39:21 +03:00
|
|
|
"github.com/go-redis/redis/extra/redisotel/v9"
|
|
|
|
"github.com/go-redis/redis/v9"
|
2020-07-15 12:07:07 +03:00
|
|
|
)
|
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
var tracer = otel.Tracer("github.com/go-redis/redis/example/otel")
|
2021-09-08 15:54:10 +03:00
|
|
|
|
2020-07-15 12:07:07 +03:00
|
|
|
func main() {
|
2021-02-04 11:38:54 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
uptrace.ConfigureOpentelemetry(
|
|
|
|
// copy your project DSN here or use UPTRACE_DSN env var
|
|
|
|
// uptrace.WithDSN("https://AQDan_E_EPe3QAF9fMP0PiVr5UWOu4q5@uptrace.dev/1"),
|
|
|
|
|
|
|
|
uptrace.WithServiceName("myservice"),
|
|
|
|
uptrace.WithServiceVersion("v1.0.0"),
|
|
|
|
)
|
|
|
|
defer uptrace.Shutdown(ctx)
|
2020-07-15 12:07:07 +03:00
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2022-10-11 15:37:34 +03:00
|
|
|
if err := redisotel.InstrumentTracing(rdb); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-10-16 11:11:54 +03:00
|
|
|
if err := redisotel.InstrumentMetrics(rdb); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
for i := 0; i < 1e6; i++ {
|
|
|
|
ctx, rootSpan := tracer.Start(ctx, "handleRequest")
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
if err := handleRequest(ctx, rdb); err != nil {
|
|
|
|
rootSpan.RecordError(err)
|
|
|
|
rootSpan.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
2021-11-05 17:17:17 +03:00
|
|
|
|
2022-10-16 11:11:54 +03:00
|
|
|
rootSpan.End()
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
fmt.Printf("view trace: %s\n", uptrace.TraceURL(rootSpan))
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2021-09-08 15:54:10 +03:00
|
|
|
}
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2021-09-23 16:31:21 +03:00
|
|
|
func handleRequest(ctx context.Context, rdb *redis.Client) error {
|
2021-09-08 15:54:10 +03:00
|
|
|
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
|
|
|
|
}
|
2020-07-15 12:07:07 +03:00
|
|
|
|
|
|
|
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" {
|
2021-09-23 16:31:21 +03:00
|
|
|
log.Printf("%q != %q", val, "value_2")
|
2020-07-15 12:07:07 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-09-08 15:54:10 +03:00
|
|
|
|
2020-07-15 12:07:07 +03:00
|
|
|
group.Wait()
|
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
if err := rdb.Del(ctx, "First value").Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := rdb.Del(ctx, "Second value").Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
return nil
|
2020-07-15 12:07:07 +03:00
|
|
|
}
|