|
| 1 | +package nroci |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 8 | + "github.com/oracle/oci-go-sdk/common" |
| 9 | + "github.com/oracle/oci-go-sdk/nosql" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +type NoSQLClient interface { |
| 17 | + Query(ctx context.Context, req nosql.QueryRequest) (nosql.QueryResponse, error) |
| 18 | + UpdateRow(ctx context.Context, req nosql.UpdateRowRequest) (nosql.UpdateRowResponse, error) |
| 19 | +} |
| 20 | + |
| 21 | +type NoSQLClientWrapper struct { |
| 22 | + Client NoSQLClient |
| 23 | +} |
| 24 | + |
| 25 | +type NoSQLClientRequestWrapper[R any] struct { |
| 26 | + ClientRequest R |
| 27 | +} |
| 28 | + |
| 29 | +type NoSQLClientResponseWrapper[T any] struct { |
| 30 | + ClientResponse T |
| 31 | +} |
| 32 | + |
| 33 | +func NRNewNoSQLClientWithConfigurationProvider(configProvider common.ConfigurationProvider) (*NoSQLClientWrapper, error) { |
| 34 | + ociNoSQLClient, err := nosql.NewNosqlClientWithConfigurationProvider(configProvider) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + return &NoSQLClientWrapper{ |
| 39 | + Client: ociNoSQLClient, |
| 40 | + }, nil |
| 41 | +} |
| 42 | + |
| 43 | +func extractRequestFieldsOCI(req any) (string, string, string) { |
| 44 | + var collection, statement, compartmentID string |
| 45 | + switch r := req.(type) { |
| 46 | + case nosql.QueryRequest: |
| 47 | + // Could do FROM <table-name> type of parse? |
| 48 | + if r.QueryDetails.CompartmentId != nil { |
| 49 | + compartmentID = *r.QueryDetails.CompartmentId |
| 50 | + } |
| 51 | + if r.Statement != nil { |
| 52 | + statement = *r.Statement |
| 53 | + } |
| 54 | + case nosql.UpdateRowRequest: |
| 55 | + if r.TableNameOrId != nil { |
| 56 | + collection = *r.TableNameOrId |
| 57 | + } |
| 58 | + if r.UpdateRowDetails.CompartmentId != nil { |
| 59 | + compartmentID = *r.UpdateRowDetails.CompartmentId |
| 60 | + } |
| 61 | + default: |
| 62 | + // keep strings empty |
| 63 | + } |
| 64 | + return collection, statement, compartmentID |
| 65 | +} |
| 66 | + |
| 67 | +func executeWithDatastoreSegmentOCI[T any, R any]( |
| 68 | + ctx context.Context, |
| 69 | + rw *NoSQLClientRequestWrapper[R], |
| 70 | + fn func() (T, error), |
| 71 | +) (*NoSQLClientResponseWrapper[T], error) { |
| 72 | + |
| 73 | + txn := newrelic.FromContext(ctx) |
| 74 | + if txn == nil { |
| 75 | + return nil, fmt.Errorf("error executing OCI request, no transaction") |
| 76 | + } |
| 77 | + |
| 78 | + collection, statement, compartmentID := extractRequestFieldsOCI(rw.ClientRequest) |
| 79 | + sgmt := newrelic.DatastoreSegment{ |
| 80 | + StartTime: txn.StartSegmentNow(), |
| 81 | + Product: newrelic.DatastoreOracle, |
| 82 | + ParameterizedQuery: statement, |
| 83 | + Collection: collection, |
| 84 | + DatabaseName: compartmentID, |
| 85 | + } |
| 86 | + res, err := fn() |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("error executing OCI requestL %s", err.Error()) |
| 89 | + } |
| 90 | + responseWrapper := NoSQLClientResponseWrapper[T]{ |
| 91 | + ClientResponse: res, |
| 92 | + } |
| 93 | + |
| 94 | + sgmt.End() |
| 95 | + return &responseWrapper, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (cw *NoSQLClientWrapper) Query(ctx context.Context, req *NoSQLClientRequestWrapper[nosql.QueryRequest]) (*NoSQLClientResponseWrapper[nosql.QueryResponse], error) { |
| 99 | + return executeWithDatastoreSegmentOCI(ctx, req, func() (nosql.QueryResponse, error) { |
| 100 | + return cw.Client.Query(ctx, req.ClientRequest) |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func (cw *NoSQLClientWrapper) UpdateRow(ctx context.Context, req *NoSQLClientRequestWrapper[nosql.UpdateRowRequest]) (*NoSQLClientResponseWrapper[nosql.UpdateRowResponse], error) { |
| 105 | + return executeWithDatastoreSegmentOCI(ctx, req, func() (nosql.UpdateRowResponse, error) { |
| 106 | + return cw.Client.UpdateRow(ctx, req.ClientRequest) |
| 107 | + }) |
| 108 | +} |
0 commit comments