-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support redisv9 * feat: add test for redis * feat: fix muzzle
- Loading branch information
1 parent
c6e58ab
commit 5dc24b5
Showing
19 changed files
with
568 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build ignore | ||
|
||
package goredis | ||
|
||
import ( | ||
redis "github.com/redis/go-redis/v9" | ||
) | ||
|
||
type goRedisRequest struct { | ||
cmd redis.Cmder | ||
endpoint string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build ignore | ||
|
||
package goredis | ||
|
||
import ( | ||
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api-semconv/instrumenter/db" | ||
"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/inst-api/instrumenter" | ||
) | ||
|
||
type goRedisAttrsGetter struct { | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetSystem(request goRedisRequest) string { | ||
return "redis" | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetUser(request goRedisRequest) string { | ||
return "" | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetName(request goRedisRequest) string { | ||
// TODO: parse database name from dsn | ||
return "" | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetConnectionString(request goRedisRequest) string { | ||
return request.endpoint | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetStatement(request goRedisRequest) string { | ||
return request.cmd.String() | ||
} | ||
|
||
func (d goRedisAttrsGetter) GetOperation(request goRedisRequest) string { | ||
return request.cmd.FullName() | ||
} | ||
|
||
func BuildGoRedisOtelInstrumenter() *instrumenter.Instrumenter[goRedisRequest, interface{}] { | ||
builder := instrumenter.Builder[goRedisRequest, interface{}]{} | ||
getter := goRedisAttrsGetter{} | ||
return builder.Init().SetSpanNameExtractor(&db.DBSpanNameExtractor[goRedisRequest]{Getter: getter}).SetSpanKindExtractor(&instrumenter.AlwaysClientExtractor[goRedisRequest]{}). | ||
AddAttributesExtractor(&db.DbClientAttrsExtractor[goRedisRequest, any, goRedisAttrsGetter]{Base: db.DbClientCommonAttrsExtractor[goRedisRequest, any, goRedisAttrsGetter]{Getter: getter}}). | ||
BuildInstrumenter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package goredis | ||
|
||
import "github.com/alibaba/opentelemetry-go-auto-instrumentation/api" | ||
|
||
func init() { | ||
api.NewRule("github.com/redis/go-redis/v9", "NewClient", "", "", "afterNewRedisClient"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
|
||
api.NewRule("github.com/redis/go-redis/v9", "NewFailoverClient", "", "", "afterNewFailOverRedisClient"). | ||
WithVersion("[9.0.5,9.5.2)"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
|
||
api.NewRule("github.com/redis/go-redis/v9", "NewSentinelClient", "", "", "afterNewSentinelClient"). | ||
WithVersion("[9.0.5,9.5.2)"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
|
||
api.NewRule("github.com/redis/go-redis/v9", "Conn", "*Client", "", "afterClientConn"). | ||
WithVersion("[9.0.5,9.5.2)"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
|
||
api.NewRule("github.com/redis/go-redis/v9", "NewClusterClient", "", "", "afterNewClusterClient"). | ||
WithVersion("[9.0.5,9.5.2)"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
|
||
api.NewRule("github.com/redis/go-redis/v9", "NewRing", "", "", "afterNewRingClient"). | ||
WithVersion("[9.0.5,9.5.2)"). | ||
WithFileDeps("goredis_data_type.go", "goredis_otel_instrumenter.go"). | ||
Register() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//go:build ignore | ||
|
||
package goredis | ||
|
||
import ( | ||
"context" | ||
redis "github.com/redis/go-redis/v9" | ||
"net" | ||
"strings" | ||
) | ||
|
||
var goRedisInstrumenter = BuildGoRedisOtelInstrumenter() | ||
|
||
func afterNewRedisClient(call redis.CallContext, client *redis.Client) { | ||
client.AddHook(newOtRedisHook(client.Options().Addr)) | ||
} | ||
|
||
func afterNewFailOverRedisClient(call redis.CallContext, client *redis.Client) { | ||
client.AddHook(newOtRedisHook(client.Options().Addr)) | ||
} | ||
|
||
func afterNewClusterClient(call redis.CallContext, client *redis.ClusterClient) { | ||
client.OnNewNode(func(rdb *redis.Client) { | ||
rdb.AddHook(newOtRedisHook(rdb.Options().Addr)) | ||
}) | ||
} | ||
|
||
func afterNewRingClient(call redis.CallContext, client *redis.Ring) { | ||
client.OnNewNode(func(rdb *redis.Client) { | ||
rdb.AddHook(newOtRedisHook(rdb.Options().Addr)) | ||
}) | ||
} | ||
|
||
func afterNewSentinelClient(call redis.CallContext, client *redis.SentinelClient) { | ||
client.AddHook(newOtRedisHook(client.String())) | ||
} | ||
|
||
func afterClientConn(call redis.CallContext, client *redis.Conn) { | ||
client.AddHook(newOtRedisHook(client.String())) | ||
} | ||
|
||
type otRedisHook struct { | ||
Addr string | ||
} | ||
|
||
func newOtRedisHook(addr string) *otRedisHook { | ||
return &otRedisHook{ | ||
Addr: addr, | ||
} | ||
} | ||
|
||
func (o *otRedisHook) DialHook(next redis.DialHook) redis.DialHook { | ||
return func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
conn, err := next(ctx, network, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return conn, err | ||
} | ||
} | ||
|
||
func (o *otRedisHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { | ||
return func(ctx context.Context, cmd redis.Cmder) error { | ||
if strings.Contains(cmd.FullName(), "ping") || strings.Contains(cmd.FullName(), "PING") { | ||
return next(ctx, cmd) | ||
} | ||
request := goRedisRequest{ | ||
cmd: cmd, | ||
endpoint: o.Addr, | ||
} | ||
ctx = goRedisInstrumenter.Start(ctx, request) | ||
if err := next(ctx, cmd); err != nil { | ||
goRedisInstrumenter.End(ctx, request, nil, err) | ||
return err | ||
} | ||
goRedisInstrumenter.End(ctx, request, nil, nil) | ||
return nil | ||
} | ||
} | ||
|
||
func (o *otRedisHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook { | ||
return func(ctx context.Context, cmds []redis.Cmder) error { | ||
summary := "" | ||
summaryCmds := cmds | ||
if len(summaryCmds) > 10 { | ||
summaryCmds = summaryCmds[:10] | ||
} | ||
for i := range summaryCmds { | ||
summary += summaryCmds[i].FullName() + "/" | ||
} | ||
if len(cmds) > 10 { | ||
summary += "..." | ||
} | ||
cmd := redis.NewCmd(ctx, "pipeline", summary) | ||
request := goRedisRequest{ | ||
cmd: cmd, | ||
endpoint: o.Addr, | ||
} | ||
ctx = goRedisInstrumenter.Start(ctx, request) | ||
if err := next(ctx, cmds); err != nil { | ||
goRedisInstrumenter.End(ctx, request, nil, err) | ||
return err | ||
} | ||
goRedisInstrumenter.End(ctx, request, nil, nil) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module redis/v9.0.5 | ||
|
||
go 1.21 | ||
|
||
replace github.com/alibaba/opentelemetry-go-auto-instrumentation => ../../../../opentelemetry-go-auto-instrumentation | ||
|
||
require ( | ||
github.com/alibaba/opentelemetry-go-auto-instrumentation v0.0.0-00010101000000-000000000000 | ||
github.com/redis/go-redis/v9 v9.0.5 | ||
go.opentelemetry.io/otel/sdk v1.28.0 | ||
) |
Oops, something went wrong.