Skip to content

Commit

Permalink
feat: add support for exporting metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Jul 23, 2024
1 parent d6e3d12 commit 31f63c1
Show file tree
Hide file tree
Showing 14 changed files with 923 additions and 146 deletions.
9 changes: 8 additions & 1 deletion .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
.github/ISSUE_TEMPLATE/config.yaml
.github/ISSUE_TEMPLATE/feature_request.yaml
.github/dependabot.yaml
.github/workflows/main.yaml
.github/workflows/semgrep.yaml
.gitignore
CHANGELOG.md
CONTRIBUTING.md
LICENSE
NOTICE.txt
OpenFga.Sdk.sln
OpenTelemetry.md
README.md
VERSION.txt
assets/FGAIcon.png
Expand Down Expand Up @@ -99,6 +99,9 @@ docs/WriteRequestWrites.md
example/Example1/Example1.cs
example/Example1/Example1.csproj
example/Makefile
example/OpenTelemetryExample/.env.example
example/OpenTelemetryExample/OpenTelemetryExample.cs
example/OpenTelemetryExample/OpenTelemetryExample.csproj
example/README.md
src/OpenFga.Sdk.Test/Api/OpenFgaApiTests.cs
src/OpenFga.Sdk.Test/Client/OpenFgaClientTests.cs
Expand Down Expand Up @@ -244,3 +247,7 @@ src/OpenFga.Sdk/Model/WriteRequest.cs
src/OpenFga.Sdk/Model/WriteRequestDeletes.cs
src/OpenFga.Sdk/Model/WriteRequestWrites.cs
src/OpenFga.Sdk/OpenFga.Sdk.csproj
src/OpenFga.Sdk/Telemetry/Attributes.cs
src/OpenFga.Sdk/Telemetry/Counters.cs
src/OpenFga.Sdk/Telemetry/Histograms.cs
src/OpenFga.Sdk/Telemetry/Metrics.cs
96 changes: 96 additions & 0 deletions OpenTelemetry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# 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.

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.

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.

## Metrics

### Supported Metrics

| Metric Name | Type | Description |
|---------------------------------|-----------|--------------------------------------------------------------------------------------|
| `fga-client.request.duration` | Histogram | The total request time for FGA requests |
| `fga-client.query.duration` | Histogram | The amount of time the FGA server took to internally process nd evaluate the request |
|` fga-client.credentials.request`| Counter | The total number of times a new token was requested when using ClientCredentials |

### Supported attributes

| Attribute Name | Type | Description |
|--------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------|
| `fga-client.response.model_id` | `string` | The authorization model ID that the FGA server used |
| `fga-client.request.method` | `string` | The FGA method/action that was performed (e.g. `check`, `listObjects`, ...) in camelCase |
| `fga-client.request.store_id` | `string` | The store ID that was sent as part of the request |
| `fga-client.request.model_id` | `string` | The authorization model ID that was sent as part of the request, if any |
| `fga-client.request.client_id` | `string` | The client ID associated with the request, if any |
| `fga-client.user` | `string` | The user that is associated with the action of the request for check and list objects |
| `fga-client.request.retries` | `int` | The number of retries attempted (starting from 1 for the original request). Deprecated, use `http.request.resend_count` |
| `http.request.resend_count` | `int` | The number of retries attempted (starting from 1 for the original request) |
| `http.status_code` | `int` | The status code of the response. Deprecated, use `http.response.status_code` |
| `http.response.status_code` | `int` | The status code of the response |
| `http.method` | `string` | The HTTP method for the request. Deprecated, use `http.request.method` |
| `http.request.method` | `string` | The HTTP method for the request |
| `http.host` | `string` | Host identifier of the origin the request was sent to |
| `url.scheme` | `string` | HTTP Scheme of the request (`http`/`https`) |
| `url.full` | `string` | Full URL of the request |
| `user_agent.original` | `string` | User Agent used in the query |
| `http.client.request.duration` | `int` | The total request time for FGA requests |
| `http.server.request.duration` | `int` | The amount of time the FGA server took to internally process nd evaluate the request |

## Configuration

See the OpenTelemetry docs on [Customizing the SDK](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/metrics/customizing-the-sdk/README.md).

```csharp
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Client.Model;
using OpenFga.Sdk.Model;
using OpenFga.Sdk.Telemetry;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using System.Diagnostics;

namespace Example {
public class Example {
public static async Task Main() {
try {
// Setup OpenTelemetry Metrics
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation() // To instrument the default http client
.AddMeter(Metrics.name) // .AddMeter("OpenFga.Sdk") also works
.ConfigureResource(resourceBuilder => resourceBuilder.AddService("openfga-dotnet-example"))
.AddOtlpExporter() // Required to export to an OTLP compatible endpoint
.AddConsoleExporter() // Only needed to export the metrics to the console (e.g. when debugging)
.Build();

// Configure the OpenFGA SDK
var configuration = new ClientConfiguration() {
ApiUrl = Environment.GetEnvironmentVariable("FGA_API_URL") ?? "http://localhost:8080", // required, e.g. https://api.fga.example
StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_MODEL_ID"), // Optional, can be overridden per request
// Credentials = ... // If needed
};
var fgaClient = new OpenFgaClient(configuration);

// Call the SDK normally
var response = await fgaClient.ReadAuthorizationModels();
} catch (ApiException e) {
Debug.Print("Error: "+ e);
}
}
}
}
```

### More Resources
* [OpenTelemetry.Instrumentation.Http](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Instrumentation.Http/README.md) for instrumenting the HttpClient.
* If you are using .NET 8+, checkout the built-in metrics.

A number of these metrics are baked into .NET 8+ as well:

## Example

There is an [example project](https://github.com/openfga/dotnet-sdk/blob/main/example/OpenTelemetryExample) that provides some guidance on how to configure OpenTelemetry available in the examples directory.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This is an autogenerated SDK for OpenFGA. It provides a wrapper around the [Open
- [Retries](#retries)
- [API Endpoints](#api-endpoints)
- [Models](#models)
- [OpenTelemetry](#opentelemetry)
- [Contributing](#contributing)
- [Issues](#issues)
- [Pull Requests](#pull-requests)
Expand Down Expand Up @@ -927,6 +928,10 @@ namespace Example {



### 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/dotnet-sdk/blob/main/OpenTelemetry.md)

## Contributing

### Issues
Expand Down
11 changes: 11 additions & 0 deletions example/OpenTelemetryExample/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Configuration for OpenFGA
FGA_CLIENT_ID=
FGA_API_TOKEN_ISSUER=
FGA_API_AUDIENCE=
FGA_CLIENT_SECRET=
FGA_STORE_ID=
FGA_AUTHORIZATION_MODEL_ID=
FGA_API_URL="http://localhost:8080"

# Configuration for OpenTelemetry
OTEL_SERVICE_NAME="openfga-otel-dotnet-example"
Loading

0 comments on commit 31f63c1

Please sign in to comment.