Skip to content

Commit

Permalink
Update with sdk-generator changes and fix batch check consistency (#131)
Browse files Browse the repository at this point in the history
* fix: update links to `SECURITY.md`

* fix: update `Stores` links

* chore: update generated files and otel docs

* fix: correctly set consistency on batch check

* docs: update docs table with latest contents

---------

Co-authored-by: vil02 <[email protected]>
  • Loading branch information
ewanharris and vil02 authored Oct 10, 2024
1 parent bd503a9 commit a19a204
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 56 deletions.
25 changes: 25 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ docs/WriteAuthorizationModelResponse.md
docs/WriteRequest.md
docs/WriteRequestDeletes.md
docs/WriteRequestWrites.md
docs/opentelemetry.md
example/Makefile
example/README.md
example/example1/example1.go
example/example1/go.mod
example/opentelemetry/go.mod
example/opentelemetry/main.go
git_push.sh
go.mod
go.sum
Expand Down Expand Up @@ -208,4 +211,26 @@ oauth2/token_test.go
oauth2/transport.go
oauth2/transport_test.go
response.go
telemetry/attribute.go
telemetry/attribute_test.go
telemetry/attributes.go
telemetry/attributes_test.go
telemetry/configuration.go
telemetry/configuration_test.go
telemetry/counter.go
telemetry/counter_test.go
telemetry/counters.go
telemetry/counters_test.go
telemetry/histogram.go
telemetry/histogram_test.go
telemetry/histograms.go
telemetry/histograms_test.go
telemetry/interfaces.go
telemetry/interfaces_test.go
telemetry/metric.go
telemetry/metric_test.go
telemetry/metrics.go
telemetry/metrics_test.go
telemetry/telemetry.go
telemetry/telemetry_test.go
utils.go
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ Please do not open issues for general support or usage questions. Instead, join

### Vulnerability Reporting

Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://github.com/openfga/go-sdk/blob/main/.github/SECURITY.md) details the procedure for disclosing security issues.
Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://github.com/openfga/.github/blob/main/SECURITY.md) details the procedure for disclosing security issues.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ If your server is configured with [authentication enabled](https://openfga.dev/d

Get a paginated list of stores.

[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/ListStores)
[API Documentation](https://openfga.dev/api/service#/Stores/ListStores)

```golang
options := ClientListStoresOptions{
Expand All @@ -248,7 +248,7 @@ stores, err := fgaClient.ListStores(context.Background()).Options(options).Execu

Create and initialize a store.

[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/CreateStore)
[API Documentation](https://openfga.dev/api/service#/Stores/CreateStore)

```golang
body := ClientCreateStoreRequest{Name: "FGA Demo"}
Expand All @@ -269,7 +269,7 @@ fgaClient.SetStoreId(store.Id)

Get information about the current store.

[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/GetStore)
[API Documentation](https://openfga.dev/api/service#/Stores/GetStore)

```golang
options := ClientGetStoreOptions{
Expand All @@ -288,7 +288,7 @@ if err != nil {

Delete a store.

[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/DeleteStore)
[API Documentation](https://openfga.dev/api/service#/Stores/DeleteStore)

```golang
options := ClientDeleteStoreOptions{
Expand Down Expand Up @@ -508,7 +508,7 @@ By default, write runs in a transaction mode where any invalid operation (deleti

```golang
body := ClientWriteRequest{
Writes: []ClientTupleKey{ {
Writes: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand All @@ -517,7 +517,7 @@ body := ClientWriteRequest{
Relation: "viewer",
Object: "document:budget",
} },
Deletes: []ClientTupleKeyWithoutCondition{ {
Deletes: &[]ClientTupleKeyWithoutCondition{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "writer",
Object: "document:roadmap",
Expand All @@ -541,7 +541,7 @@ The SDK will split the writes into separate chunks and send them in separate req

```golang
body := ClientWriteRequest{
Writes: []ClientTupleKey{ {
Writes: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand All @@ -550,7 +550,7 @@ body := ClientWriteRequest{
Relation: "viewer",
Object: "document:budget",
} },
Deletes: []ClientTupleKeyWithoutCondition{ {
Deletes: &[]ClientTupleKeyWithoutCondition{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "writer",
Object: "document:roadmap",
Expand Down Expand Up @@ -602,7 +602,7 @@ body := ClientCheckRequest{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
ContextualTuples: []ClientTupleKey{ {
ContextualTuples: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "editor",
Object: "document:roadmap",
Expand Down Expand Up @@ -640,7 +640,7 @@ body := ClientBatchCheckBody{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
ContextualTuples: []ClientTupleKey{ {
ContextualTuples: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "editor",
Object: "document:roadmap",
Expand All @@ -649,7 +649,7 @@ body := ClientBatchCheckBody{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "admin",
Object: "document:roadmap",
ContextualTuples: []ClientTupleKey{ {
ContextualTuples: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "editor",
Object: "document:roadmap",
Expand Down Expand Up @@ -753,7 +753,7 @@ body := ClientListObjectsRequest{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "can_read",
Type: "document",
ContextualTuples: []ClientTupleKey{ {
ContextualTuples: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "editor",
Object: "folder:product",
Expand Down Expand Up @@ -788,7 +788,7 @@ body := ClientListRelationsRequest{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Object: "document:roadmap",
Relations: []string{"can_view", "can_edit", "can_delete", "can_rename"},
ContextualTuples: []ClientTupleKey{ {
ContextualTuples: &[]ClientTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "editor",
Object: "document:roadmap",
Expand Down Expand Up @@ -1038,7 +1038,7 @@ Class | Method | HTTP request | Description

### OpenTelemetry

This SDK supports producing metrics that can be consumed as part of an [OpenTelemetry](https://opentelemetry.io/) setup. For more information, please see [the documentation](https://github.com/openfga/go-sdk/blob/main/docs/OpenTelemetry.md)
This SDK supports producing metrics that can be consumed as part of an [OpenTelemetry](https://opentelemetry.io/) setup. For more information, please see [the documentation](https://github.com/openfga/go-sdk/blob/main/docs/opentelemetry.md)

## Contributing

Expand Down
20 changes: 13 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2018,17 +2018,23 @@ func (client *OpenFgaClient) BatchCheckExecute(request SdkClientBatchCheckReques
return nil, err
}

checkOptions := &ClientCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
}

if request.GetOptions() != nil && request.GetOptions().Consistency != nil {
checkOptions.Consistency = request.GetOptions().Consistency
}

for index, checkBody := range *request.GetBody() {
index, checkBody := index, checkBody
group.Go(func() error {
singleResponse, err := client.CheckExecute(&SdkClientCheckRequest{
ctx: ctx,
Client: client,
body: &checkBody,
options: &ClientCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
},
ctx: ctx,
Client: client,
body: &checkBody,
options: checkOptions,
})

if _, ok := err.(fgaSdk.FgaApiAuthenticationError); ok {
Expand Down
12 changes: 9 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ func TestOpenFgaClient(t *testing.T) {
options := ClientBatchCheckOptions{
AuthorizationModelId: openfga.PtrString(authModelId),
MaxParallelRequests: openfga.PtrInt32(5),
Consistency: openfga.CONSISTENCYPREFERENCE_UNSPECIFIED.Ptr(),
Consistency: openfga.CONSISTENCYPREFERENCE_HIGHER_CONSISTENCY.Ptr(),
}

var expectedResponse openfga.CheckResponse
Expand All @@ -2213,7 +2213,7 @@ func TestOpenFgaClient(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterMatcherResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, getStoreId(t, fgaClient), test.RequestPath),
httpmock.BodyContainsString(`"consistency":"UNSPECIFIED"`),
httpmock.BodyContainsString(`"consistency":"HIGHER_CONSISTENCY"`),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand All @@ -2223,10 +2223,16 @@ func TestOpenFgaClient(t *testing.T) {
},
)

_, err := fgaClient.BatchCheck(context.Background()).Body(requestBody).Options(options).Execute()
checks, err := fgaClient.BatchCheck(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
t.Fatalf("%v", err)
}

for _, check := range *checks {
if check.Error != nil {
t.Fatalf("a check failed %v", check.Error)
}
}
})

t.Run("Expand", func(t *testing.T) {
Expand Down
80 changes: 77 additions & 3 deletions docs/OpenTelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,82 @@ In cases when metrics events are sent, they will not be viewable outside of infr
| `url.full` | string | Yes | Full URL of the request |
| `user_agent.original` | string | Yes | User Agent used in the query |

## Example
## Customizing Reporting

You can find a basic example integration in the [examples/opentelemetry](../../examples/opentelemetry) directory, which demonstrates how to configure the OpenFGA SDK with OpenTelemetry.
To control which metrics and attributes are reported by the SDK, you can provide your own `TelemetryConfiguration` instance during initialization, as shown in the example above. The `TelemetryConfiguration` class allows you to configure the metrics and attributes that are reported by the SDK, as outlined in [the tables above](#metrics).

Please see [the OpenTelemetry documentation](https://opentelemetry.io/docs/languages/go/) for additional details on how to further configure their SDK for your applications.
## Usage

### 1. Install Dependencies

Install the OpenFGA SDK and OpenTelemetry SDK in your application using `pip`:

```sh
go get "github.com/openfg/go-sdk" \
"go.opentelemetry.io/otel" \
"go.opentelemetry.io/otel/sdk/metric"
```

You must also install an OpenTelemetry exporter; for example, the OTLP gRPC exporter:

```sh
go get "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
```

### 2. Configure OpenTelemetry

Configure your application to use OpenTelemetry, and set up the metrics provider to export metrics using an exporter:

```go
package main

import (
"context"
"errors"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/sdk/metric"
)

// Configure OpenTelemetry
metricExporter, _ := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn))

meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metricExporter)),
sdkmetric.WithResource(res),
)
otel.SetMeterProvider(meterProvider)
defer meterProvider.Shutdown()
```

### 3. Configure OpenFGA

Configure the OpenFGA client, and (optionally) customize what metrics and attributes are reported:

```go
import (
"github.com/openfga/go-sdk/client"
"os"
)

fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiUrl: os.Getenv("FGA_API_URL"), // required, e.g. https://api.fga.example
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId: os.Getenv("FGA_MODEL_ID"), // optional, recommended to be set for production
Telemetry: &telemetry.Configuration{
Metrics: &telemetry.MetricsConfiguration{
METRIC_HISTOGRAM_REQUEST_DURATION: &telemetry.MetricConfiguration{
ATTR_FGA_CLIENT_REQUEST_CLIENT_ID: &telemetry.AttributeConfiguration{Enabled: true},
ATTR_HTTP_RESPONSE_STATUS_CODE: &telemetry.AttributeConfiguration{Enabled: true},
}
}
}
})

```

## Example Integration

An [example integration](../example/opentelemetry) is provided that also demonstrates how to configure an application with OpenFGA and OpenTelemetry. Please refer to [the README](../example/opentelemetry/README.md) for more information.
11 changes: 9 additions & 2 deletions example/example1/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ go 1.22.2

require github.com/openfga/go-sdk v0.6.1

require golang.org/x/sync v0.8.0 // indirect
require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
)

// To reference local build, uncomment below and run `go mod tidy`
replace github.com/openfga/go-sdk v0.6.1 => ../../
// replace github.com/openfga/go-sdk v0.6.1 => ../../
23 changes: 23 additions & 0 deletions example/example1/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/openfga/go-sdk v0.6.1 h1:AlCjX4auM7X9sktHLx9YvFjvU+FoMGuvQ8QkJD627Lo=
github.com/openfga/go-sdk v0.6.1/go.mod h1:zui7pHE3eLAYh2fFmEMrWg9XbxYns2WW5Xr/GEgili4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw=
go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8=
go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2g+8YLc=
go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8=
go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4=
go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 3 additions & 2 deletions example/opentelemetry/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module openfga-opentelemetry-example

go 1.21

replace github.com/openfga/go-sdk => ../..
// To reference local build, uncomment below and run `go mod tidy`
// replace github.com/openfga/go-sdk v0.6.1 => ../../

require (
github.com/joho/godotenv v1.5.1
github.com/openfga/go-sdk v0.0.0-00010101000000-000000000000
github.com/openfga/go-sdk v0.6.1
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0
go.opentelemetry.io/otel/sdk v1.29.0
Expand Down
2 changes: 2 additions & 0 deletions example/opentelemetry/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInw
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/openfga/go-sdk v0.6.1 h1:AlCjX4auM7X9sktHLx9YvFjvU+FoMGuvQ8QkJD627Lo=
github.com/openfga/go-sdk v0.6.1/go.mod h1:zui7pHE3eLAYh2fFmEMrWg9XbxYns2WW5Xr/GEgili4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
Loading

0 comments on commit a19a204

Please sign in to comment.