Skip to content

Commit 140df6f

Browse files
committed
Update example
1 parent 01d8581 commit 140df6f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

example/otel/client.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strconv"
78
"sync"
89
"time"
910

1011
"github.com/uptrace/uptrace-go/uptrace"
1112
"go.opentelemetry.io/otel"
1213
"go.opentelemetry.io/otel/attribute"
1314
"go.opentelemetry.io/otel/codes"
15+
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
1416

1517
"github.com/redis/go-redis/extra/redisotel/v9"
1618
"github.com/redis/go-redis/v9"
@@ -19,9 +21,14 @@ import (
1921
var tracer = otel.Tracer("github.com/redis/go-redis/example/otel")
2022

2123
func customAttrFn(ctx context.Context) []attribute.KeyValue {
22-
return []attribute.KeyValue{
23-
attribute.String("custom_attr", "custom_value"),
24+
25+
attributes := make([]attribute.KeyValue, 0)
26+
27+
if method, ok := ctx.Value(semconv.RPCMethodKey).(string); ok {
28+
attributes = append(attributes, semconv.RPCMethodKey.String(method))
2429
}
30+
31+
return attributes
2532
}
2633
func main() {
2734
ctx := context.Background()
@@ -48,7 +55,8 @@ func main() {
4855

4956
for i := 0; i < 1e6; i++ {
5057
ctx, rootSpan := tracer.Start(ctx, "handleRequest")
51-
58+
ctx = context.WithValue(ctx, semconv.RPCMethodKey, "handleRequest "+ strconv.Itoa(i))
59+
5260
if err := handleRequest(ctx, rdb); err != nil {
5361
rootSpan.RecordError(err)
5462
rootSpan.SetStatus(codes.Error, err.Error())

0 commit comments

Comments
 (0)