2020-07-15 12:07:07 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-16 09:52:07 +03:00
|
|
|
"sync"
|
|
|
|
|
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-08-12 14:24:39 +03:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
2020-07-15 12:07:07 +03:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2021-09-08 16:00:52 +03:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/extra/redisotel/v8"
|
|
|
|
"github.com/go-redis/redis/v8"
|
2020-07-15 12:07:07 +03:00
|
|
|
)
|
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
var tracer = otel.Tracer("redisexample")
|
|
|
|
|
2020-07-15 12:07:07 +03:00
|
|
|
func main() {
|
2021-02-04 11:38:54 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
stop := configureOpentelemetry(ctx)
|
|
|
|
defer stop()
|
2020-07-15 12:07:07 +03:00
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: ":6379",
|
|
|
|
})
|
2020-11-21 10:56:52 +03:00
|
|
|
rdb.AddHook(redisotel.TracingHook{})
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
ctx, span := tracer.Start(ctx, "handleRequest")
|
|
|
|
defer span.End()
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
if err := handleRequest(ctx); err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 12:07:07 +03:00
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
func handleRequest(ctx context.Context) 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
|
|
|
|
}
|
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-08 15:54:10 +03:00
|
|
|
panic(err)
|
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
|
|
|
}
|
2021-01-16 22:20:03 +03:00
|
|
|
|
2021-09-08 15:54:10 +03:00
|
|
|
func configureOpentelemetry(ctx context.Context) func() {
|
2021-08-12 14:24:39 +03:00
|
|
|
provider := sdktrace.NewTracerProvider()
|
|
|
|
otel.SetTracerProvider(provider)
|
|
|
|
|
|
|
|
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
|
2021-01-16 22:20:03 +03:00
|
|
|
if err != nil {
|
2021-09-08 15:54:10 +03:00
|
|
|
panic(err)
|
2021-01-16 22:20:03 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:24:39 +03:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
|
|
|
provider.RegisterSpanProcessor(bsp)
|
2021-02-04 11:38:54 +03:00
|
|
|
|
2021-08-12 14:24:39 +03:00
|
|
|
return func(ctx context.Context) {
|
|
|
|
if err := provider.Shutdown(ctx); err != nil {
|
2021-09-08 15:54:10 +03:00
|
|
|
panic(err)
|
2021-08-12 14:24:39 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 22:20:03 +03:00
|
|
|
}
|