-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for tracking Redis client calls for non-Go programs (#891)
- Loading branch information
Showing
95 changed files
with
1,145 additions
and
140 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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,149 @@ | ||
package ebpfcommon | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
trace2 "go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/grafana/beyla/pkg/internal/request" | ||
) | ||
|
||
const minRedisFrameLen = 3 | ||
|
||
func isRedis(buf []uint8) bool { | ||
if len(buf) < minRedisFrameLen { | ||
return false | ||
} | ||
|
||
return isRedisOp(buf) | ||
} | ||
|
||
// nolint:cyclop | ||
func isRedisOp(buf []uint8) bool { | ||
if len(buf) == 0 { | ||
return false | ||
} | ||
c := buf[0] | ||
|
||
switch c { | ||
case '+': | ||
return crlfTerminatedMatch(buf[1:], func(c uint8) bool { | ||
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == ' ' || c == '-' || c == '_' | ||
}) | ||
case '-': | ||
return isRedisError(buf[1:]) | ||
case ':', '$', '*': | ||
return crlfTerminatedMatch(buf[1:], func(c uint8) bool { | ||
return (c >= '0' && c <= '9') | ||
}) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func isRedisError(buf []uint8) bool { | ||
return bytes.HasPrefix(buf, []byte("ERR ")) || | ||
bytes.HasPrefix(buf, []byte("WRONGTYPE ")) || | ||
bytes.HasPrefix(buf, []byte("MOVED ")) || | ||
bytes.HasPrefix(buf, []byte("ASK ")) || | ||
bytes.HasPrefix(buf, []byte("BUSY ")) || | ||
bytes.HasPrefix(buf, []byte("NOSCRIPT ")) || | ||
bytes.HasPrefix(buf, []byte("CLUSTERDOWN ")) | ||
} | ||
|
||
func crlfTerminatedMatch(buf []uint8, matches func(c uint8) bool) bool { | ||
cr := false | ||
i := 0 | ||
for ; i < len(buf); i++ { | ||
c := buf[i] | ||
if matches(c) { | ||
continue | ||
} | ||
if c == '\r' { | ||
cr = true | ||
break | ||
} | ||
|
||
return false | ||
} | ||
|
||
if !cr || i >= len(buf)-1 { | ||
return false | ||
} | ||
|
||
return buf[i+1] == '\n' | ||
} | ||
|
||
func parseRedisRequest(buf string) (string, string, bool) { | ||
lines := strings.Split(buf, "\r\n") | ||
|
||
if len(lines) < 2 { | ||
return "", "", false | ||
} | ||
|
||
// It's not a command, something else? | ||
if lines[0][0] != '*' { | ||
return "", "", true | ||
} | ||
|
||
op := "" | ||
text := "" | ||
|
||
read := false | ||
// Skip the first line | ||
for _, l := range lines[1:] { | ||
if len(l) == 0 { | ||
continue | ||
} | ||
if !read { | ||
if isRedisOp([]uint8(l + "\r\n")) { | ||
read = true | ||
} else { | ||
break | ||
} | ||
} else { | ||
if op == "" { | ||
op = l | ||
} | ||
text += l + " " | ||
read = false | ||
} | ||
} | ||
|
||
return op, text, true | ||
} | ||
|
||
func TCPToRedisToSpan(trace *TCPRequestInfo, op, text string, status int) request.Span { | ||
peer := "" | ||
hostname := "" | ||
hostPort := 0 | ||
|
||
if trace.ConnInfo.S_port != 0 || trace.ConnInfo.D_port != 0 { | ||
peer, hostname = trace.reqHostInfo() | ||
hostPort = int(trace.ConnInfo.D_port) | ||
} | ||
|
||
return request.Span{ | ||
Type: request.EventTypeRedisClient, | ||
Method: op, | ||
Path: text, | ||
Peer: peer, | ||
Host: hostname, | ||
HostPort: hostPort, | ||
ContentLength: 0, | ||
RequestStart: int64(trace.StartMonotimeNs), | ||
Start: int64(trace.StartMonotimeNs), | ||
End: int64(trace.EndMonotimeNs), | ||
Status: status, | ||
TraceID: trace2.TraceID(trace.Tp.TraceId), | ||
SpanID: trace2.SpanID(trace.Tp.SpanId), | ||
ParentSpanID: trace2.SpanID(trace.Tp.ParentId), | ||
Flags: trace.Tp.Flags, | ||
Pid: request.PidInfo{ | ||
HostPID: trace.Pid.HostPid, | ||
UserPID: trace.Pid.UserPid, | ||
Namespace: trace.Pid.Ns, | ||
}, | ||
} | ||
} |
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,29 @@ | ||
package ebpfcommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type crlfTest struct { | ||
testStr string | ||
result bool | ||
} | ||
|
||
func TestCRLFMatching(t *testing.T) { | ||
for _, ts := range []crlfTest{ | ||
{testStr: "Not a sql or any known protocol", result: false}, | ||
{testStr: "Not a sql or any known protocol\r\n", result: true}, | ||
{testStr: "123\r\n", result: false}, | ||
{testStr: "\r\n", result: true}, | ||
{testStr: "\n", result: false}, | ||
{testStr: "\r", result: false}, | ||
{testStr: "", result: false}, | ||
} { | ||
res := crlfTerminatedMatch([]uint8(ts.testStr), func(c uint8) bool { | ||
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == ' ' || c == '-' || c == '_' | ||
}) | ||
assert.Equal(t, res, ts.result) | ||
} | ||
} |
Oops, something went wrong.