Skip to content

Commit

Permalink
feat(rueidis): add auto instrumentation for github.com/redis/rueidis.
Browse files Browse the repository at this point in the history
  • Loading branch information
hossein-raeisi committed Jan 11, 2025
1 parent e6f3f3c commit 3d25b75
Show file tree
Hide file tree
Showing 21 changed files with 2,659 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ FROM --platform=$BUILDPLATFORM golang:1.23.4-bookworm@sha256:2e838582004fab0931

RUN apt-get update && apt-get install -y curl clang gcc llvm make libbpf-dev

ARG GOPROXY
ENV GOPROXY=$GOPROXY

WORKDIR /app

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ license-header-check:
exit 1; \
fi

.PHONY: fixture-nethttp fixture-gin fixture-databasesql fixture-nethttp-custom fixture-otelglobal fixture-autosdk fixture-kafka-go
.PHONY: fixture-nethttp fixture-gin fixture-databasesql fixture-nethttp-custom fixture-otelglobal fixture-autosdk fixture-kafka-go fixture-rueidis
fixture-nethttp-custom: fixtures/nethttp_custom
fixture-nethttp: fixtures/nethttp
fixture-gin: fixtures/gin
Expand All @@ -168,6 +168,7 @@ fixture-grpc: fixtures/grpc
fixture-otelglobal: fixtures/otelglobal
fixture-autosdk: fixtures/autosdk
fixture-kafka-go: fixtures/kafka-go
fixture-rueidis: fixtures/rueidis
fixtures/%: LIBRARY=$*
fixtures/%:
$(MAKE) docker-build
Expand Down
1 change: 1 addition & 0 deletions examples/httpPlusdb/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
depends_on:
- jaeger
build:
no_cache: true
context: .
dockerfile: ./Dockerfile
pid: "host"
Expand Down
7 changes: 7 additions & 0 deletions examples/rueidis/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.23.4@sha256:7ea4c9dcb2b97ff8ee80a67db3d44f98c8ffa0d191399197007d8459c1453041
WORKDIR /app
COPY ./*.go .
RUN go mod init main
RUN go mod tidy
RUN go build -o main
ENTRYPOINT ["/app/main"]
20 changes: 20 additions & 0 deletions examples/rueidis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Example of Auto instrumentation of HTTP server

Server exposes an endpoint `/do.` When we hit the endpoint, it sets a random value for `test_key` in redis and then
retrieves it using rueidis client.

For testing auto instrumentation, we can use the docker compose.

To run the example, bring up the services using the command.

```
docker compose up --build
```

Now, you can hit roll dice server using the below command
```
curl localhost:8081/do
```
Every hit to the server should generate a trace that we can observe in [Jaeger UI](http://localhost:16686/)

Example trace ![Image](jaeger.png)
52 changes: 52 additions & 0 deletions examples/rueidis/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
networks:
default:
name: rueidis
driver: bridge

services:
redis:
image: redis:7.2

app:
depends_on:
- jaeger
- redis
build:
context: .
dockerfile: ./Dockerfile
pid: "host"
ports:
- "8081:8081"
volumes:
- /proc:/host/proc

go-auto:
depends_on:
- app
build:
context: ../..
dockerfile: Dockerfile
privileged: true
pid: "host"
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318
- OTEL_GO_AUTO_TARGET_EXE=/app/main
- OTEL_SERVICE_NAME=rueidis-app
- OTEL_PROPAGATORS=tracecontext,baggage
volumes:
- /proc:/host/proc

jaeger:
image: jaegertracing/all-in-one:1.60@sha256:4fd2d70fa347d6a47e79fcb06b1c177e6079f92cba88b083153d56263082135e
ports:
- "16686:16686"
- "14268:14268"
environment:
- COLLECTOR_OTLP_ENABLED=true
- LOG_LEVEL=debug
- JAEGER_DISABLED=true
deploy:
resources:
limits:
memory: 300M
restart: unless-stopped
7 changes: 7 additions & 0 deletions examples/rueidis/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module go.opentelemetry.io/auto/examples/rueidis

go 1.20

require github.com/redis/rueidis v1.0.40

require golang.org/x/sys v0.19.0 // indirect
9 changes: 9 additions & 0 deletions examples/rueidis/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/redis/rueidis v1.0.40 h1:zoC+GUTdNHhP7ZHrnMiIDcP16DUEVcxsPThQsvD7yDg=
github.com/redis/rueidis v1.0.40/go.mod h1:bnbkk4+CkXZgDPEbUtSos/o55i4RhFYYesJ4DS2zmq0=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Binary file added examples/rueidis/jaeger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions examples/rueidis/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"

"github.com/redis/rueidis"
)

const testKey = "test_key"

type server struct {
client rueidis.Client
}

func (s *server) Do(w http.ResponseWriter, req *http.Request) {
randomValue := fmt.Sprintf("random_value_%d", rand.Intn(1000))
err := setKey(req.Context(), s.client, testKey, randomValue)
if err != nil {
fmt.Println(fmt.Errorf("failed to set key: %v", err))
}
fmt.Printf("Set key '%s' with value: %s\n", testKey, randomValue)

value, err := getKey(req.Context(), s.client, testKey)
if err != nil {
fmt.Println(fmt.Errorf("failed to get key: %v", err))
}
fmt.Printf("Got value for key '%s': %s\n", testKey, value)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_ = json.NewEncoder(w).Encode(map[string]string{"value": value})
}

func main() {
address := "redis:6379"
fmt.Println("Connecting to redis server...")
fmt.Println(address)

client, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{address},
SelectDB: 0,
})
if err != nil {
log.Fatalf("Failed to create Redis client: %v", err)
}
defer client.Close()

s := &server{client: client}

http.HandleFunc("/do", s.Do)
port := ":8081"
fmt.Printf("Server starting on port %s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}

func setKey(ctx context.Context, client rueidis.Client, key, value string) error {
cmd := client.B().Set().Key(key).Value(value).Build()
return client.Do(ctx, cmd).Error()
}

func getKey(ctx context.Context, client rueidis.Client, key string) (string, error) {
cmd := client.B().Get().Key(key).Build()
result := client.Do(ctx, cmd)
if result.Error() != nil {
return "", result.Error()
}
return result.ToString()
}
Loading

0 comments on commit 3d25b75

Please sign in to comment.