Skip to content

Commit

Permalink
modified error reporting to avoid double reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
nr-swilloughby committed Jul 8, 2021
1 parent 094273d commit 1ccdb9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 61 deletions.
5 changes: 4 additions & 1 deletion v3/integrations/nrgrpc/example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
sampleapp "github.com/newrelic/go-agent/v3/integrations/nrgrpc/example/sampleapp"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Server is a gRPC server.
Expand All @@ -28,7 +30,8 @@ func processMessage(ctx context.Context, msg *sampleapp.Message) {
// DoUnaryUnary is a unary request, unary response method.
func (s *Server) DoUnaryUnary(ctx context.Context, msg *sampleapp.Message) (*sampleapp.Message, error) {
processMessage(ctx, msg)
return &sampleapp.Message{Text: "Hello from DoUnaryUnary"}, nil
// return &sampleapp.Message{Text: "Hello from DoUnaryUnary"}, nil
return &sampleapp.Message{}, status.New(codes.DataLoss, "oooooops!").Err()
}

// DoUnaryStream is a unary request, stream response method.
Expand Down
14 changes: 10 additions & 4 deletions v3/integrations/nrgrpc/nrgrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@ func OKInterceptorStatusHandler(ctx context.Context, txn *newrelic.Transaction,
// contextual information gleaned from the error value received from the RPC call.
//
func ErrorInterceptorStatusHandler(ctx context.Context, txn *newrelic.Transaction, s *status.Status) {
txn.SetWebResponse(nil).WriteHeader(int(s.Code()))
txn.NoticeError(s.Err())
txn.SetWebResponse(nil).WriteHeader(int(codes.OK))
txn.NoticeError(&newrelic.Error{
Message: s.Message(),
Class: "gRPC Error: " + s.Code().String(),
})
txn.AddAttribute("GrpcStatusLevel", "error")
txn.AddAttribute("GrpcStatusMessage", s.Message())
txn.AddAttribute("GrpcStatusCode", s.Code().String())
}

// examples
Expand All @@ -199,7 +205,7 @@ func WarningInterceptorStatusHandler(ctx context.Context, txn *newrelic.Transact
txn.SetWebResponse(nil).WriteHeader(int(codes.OK))
txn.AddAttribute("GrpcStatusLevel", "warning")
txn.AddAttribute("GrpcStatusMessage", s.Message())
txn.AddAttribute("GrpcStatusCode", s.Code())
txn.AddAttribute("GrpcStatusCode", s.Code().String())
}

//
Expand All @@ -211,7 +217,7 @@ func InfoInterceptorStatusHandler(ctx context.Context, txn *newrelic.Transaction
txn.SetWebResponse(nil).WriteHeader(int(codes.OK))
txn.AddAttribute("GrpcStatusLevel", "info")
txn.AddAttribute("GrpcStatusMessage", s.Message())
txn.AddAttribute("GrpcStatusCode", s.Code())
txn.AddAttribute("GrpcStatusCode", s.Code().String())
}

//
Expand Down
86 changes: 30 additions & 56 deletions v3/integrations/nrgrpc/nrgrpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,23 @@ func TestUnaryServerInterceptorError(t *testing.T) {
"sampled": internal.MatchAnything,
"traceId": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
UserAttributes: map[string]interface{}{
"GrpcStatusMessage": "oooooops!",
"GrpcStatusCode": "DataLoss",
"GrpcStatusLevel": "error",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"httpResponseCode": 0,
"http.statusCode": 0,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryUnaryError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryUnaryError",
},
}})
app.ExpectErrorEvents(t, []internal.WantEvent{{
Intrinsics: map[string]interface{}{
"error.class": "15",
"error.message": "response code 15",
"error.class": "gRPC Error: DataLoss",
"error.message": "oooooops!",
"guid": internal.MatchAnything,
"priority": internal.MatchAnything,
"sampled": internal.MatchAnything,
Expand All @@ -202,36 +206,19 @@ func TestUnaryServerInterceptorError(t *testing.T) {
"transactionName": "WebTransaction/Go/TestApplication/DoUnaryUnaryError",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"httpResponseCode": 0,
"http.statusCode": 0,
"request.headers.User-Agent": internal.MatchAnything,
"request.headers.userAgent": internal.MatchAnything,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryUnaryError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryUnaryError",
},
UserAttributes: map[string]interface{}{},
}, {
Intrinsics: map[string]interface{}{
"error.class": internal.MatchAnything,
"error.message": "rpc error: code = DataLoss desc = oooooops!",
"guid": internal.MatchAnything,
"priority": internal.MatchAnything,
"sampled": internal.MatchAnything,
"spanId": internal.MatchAnything,
"traceId": internal.MatchAnything,
"transactionName": "WebTransaction/Go/TestApplication/DoUnaryUnaryError",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"request.headers.User-Agent": internal.MatchAnything,
"request.headers.userAgent": internal.MatchAnything,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryUnaryError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryUnaryError",
UserAttributes: map[string]interface{}{
"GrpcStatusMessage": "oooooops!",
"GrpcStatusCode": "DataLoss",
"GrpcStatusLevel": "error",
},
UserAttributes: map[string]interface{}{},
}})
}

Expand Down Expand Up @@ -604,19 +591,23 @@ func TestStreamServerInterceptorError(t *testing.T) {
"sampled": internal.MatchAnything,
"traceId": internal.MatchAnything,
},
UserAttributes: map[string]interface{}{},
UserAttributes: map[string]interface{}{
"GrpcStatusLevel": "error",
"GrpcStatusMessage": "oooooops!",
"GrpcStatusCode": "DataLoss",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"httpResponseCode": 0,
"http.statusCode": 0,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryStreamError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryStreamError",
},
}})
app.ExpectErrorEvents(t, []internal.WantEvent{{
Intrinsics: map[string]interface{}{
"error.class": "15",
"error.message": "response code 15",
"error.class": "gRPC Error: DataLoss",
"error.message": "oooooops!",
"guid": internal.MatchAnything,
"priority": internal.MatchAnything,
"sampled": internal.MatchAnything,
Expand All @@ -625,36 +616,19 @@ func TestStreamServerInterceptorError(t *testing.T) {
"transactionName": "WebTransaction/Go/TestApplication/DoUnaryStreamError",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"httpResponseCode": 0,
"http.statusCode": 0,
"request.headers.User-Agent": internal.MatchAnything,
"request.headers.userAgent": internal.MatchAnything,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryStreamError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryStreamError",
},
UserAttributes: map[string]interface{}{},
}, {
Intrinsics: map[string]interface{}{
"error.class": internal.MatchAnything,
"error.message": "rpc error: code = DataLoss desc = oooooops!",
"guid": internal.MatchAnything,
"priority": internal.MatchAnything,
"sampled": internal.MatchAnything,
"spanId": internal.MatchAnything,
"traceId": internal.MatchAnything,
"transactionName": "WebTransaction/Go/TestApplication/DoUnaryStreamError",
},
AgentAttributes: map[string]interface{}{
"httpResponseCode": 15,
"http.statusCode": 15,
"request.headers.User-Agent": internal.MatchAnything,
"request.headers.userAgent": internal.MatchAnything,
"request.headers.contentType": "application/grpc",
"request.method": "TestApplication/DoUnaryStreamError",
"request.uri": "grpc://bufnet/TestApplication/DoUnaryStreamError",
UserAttributes: map[string]interface{}{
"GrpcStatusLevel": "error",
"GrpcStatusMessage": "oooooops!",
"GrpcStatusCode": "DataLoss",
},
UserAttributes: map[string]interface{}{},
}})
}

Expand Down

0 comments on commit 1ccdb9d

Please sign in to comment.