Skip to content

Commit 0982b38

Browse files
authored
Merge pull request #1882 from go-redis/chore/otel-example
chore: cleanup OpenTelemetry example
2 parents b94bde3 + 9b6ee9c commit 0982b38

37 files changed

+121
-82
lines changed

Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ testdata/redis:
1919
testdata/redis/src/redis-server: testdata/redis
2020
cd $< && make all
2121

22-
tag:
23-
git tag $(VERSION)
24-
git tag extra/rediscmd/$(VERSION)
25-
git tag extra/redisotel/$(VERSION)
26-
git tag extra/rediscensus/$(VERSION)
22+
fmt:
23+
gofmt -w -s ./
24+
goimports -w -local github.com/go-redis/redis ./

cluster_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"sync"
1010
"time"
1111

12-
"github.com/go-redis/redis/v8"
13-
"github.com/go-redis/redis/v8/internal/hashtag"
14-
1512
. "github.com/onsi/ginkgo"
1613
. "github.com/onsi/gomega"
14+
15+
"github.com/go-redis/redis/v8"
16+
"github.com/go-redis/redis/v8/internal/hashtag"
1717
)
1818

1919
type clusterScenario struct {

command_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"errors"
55
"time"
66

7-
redis "github.com/go-redis/redis/v8"
8-
97
. "github.com/onsi/ginkgo"
108
. "github.com/onsi/gomega"
9+
10+
redis "github.com/go-redis/redis/v8"
1111
)
1212

