Skip to content

Commit 2e9d8aa

Browse files
authored
Merge pull request #243 from coroot/grpc_status
grpc: get grpc-status from payload if possible
2 parents c71a416 + da8b5a4 commit 2e9d8aa

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

containers/container.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,12 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R
741741
requests := conn.http2Parser.Parse(r.Method, r.Payload, uint64(r.Duration))
742742
for _, req := range requests {
743743
if !common.HttpFilter.ShouldBeSkipped(req.Path) {
744-
stats.observe(req.Status.Http(), "", req.Duration)
745-
trace.Http2Request(req.Method, req.Path, req.Scheme, req.Status, req.Duration)
744+
status := req.Status.Http()
745+
if req.GrpcStatus >= 0 {
746+
status = req.GrpcStatus.GRPC()
747+
}
748+
stats.observe(status, "", req.Duration)
749+
trace.Http2Request(req.Method, req.Path, req.Scheme, req.Status, req.GrpcStatus, req.Duration)
746750
}
747751
}
748752
case l7.ProtocolPostgres:

ebpftracer/l7/http2.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ type Http2FrameHeader struct {
2424
}
2525

2626
type Http2Request struct {
27-
Method string
28-
Path string
29-
Scheme string
30-
Status Status
31-
Duration time.Duration
27+
Method string
28+
Path string
29+
Scheme string
30+
Status Status
31+
GrpcStatus Status
32+
Duration time.Duration
3233

3334
kernelTime uint64
3435
}
@@ -61,6 +62,8 @@ func (p *Http2Parser) Parse(method Method, payload []byte, kernelTime uint64) []
6162

6263
var decoder *hpack.Decoder
6364
statuses := map[uint32]Status{}
65+
grpcStatuses := map[uint32]Status{}
66+
6467
offset := 0
6568

6669
switch method {
@@ -119,9 +122,13 @@ func (p *Http2Parser) Parse(method Method, payload []byte, kernelTime uint64) []
119122
statuses[h.StreamId] = 0
120123
}
121124
decoder.SetEmitFunc(func(hf hpack.HeaderField) {
122-
if hf.Name == ":status" {
125+
switch hf.Name {
126+
case ":status":
123127
s, _ := strconv.Atoi(hf.Value)
124128
statuses[h.StreamId] = Status(s)
129+
case "grpc-status":
130+
s, _ := strconv.Atoi(hf.Value)
131+
grpcStatuses[h.StreamId] = Status(s)
125132
}
126133
})
127134
}
@@ -141,6 +148,12 @@ func (p *Http2Parser) Parse(method Method, payload []byte, kernelTime uint64) []
141148
continue
142149
}
143150
r.Status = status
151+
grpcStatus, ok := grpcStatuses[streamId]
152+
if ok {
153+
r.GrpcStatus = grpcStatus
154+
} else {
155+
r.GrpcStatus = -1
156+
}
144157
r.Duration = time.Duration(kernelTime - r.kernelTime)
145158
res = append(res, *r)
146159
delete(p.activeRequests, streamId)

ebpftracer/l7/l7.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,46 @@ func (s Status) Zookeeper() string {
160160
return "ok"
161161
}
162162

163+
func (s Status) GRPC() string {
164+
switch s {
165+
case 0:
166+
return "grpc:OK"
167+
case 1:
168+
return "grpc:CANCELLED"
169+
case 2:
170+
return "grpc:UNKNOWN"
171+
case 3:
172+
return "grpc:INVALID_ARGUMENT"
173+
case 4:
174+
return "grpc:DEADLINE_EXCEEDED"
175+
case 5:
176+
return "grpc:NOT_FOUND"
177+
case 6:
178+
return "grpc:ALREADY_EXISTS"
179+
case 7:
180+
return "grpc:PERMISSION_DENIED"
181+
case 8:
182+
return "grpc:RESOURCE_EXHAUSTED"
183+
case 9:
184+
return "grpc:FAILED_PRECONDITION"
185+
case 10:
186+
return "grpc:ABORTED"
187+
case 11:
188+
return "grpc:OUT_OF_RANGE"
189+
case 12:
190+
return "grpc:UNIMPLEMENTED"
191+
case 13:
192+
return "grpc:INTERNAL"
193+
case 14:
194+
return "grpc:UNAVAILABLE"
195+
case 15:
196+
return "grpc:DATA_LOSS"
197+
case 16:
198+
return "grpc:UNAUTHENTICATED"
199+
}
200+
return ""
201+
}
202+
163203
func (s Status) Error() bool {
164204
return s == StatusFailed
165205
}

tracing/tracing.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time
151151
)
152152
}
153153

154-
func (t *Trace) Http2Request(method, path, scheme string, status l7.Status, duration time.Duration) {
154+
func (t *Trace) Http2Request(method, path, scheme string, status, grpcStatus l7.Status, duration time.Duration) {
155155
if t == nil {
156156
return
157157
}
@@ -164,11 +164,16 @@ func (t *Trace) Http2Request(method, path, scheme string, status l7.Status, dura
164164
if scheme == "" {
165165
scheme = "unknown"
166166
}
167-
t.createSpan(method, duration, status > 400,
167+
168+
attrs := []attribute.KeyValue{
168169
semconv.HTTPURL(fmt.Sprintf("%s://%s%s", scheme, t.destination.String(), path)),
169170
semconv.HTTPMethod(method),
170171
semconv.HTTPStatusCode(int(status)),
171-
)
172+
}
173+
if grpcStatus >= 0 {
174+
attrs = append(attrs, semconv.RPCGRPCStatusCodeKey.Int(int(grpcStatus)))
175+
}
176+
t.createSpan(method, duration, status > 400 || grpcStatus > 0, attrs...)
172177
}
173178

174179
func (t *Trace) PostgresQuery(query string, error bool, duration time.Duration) {

0 commit comments

Comments
 (0)