Skip to content

Commit b9ca262

Browse files
committed
feat: Initial OCI Wrapper/Queries
1 parent de8b2e1 commit b9ca262

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/user"
8+
"path/filepath"
9+
"time"
10+
11+
"github.com/newrelic/go-agent/v3/integrations/nroci"
12+
"github.com/newrelic/go-agent/v3/newrelic"
13+
"github.com/oracle/oci-go-sdk/common"
14+
"github.com/oracle/oci-go-sdk/nosql"
15+
)
16+
17+
func main() {
18+
app, err := newrelic.NewApplication(
19+
newrelic.ConfigAppName("Basic NOSQLOCI App"),
20+
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
21+
newrelic.ConfigDebugLogger(os.Stdout),
22+
)
23+
if err != nil {
24+
panic(err)
25+
}
26+
app.WaitForConnection(10 * time.Second) // for short lived processes in apps
27+
defer app.Shutdown(10 * time.Second)
28+
29+
usr, _ := user.Current()
30+
configPath := filepath.Join(usr.HomeDir, ".oci/config")
31+
configProvider, err := common.ConfigurationProviderFromFile(configPath, "")
32+
33+
configWrapper, err := nroci.NRNewNoSQLClientWithConfigurationProvider(configProvider)
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
txn := app.StartTransaction("OCI NoSQL Transaction")
39+
40+
ctx := newrelic.NewContext(context.Background(), txn)
41+
42+
compartmentID := os.Getenv("COMPARTMENT_OCID")
43+
statement := "SELECT * FROM audienceData1"
44+
tableName := "audienceData1"
45+
46+
putReq := nosql.UpdateRowRequest{
47+
TableNameOrId: &tableName,
48+
UpdateRowDetails: nosql.UpdateRowDetails{
49+
Value: map[string]interface{}{
50+
"cookie_id": 123,
51+
"audience_data": map[string]interface{}{
52+
"ipaddr": "10.0.0.3",
53+
"audience_segment": map[string]interface{}{
54+
"sports_lover": "2018-11-30",
55+
"book_reader": "2018-12-01",
56+
},
57+
},
58+
},
59+
CompartmentId: &compartmentID,
60+
},
61+
}
62+
queryReq := nosql.QueryRequest{
63+
QueryDetails: nosql.QueryDetails{
64+
CompartmentId: &compartmentID,
65+
Statement: &statement,
66+
},
67+
}
68+
putRes, err := configWrapper.UpdateRow(ctx, &nroci.NoSQLClientRequestWrapper[nosql.UpdateRowRequest]{
69+
ClientRequest: putReq,
70+
})
71+
if err != nil {
72+
panic(err)
73+
}
74+
fmt.Printf("UpdateRow row: %v\nresult\n", putRes)
75+
76+
queryRes, err := configWrapper.Query(ctx, &nroci.NoSQLClientRequestWrapper[nosql.QueryRequest]{
77+
ClientRequest: queryReq,
78+
})
79+
80+
if err != nil {
81+
panic(err)
82+
}
83+
fmt.Printf("QueryRow row: %v\nresult\n", queryRes)
84+
85+
txn.End()
86+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package nroci

0 commit comments

Comments
 (0)