chore: cleanup OpenTelemetry example

This commit is contained in:
Vladimir Mihailenco 2021-09-08 15:54:10 +03:00
parent b94bde306e
commit 11c4d6f556
4 changed files with 49 additions and 27 deletions

View File

@ -1,9 +1,16 @@
### OpenTelemetry Example # Example for go-redis OpenTelemetry instrumentation
Prints spans and metrics to the console.
#### To Run:
- `docker-compose up -d`
- `go run .`
When you're finished, be sure to run `docker-compose down` to shutdown This example requires running Redis Server. You can start Redis Server using Docker:
the redis server.
```shell
docker-compose up -d
```
To run this example:
```shell
go run .
```
See [Monitoring performance and errors](https://redis.uptrace.dev/guide/tracing.html) for more
details.

View File

@ -1,8 +1,9 @@
version: '3' version: '3'
services: services:
redis-server: redis-server:
image: redis image: redis
ports: ports:
- "6379:6379" - '6379:6379'
redis-cli: redis-cli:
image: redis image: redis

View File

@ -2,35 +2,45 @@ package main
import ( import (
"context" "context"
"log"
"sync" "sync"
"time"
"github.com/go-redis/redis/extra/redisotel/v8" "github.com/go-redis/redis/extra/redisotel/v8"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
) )
var tracer = otel.Tracer("redisexample")
func main() { func main() {
ctx := context.Background() ctx := context.Background()
stop := runExporter(ctx) stop := configureOpentelemetry(ctx)
defer stop(ctx) defer stop()
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(&redis.Options{
Addr: ":6379", Addr: ":6379",
}) })
rdb.AddHook(redisotel.TracingHook{}) rdb.AddHook(redisotel.TracingHook{})
tracer := otel.Tracer("Example tracer") ctx, span := tracer.Start(ctx, "handleRequest")
ctx, span := tracer.Start(ctx, "start-test-span") defer span.End()
rdb.Set(ctx, "First value", "value_1", 0) if err := handleRequest(ctx); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}
rdb.Set(ctx, "Second value", "value_2", 0) 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
}
var group sync.WaitGroup var group sync.WaitGroup
@ -40,27 +50,30 @@ func main() {
defer group.Done() defer group.Done()
val := rdb.Get(ctx, "Second value").Val() val := rdb.Get(ctx, "Second value").Val()
if val != "value_2" { if val != "value_2" {
log.Fatalf("val was not set. expected: %s but got: %s", "value_2", val) panic(err)
} }
}() }()
} }
group.Wait() group.Wait()
rdb.Del(ctx, "First value") if err := rdb.Del(ctx, "First value").Err(); err != nil {
rdb.Del(ctx, "Second value") return err
}
if err := rdb.Del(ctx, "Second value").Err(); err != nil {
return err
}
// Wait some time to allow spans to export return nil
<-time.After(5 * time.Second)
span.End()
} }
func runExporter(ctx context.Context) func(context.Context) { func configureOpentelemetry(ctx context.Context) func() {
provider := sdktrace.NewTracerProvider() provider := sdktrace.NewTracerProvider()
otel.SetTracerProvider(provider) otel.SetTracerProvider(provider)
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
bsp := sdktrace.NewBatchSpanProcessor(exp) bsp := sdktrace.NewBatchSpanProcessor(exp)
@ -68,7 +81,7 @@ func runExporter(ctx context.Context) func(context.Context) {
return func(ctx context.Context) { return func(ctx context.Context) {
if err := provider.Shutdown(ctx); err != nil { if err := provider.Shutdown(ctx); err != nil {
log.Printf("Shutdown failed: %s", err) panic(err)
} }
} }
} }

3
tx.go
View File

@ -13,7 +13,8 @@ const TxFailedErr = proto.RedisError("redis: transaction failed")
// Tx implements Redis transactions as described in // Tx implements Redis transactions as described in
// http://redis.io/topics/transactions. It's NOT safe for concurrent use // http://redis.io/topics/transactions. It's NOT safe for concurrent use
// by multiple goroutines, because Exec resets list of watched keys. // by multiple goroutines, because Exec resets list of watched keys.
// If you don't need WATCH it is better to use Pipeline. //
// If you don't need WATCH, use Pipeline instead.
type Tx struct { type Tx struct {
baseClient baseClient
cmdable cmdable