@@ -10,10 +10,31 @@ import (
10
10
"net/http"
11
11
)
12
12
13
+ // https://grpc.github.io/grpc/core/md_doc_statuscodes.html
14
+ const (
15
+ GrpcOk = "0"
16
+ GrpcCancelled = "1"
17
+ GrpcUnknown = "2"
18
+ GrpcInvalidArgument = "3"
19
+ GrpcDeadlineExceeded = "4"
20
+ GrpcNotFound = "5"
21
+ GrpcAlreadyExist = "6"
22
+ GrpcPermissionDenied = "7"
23
+ GrpcResourceExhausted = "8"
24
+ GrpcFailedPrecondition = "9"
25
+ GrpcAbort = "10"
26
+ GrpcOutOfRange = "11"
27
+ GrpcUnimplemented = "12"
28
+ GrpcInternal = "13"
29
+ GrpcUnavailable = "14"
30
+ GrpcDataLoss = "15"
31
+ GrpcUnauthenticated = "16"
32
+ )
33
+
13
34
func getProtobufData (r * http.Request ) ([]byte , error ) {
14
35
reqBody , err := io .ReadAll (r .Body )
15
36
if err != nil {
16
- return nil , & httpserver.ErrorWithStatusCode {StatusCode : http .StatusBadRequest , Err : fmt .Errorf ("cannot read request body: %s" , err )}
37
+ return nil , & httpserver.ErrorWithStatusCode {StatusCode : http .StatusInternalServerError , Err : fmt .Errorf ("cannot read request body: %s" , err )}
17
38
}
18
39
// +--------+-------------------------------------------------+
19
40
// | 1 byte | 4 bytes |
@@ -26,6 +47,7 @@ func getProtobufData(r *http.Request) ([]byte, error) {
26
47
// | (variable length) |
27
48
// | |
28
49
// +----------------------------------------------------------+
50
+ // See https://grpc.github.io/grpc/core/md_doc__p_r_o_t_o_c_o_l-_h_t_t_p2.html
29
51
if len (reqBody ) < 5 {
30
52
return nil , & httpserver.ErrorWithStatusCode {StatusCode : http .StatusBadRequest , Err : fmt .Errorf ("invalid grpc header length: %d" , len (reqBody ))}
31
53
}
@@ -40,17 +62,27 @@ func getProtobufData(r *http.Request) ([]byte, error) {
40
62
return reqBody [5 :], nil
41
63
}
42
64
65
+ func writeErrorGrpcResponse (w http.ResponseWriter , grpcErrorCode , grpcErrorMessage string ) {
66
+ w .Header ().Set ("Content-Type" , "application/grpc+proto" )
67
+ w .Header ().Set ("Trailer" , "grpc-status, grpc-message" )
68
+ w .Header ().Set ("Grpc-Status" , grpcErrorCode )
69
+ w .Header ().Set ("Grpc-Message" , grpcErrorMessage )
70
+ }
71
+
43
72
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#message-encoding
44
- func writeExportTraceResponses (w http.ResponseWriter , rejectedSpans int64 , errorMessage string ) {
73
+ func writeExportTracesGrpcResponse (w http.ResponseWriter , rejectedSpans int64 , errorMessage string ) {
45
74
resp := pb.ExportTraceServiceResponse {
46
75
ExportTracePartialSuccess : pb.ExportTracePartialSuccess {
47
76
RejectedSpans : rejectedSpans ,
48
77
ErrorMessage : errorMessage ,
49
78
},
50
79
}
51
80
respData := resp .MarshalProtobuf (nil )
81
+
52
82
grpcRespData := make ([]byte , 5 + len (respData ))
83
+ // Compressed flag
53
84
grpcRespData [0 ] = 0
85
+ // Message Length
54
86
binary .BigEndian .PutUint32 (grpcRespData [1 :5 ], uint32 (len (respData )))
55
87
copy (grpcRespData [5 :], respData )
56
88
w .Header ().Set ("Content-Type" , "application/grpc+proto" )
@@ -61,12 +93,14 @@ func writeExportTraceResponses(w http.ResponseWriter, rejectedSpans int64, error
61
93
logger .Errorf ("unexpected write of %d bytes in replying OLTP export grpc request, expected:%d" , writtenLen , len (grpcRespData ))
62
94
return
63
95
}
96
+ grpcStatus := GrpcOk
97
+
64
98
if err != nil {
65
- logger .Errorf ("failed to reply OLTP export grpc request , error:%s" , err )
66
- return
99
+ grpcStatus = GrpcInternal
100
+ grpcErrorMessage := fmt .Sprintf ("failed to reply OLTP export grpc request , error:%s" , err )
101
+ logger .Errorf (grpcErrorMessage )
102
+ w .Header ().Set ("Grpc-Message" , grpcErrorMessage )
67
103
}
68
104
69
- w .Header ().Set ("Grpc-Status" , "0" )
70
- w .Header ().Set ("Grpc-Message" , "" )
71
-
105
+ w .Header ().Set ("Grpc-Status" , grpcStatus )
72
106
}
0 commit comments