1313
var _ = Describe("Cmd", func() {
@@ -72,7 +72,7 @@ var _ = Describe("Cmd", func() {
7272
})
7373

7474
It("supports time.Time", func() {
75-
tm := time.Date(2019, 01, 01, 9, 45, 10, 222125, time.UTC)
75+
tm := time.Date(2019, 1, 1, 9, 45, 10, 222125, time.UTC)
7676

7777
err := client.Set(ctx, "time_key", tm, 0).Err()
7878
Expect(err).NotTo(HaveOccurred())

commands_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,11 @@ var _ = Describe("Commands", func() {
472472
idleTime := client.ObjectIdleTime(ctx, "key")
473473
Expect(idleTime.Err()).NotTo(HaveOccurred())
474474

475-
//Redis returned milliseconds/1000, which may cause ObjectIdleTime to be at a critical value,
476-
//should be +1s to deal with the critical value problem.
477-
//if too much time (>1s) is used during command execution, it may also cause the test to fail.
478-
//so the ObjectIdleTime result should be <=now-start+1s
479-
//link: https://github.com/redis/redis/blob/5b48d900498c85bbf4772c1d466c214439888115/src/object.c#L1265-L1272
475+
// Redis returned milliseconds/1000, which may cause ObjectIdleTime to be at a critical value,
476+
// should be +1s to deal with the critical value problem.
477+
// if too much time (>1s) is used during command execution, it may also cause the test to fail.
478+
// so the ObjectIdleTime result should be <=now-start+1s
479+
// link: https://github.com/redis/redis/blob/5b48d900498c85bbf4772c1d466c214439888115/src/object.c#L1265-L1272
480480
Expect(idleTime.Val()).To(BeNumerically("<=", time.Now().Sub(start)+time.Second))
481481
})
482482

example/otel/README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
### OpenTelemetry Example
2-
Prints spans and metrics to the console.
3-
#### To Run:
4-
- `docker-compose up -d`
5-
- `go run .`
1+
# Example for go-redis OpenTelemetry instrumentation
62

7-
When you're finished, be sure to run `docker-compose down` to shutdown
8-
the redis server.
3+
This example requires running Redis Server. You can start Redis Server using Docker:
94

5+
```shell
6+
docker-compose up -d
7+
```
8+
9+
To run this example:
10+
11+
```shell
12+
go run .
13+
```
14+
15+
See [Monitoring performance and errors](https://redis.uptrace.dev/guide/tracing.html) for more
16+
details.

example/otel/docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
version: '3'
2+
23
services:
34
redis-server:
45
image: redis
56
ports:
6-
- "6379:6379"
7+
- '6379:6379'
78
redis-cli:
89
image: redis

example/otel/example-client.go renamed to example/otel/main.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,46 @@ package main
22

33
import (
44
"context"
5-
"log"
65
"sync"
7-
"time"
86

9-
"github.com/go-redis/redis/extra/redisotel/v8"
10-
"github.com/go-redis/redis/v8"
117
"go.opentelemetry.io/otel"
8+
"go.opentelemetry.io/otel/codes"
129
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
1310
sdktrace "go.opentelemetry.io/otel/sdk/trace"
11+
12+
"github.com/go-redis/redis/extra/redisotel/v8"
13+
"github.com/go-redis/redis/v8"
1414
)
1515

16+
var tracer = otel.Tracer("redisexample")
17+
1618
func main() {
1719
ctx := context.Background()
1820

19-
stop := runExporter(ctx)
20-
defer stop(ctx)
21+
stop := configureOpentelemetry(ctx)
22+
defer stop()
2123

2224
rdb := redis.NewClient(&redis.Options{
2325
Addr: ":6379",
2426
})
25-
2627
rdb.AddHook(redisotel.TracingHook{})
2728

28-
tracer := otel.Tracer("Example tracer")
29-
ctx, span := tracer.Start(ctx, "start-test-span")
29+
ctx, span := tracer.Start(ctx, "handleRequest")
30+
defer span.End()
3031

31-
rdb.Set(ctx, "First value", "value_1", 0)
32+
if err := handleRequest(ctx); err != nil {
33+
span.RecordError(err)
34+
span.SetStatus(codes.Error, err.Error())
35+
}
36+
}
3237

33-
rdb.Set(ctx, "Second value", "value_2", 0)
38+
func handleRequest(ctx context.Context) error {
39+
if err := rdb.Set(ctx, "First value", "value_1", 0).Err(); err != nil {
40+
return err
41+
}
42+
if err := rdb.Set(ctx, "Second value", "value_2", 0).Err(); err != nil {
43+
return err
44+
}
3445

3546
var group sync.WaitGroup
3647

@@ -40,35 +51,38 @@ func main() {
4051
defer group.Done()
4152
val := rdb.Get(ctx, "Second value").Val()
4253
if val != "value_2" {
43-
log.Fatalf("val was not set. expected: %s but got: %s", "value_2", val)
54+
panic(err)
4455
}
4556
}()
4657
}
58+
4759
group.Wait()
4860

49-
rdb.Del(ctx, "First value")
50-
rdb.Del(ctx, "Second value")
61+
if err := rdb.Del(ctx, "First value").Err(); err != nil {
62+
return err
63+
}
64+
if err := rdb.Del(ctx, "Second value").Err(); err != nil {
65+
return err
66+
}
5167

52-
// Wait some time to allow spans to export
53-
<-time.After(5 * time.Second)
54-
span.End()
68+
return nil
5569
}
5670

57-
func runExporter(ctx context.Context) func(context.Context) {
71+
func configureOpentelemetry(ctx context.Context) func() {
5872
provider := sdktrace.NewTracerProvider()
5973
otel.SetTracerProvider(provider)
6074

6175
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
6276
if err != nil {
63-
log.Fatal(err)
77+
panic(err)
6478
}
6579

6680
bsp := sdktrace.NewBatchSpanProcessor(exp)
6781
provider.RegisterSpanProcessor(bsp)
6882

6983
return func(ctx context.Context) {
7084
if err := provider.Shutdown(ctx); err != nil {
71-
log.Printf("Shutdown failed: %s", err)
85+
panic(err)
7286
}
7387
}
7488
}

example/scan-struct/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/davecgh/go-spew/spew"
7+
78
"github.com/go-redis/redis/v8"
89
)
910

extra/rediscensus/rediscensus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package rediscensus
33
import (
44
"context"
55

6+
"go.opencensus.io/trace"
7+
68
"github.com/go-redis/redis/extra/rediscmd/v8"
79
"github.com/go-redis/redis/v8"
8-
"go.opencensus.io/trace"
910
)
1011

1112
type TracingHook struct{}

extra/rediscmd/safe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build appengine
12
// +build appengine
23

34
package rediscmd

0 commit comments

Comments
 (0)