openfga / dotnet-sdk

OpenFGA SDK for .NET - https://www.nuget.org/packages/OpenFga.Sdk
https://openfga.dev
Apache License 2.0
49 stars 7 forks source link

feat: add support for exporting metrics #69

Closed rhamzeh closed 2 months ago

rhamzeh commented 4 months ago

Description

Introduces OpenTelemetry metrics reporting into the SDK for specific actions. We're intentionally starting with only a few metric events that we can then grow over time.

We don't have any formal documentation for this currently. We'll write this as we roll it out across all SDKs, so here is a summary of the events and the associated attributes:

Metrics

Supported Metrics

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

Supported attributes

Attribute Name Type Enabled by Default Description
fga-client.response.model_id string Yes The authorization model ID that the FGA server used
fga-client.request.method string Yes The FGA method/action that was performed (e.g. Check, ListObjects, ...) in TitleCase
fga-client.request.store_id string Yes The store ID that was sent as part of the request
fga-client.request.model_id string Yes The authorization model ID that was sent as part of the request, if any
fga-client.request.client_id string Yes The client ID associated with the request, if any
fga-client.user string No The user that is associated with the action of the request for check and list objects
http.request.resend_count int Yes The number of retries attempted (Only sent if the request was retried. Count of 1 means the request was retried once in addition to the original request)
http.response.status_code int Yes The status code of the response
http.request.method string No The HTTP method for the request
http.host string Yes Host identifier of the origin the request was sent to
url.scheme string No HTTP Scheme of the request (http/https)
url.full string No Full URL of the request
user_agent.original string Yes User Agent used in the query

Testing

Spin up Prometheus, build the project and then run the OpenTelemetryExample app.

You should see metrics in Prometheus such as:

fga_client_query_duration_bucket{exported_instance="28790af8-2116-4e47-baaf-319f9f23f518", exported_job=""openfga-otel-dotnet-example"", fga_client_request_client_id="3DSvMhkJwjoSBMTliPEdPjybbVewfVes", fga_client_request_method="Check", fga_client_request_model_id="01J41WCA3KBA9SP2M350FPDEJ0", fga_client_request_store_id="01J40DW2J9NNYNNVEC430S3Z97", fga_client_response_model_id="01J41WCA3KBA9SP2M350FPDEJ0", fga_client_user="user:anne", http_client_request_duration="40", http_host="api.us1.fga.dev", http_request_method="POST", http_response_status_code="200", http_server_request_duration="2", instance="otel-collector:8889", job="otel-collector", label1="value1", le="5", url_full="https://api.us1.fga.dev/stores/01J40DW2J9NNYNNVEC430S3Z97/check", url_scheme="https", user_agent_original="openfga-sdk dotnet/0.4.0"}
---
fga_client_query_duration_bucket{exported_instance="fbee572d-0bff-4957-95a2-c532fe7312c1", exported_job=""openfga-otel-dotnet-example"", fga_client_request_client_id="3DSvMhkJwjoSBMTliPEdPjybbVewfVes", fga_client_request_method="WriteAuthorizationModel", fga_client_request_store_id="01J40DW2J9NNYNNVEC430S3Z97", http_client_request_duration="139", http_host="api.us1.fga.dev", http_request_method="POST", http_request_resend_count="1", http_response_status_code="201", http_server_request_duration="69", instance="otel-collector:8889", job="otel-collector", label1="value1", le="75", url_full="https://api.us1.fga.dev/stores/01J40DW2J9NNYNNVEC430S3Z97/authorization-models", url_scheme="https", user_agent_original="openfga-sdk dotnet/0.4.0"}
---
fga_client_query_duration_bucket{exported_instance="fbee572d-0bff-4957-95a2-c532fe7312c1", exported_job=""openfga-otel-dotnet-example"", fga_client_request_client_id="3DSvMhkJwjoSBMTliPEdPjybbVewfVes", fga_client_request_method="Write", fga_client_request_model_id="01J41WCA3KBA9SP2M350FPDEJ0", fga_client_request_store_id="01J40DW2J9NNYNNVEC430S3Z97", fga_client_response_model_id="01J41WCA3KBA9SP2M350FPDEJ0", http_client_request_duration="68", http_host="api.us1.fga.dev", http_request_method="POST", http_request_resend_count="1", http_response_status_code="200", http_server_request_duration="30", instance="otel-collector:8889", job="otel-collector", label1="value1", le="5000", url_full="https://api.us1.fga.dev/stores/01J40DW2J9NNYNNVEC430S3Z97/write", url_scheme="https", user_agent_original="openfga-sdk dotnet/0.4.0"}

Also for client credential exchange, you should see metrics such as:

{__name__="fga_client_credentials_request", exported_instance="28790af8-2116-4e47-baaf-319f9f23f518", exported_job=""openfga-otel-dotnet-example"", fga_client_request_client_id="3DSvMhkJwjoSBMTliPEdPjybbVewfVes", fga_client_request_method="ClientCredentialsExchange", http_client_request_duration="1461", http_host="fga.us.auth0.com", http_request_method="POST", http_response_status_code="200", instance="otel-collector:8889", job="otel-collector", label1="value1", url_full="https://fga.us.auth0.com/oauth/token", url_scheme="https", user_agent_original="openfga-sdk dotnet/0.4.0"}

References

Closes #68

Follows the implementations in JS (https://github.com/openfga/js-sdk/pull/117), Python (https://github.com/openfga/python-sdk/pull/107) and Java (https://github.com/openfga/java-sdk/pull/94)

Review Checklist

codecov-commenter commented 4 months ago

Codecov Report

Attention: Patch coverage is 73.87580% with 122 lines in your changes missing coverage. Please review.

Project coverage is 33.01%. Comparing base (f76e9eb) to head (a3489ed).

Files with missing lines Patch % Lines
src/OpenFga.Sdk/Telemetry/Attributes.cs 65.18% 47 Missing and 8 partials :warning:
src/OpenFga.Sdk/Configuration/TelemetryConfig.cs 67.30% 17 Missing :warning:
src/OpenFga.Sdk/Telemetry/Metrics.cs 72.72% 5 Missing and 7 partials :warning:
src/OpenFga.Sdk/ApiClient/OAuth2Client.cs 80.00% 2 Missing and 6 partials :warning:
src/OpenFga.Sdk/Telemetry/Meters.cs 33.33% 8 Missing :warning:
src/OpenFga.Sdk/ApiClient/RequestBuilder.cs 65.00% 3 Missing and 4 partials :warning:
src/OpenFga.Sdk/Telemetry/Histograms.cs 61.11% 6 Missing and 1 partial :warning:
src/OpenFga.Sdk/ApiClient/BaseClient.cs 81.25% 1 Missing and 2 partials :warning:
src/OpenFga.Sdk/ApiClient/ApiClient.cs 93.93% 1 Missing and 1 partial :warning:
src/OpenFga.Sdk/Configuration/Configuration.cs 83.33% 0 Missing and 2 partials :warning:
... and 1 more
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #69 +/- ## ========================================== + Coverage 31.15% 33.01% +1.86% ========================================== Files 122 128 +6 Lines 6083 6388 +305 Branches 785 840 +55 ========================================== + Hits 1895 2109 +214 - Misses 4030 4099 +69 - Partials 158 180 +22 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.