Skip to content

Commit

Permalink
Merge pull request #757 from k2io/dev
Browse files Browse the repository at this point in the history
 Issue fixes and few new Implementations for nrsecurityagent
  • Loading branch information
nr-swilloughby authored Jul 31, 2023
2 parents a61a47a + 9169e00 commit 1f1c2dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions v3/integrations/nrgrpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
github.com/newrelic/go-agent/v3 v3.23.0
// v1.15.0 is the earliest version of grpc using modules.
google.golang.org/grpc v1.54.0
google.golang.org/protobuf v1.28.1
)
replace github.com/newrelic/go-agent/v3 => ../..
32 changes: 28 additions & 4 deletions v3/integrations/nrgrpc/nrgrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ import (
"net/http"
"strings"

protoV1 "github.com/golang/protobuf/proto"
"github.com/newrelic/go-agent/v3/newrelic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
protoV2 "google.golang.org/protobuf/proto"
)

func startTransaction(ctx context.Context, app *newrelic.Application, fullMethod string) *newrelic.Transaction {
Expand Down Expand Up @@ -309,7 +311,11 @@ func UnaryServerInterceptor(app *newrelic.Application, options ...HandlerOption)

return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
txn := startTransaction(ctx, app, info.FullMethod)
newrelic.GetSecurityAgentInterface().SendEvent("GRPC", req)

if newrelic.IsSecurityAgentPresent() {
messageType, version := getMessageType(req)
newrelic.GetSecurityAgentInterface().SendEvent("GRPC", req, messageType, version)
}
defer txn.End()

ctx = newrelic.NewContext(ctx, txn)
Expand All @@ -330,7 +336,10 @@ func (s wrappedServerStream) Context() context.Context {
}

func (s wrappedServerStream) RecvMsg(msg any) error {
newrelic.GetSecurityAgentInterface().SendEvent("GRPC", msg)
if newrelic.IsSecurityAgentPresent() {
messageType, version := getMessageType(msg)
newrelic.GetSecurityAgentInterface().SendEvent("GRPC", msg, messageType, version)
}
return s.ServerStream.RecvMsg(msg)
}

Expand All @@ -350,7 +359,6 @@ func newWrappedServerStream(stream grpc.ServerStream, txn *newrelic.Transaction)
// streaming calls.
//
// See the notes and examples for the UnaryServerInterceptor function.
//
func StreamServerInterceptor(app *newrelic.Application, options ...HandlerOption) grpc.StreamServerInterceptor {
if app == nil {
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
Expand All @@ -369,9 +377,25 @@ func StreamServerInterceptor(app *newrelic.Application, options ...HandlerOption
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
txn := startTransaction(ss.Context(), app, info.FullMethod)
defer txn.End()

newrelic.GetSecurityAgentInterface().SendEvent("GRPC_INFO", info.IsClientStream, info.IsServerStream)
err := handler(srv, newWrappedServerStream(ss, txn))
reportInterceptorStatus(ss.Context(), txn, localHandlerMap, err)
return err
}
}

func getMessageType(req any) (string, string) {
messageType := ""
version := "v2"
messagev2, ok := req.(protoV2.Message)
if ok {
messageType = string(messagev2.ProtoReflect().Descriptor().FullName())
} else {
messagev1, ok := req.(protoV1.Message)
if ok {
messageType = string(protoV1.MessageReflect(messagev1).Descriptor().FullName())
version = "v1"
}
}
return messageType, version
}
2 changes: 1 addition & 1 deletion v3/integrations/nrsecurityagent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/newrelic/go-agent/v3/integrations/nrsecurityagent
go 1.19

require (
github.com/newrelic/csec-go-agent v0.2.1
github.com/newrelic/csec-go-agent v0.3.0
github.com/newrelic/go-agent/v3 v3.23.0
github.com/newrelic/go-agent/v3/integrations/nrsqlite3 v1.2.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
3 changes: 3 additions & 0 deletions v3/newrelic/secure_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (app *Application) RegisterSecurityAgent(s securityAgent) {
if app != nil && app.app != nil && s != nil {
secureAgent = s
}
if app.app.run != nil {
secureAgent.RefreshState(getLinkedMetaData(app.app))
}
}

func getLinkedMetaData(app *app) map[string]string {
Expand Down

0 comments on commit 1f1c2dc

Please sign in to comment.