Skip to content

Commit

Permalink
chore: update generated files and otel docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Oct 9, 2024
1 parent 5ff21d0 commit 0a690fc
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 34 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
99 changes: 92 additions & 7 deletions docs/OpenTelemetry.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# OpenTelemetry

This SDK produces [metrics](https://opentelemetry.io/docs/concepts/signals/metrics/) using [OpenTelemetry](https://opentelemetry.io/) that allow you to view data such as request timings. These metrics also include attributes for the model and store ID, as well as the API called to allow you to build reporting.
- [Overview](#overview)
- [Metrics](#metrics)
- [Supported Metrics](#supported-metrics)
- [Supported Attributes](#supported-attributes)
- [Customizing Reporting](#customizing-reporting)
- [Usage](#usage)
- [Installation](#1-install-dependencies)
- [Configure OpenTelemetry](#2-configure-opentelemetry)
- [Configure OpenFGA](#3-configure-openfga)
- [Example Integration](#example-integration)

When an OpenTelemetry SDK instance is configured, the metrics will be exported and sent to the collector configured as part of your applications configuration. If you are not using OpenTelemetry, the metric functionality is a no-op and the events are never sent.
## Overview

In cases when metrics events are sent, they will not be viewable outside of infrastructure configured in your application, and are never available to the OpenFGA team or contributors.
This SDK supports [OpenTelemetry](https://opentelemetry.io/) to export [metrics](https://opentelemetry.io/docs/concepts/signals/metrics/) that provide insights into your application's performance, such as request timings. These metrics include attributes like model and store IDs, and the API called, which you can use to build detailed reports and dashboards.

If you configure the OpenTelemetry SDK, these metrics will be exported and sent to a collector as specified in your application's configuration. If OpenTelemetry is not configured, metrics functionality is disabled, and no events are sent.

## Metrics

Expand All @@ -31,13 +42,87 @@ In cases when metrics events are sent, they will not be viewable outside of infr
| `http.request.method` | string | Yes | HTTP method for the request |
| `http.request.resend_count` | int | Yes | Number of retries attempted, if any |
| `http.response.status_code` | int | Yes | Status code of the response (e.g., `200` for success) |
| `http.server.request.duration` | int | No | Time taken by the FGA server to process and evaluate the request, in milliseconds |
| `http.server.request.duration` | int | Yes | Time taken by the FGA server to process and evaluate the request, in milliseconds |
| `url.scheme` | string | Yes | HTTP scheme of the request (`http`/`https`) |
| `url.full` | string | Yes | Full URL of the request |
| `user_agent.original` | string | Yes | User Agent used in the query |

## Example
## Customizing Reporting

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).

## 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},
}
}
}
})

```

You can find a basic example integration in the [examples/opentelemetry](../../examples/opentelemetry) directory, which demonstrates how to configure the OpenFGA SDK with OpenTelemetry.
## Example Integration

Please see [the OpenTelemetry documentation](https://opentelemetry.io/docs/languages/go/) for additional details on how to further configure their SDK for your applications.
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
46 changes: 23 additions & 23 deletions telemetry/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package telemetry

/*
CheckRequestTupleKeyInterface is a simplified interface that defines the methods the CheckRequestTupleKey struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestTupleKeyInterface interface {
GetUser() *string
}

/*
CheckRequestInterface is a simplified interface that defines the methods the CheckRequest struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestInterface interface {
GetTupleKey() CheckRequestTupleKeyInterface
RequestAuthorizationModelIdInterface
}

/*
RequestAuthorizationModelIdInterface is a generic interface that defines the GetAuthorizationModelId() method a Request struct implements, relevant to the context of the telemetry package.
*/
type RequestAuthorizationModelIdInterface interface {
GetAuthorizationModelId() *string
}
package telemetry

/*
CheckRequestTupleKeyInterface is a simplified interface that defines the methods the CheckRequestTupleKey struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestTupleKeyInterface interface {
GetUser() *string
}

/*
CheckRequestInterface is a simplified interface that defines the methods the CheckRequest struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestInterface interface {
GetTupleKey() CheckRequestTupleKeyInterface
RequestAuthorizationModelIdInterface
}

/*
RequestAuthorizationModelIdInterface is a generic interface that defines the GetAuthorizationModelId() method a Request struct implements, relevant to the context of the telemetry package.
*/
type RequestAuthorizationModelIdInterface interface {
GetAuthorizationModelId() *string
}

0 comments on commit 0a690fc

Please sign in to comment.