OpenTelemetry

December 18, 2024 ยท View on GitHub

Overview

This SDK supports OpenTelemetry to export 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

Supported Metrics

Metric NameTypeEnabled by DefaultDescription
fga-client.request.durationHistogramYesTotal request time for FGA requests, in milliseconds
fga-client.query.durationHistogramYesTime taken by the FGA server to process and evaluate the request, in milliseconds
fga-client.credentials.requestCounterYesTotal number of new token requests initiated using the Client Credentials flow
fga-client.requestCounterNoTotal number of requests made to the FGA server

Supported Attributes

Attribute NameTypeEnabled by DefaultDescription
fga-client.request.batch_check_sizeintNoThe total size of the check list in a BatchCheck call
fga-client.request.client_idstringYesClient ID associated with the request, if any
fga-client.request.methodstringYesFGA method/action that was performed (e.g., Check, ListObjects) in TitleCase
fga-client.request.model_idstringYesAuthorization model ID that was sent as part of the request, if any
fga-client.request.store_idstringYesStore ID that was sent as part of the request
fga-client.response.model_idstringYesAuthorization model ID that the FGA server used
fga-client.userstringNoUser associated with the action of the request for check and list users
http.client.request.durationintNoDuration for the SDK to complete the request, in milliseconds
http.hoststringYesHost identifier of the origin the request was sent to
http.request.methodstringYesHTTP method for the request
http.request.resend_countintYesNumber of retries attempted, if any
http.response.status_codeintYesStatus code of the response (e.g., 200 for success)
http.server.request.durationintNoTime taken by the FGA server to process and evaluate the request, in milliseconds
url.schemestringYesHTTP scheme of the request (http/https)
url.fullstringYesFull URL of the request
user_agent.originalstringYesUser Agent used in the query

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.

Usage

1. Install Dependencies

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

pip install openfga opentelemetry-sdk

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

pip install opentelemetry-exporter-otlp-proto-grpc

2. Configure OpenTelemetry

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

from opentelemetry import metrics
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
from opentelemetry.sdk.metrics import MeterProvider

# Configure OpenTelemetry
metrics.set_meter_provider(
    MeterProvider(
        resource=Resource(attributes={SERVICE_NAME: "openfga-example"}),
        metric_readers=[PeriodicExportingMetricReader(OTLPMetricExporter())],
    )
)

3. Configure OpenFGA

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

from openfga_sdk.telemetry.configuration import (
    TelemetryConfiguration,
    TelemetryMetricConfiguration,
    TelemetryMetricsConfiguration,
)
from openfga_sdk import ClientConfiguration, OpenFgaClient

configuration = ClientConfiguration(
    api_url=os.getenv("FGA_API_URL"),
    store_id=os.getenv("FGA_STORE_ID"),
    authorization_model_id=os.getenv("FGA_AUTHORIZATION_MODEL_ID"),

    # If you are comfortable with the default configuration outlined in the tables above, you can omit providing your own TelemetryConfiguration object, as one will be created for you.
    telemetry={
        "metrics": {
            "fga-client.request.duration": {
                "fga-client.request.method": True,
                "http.response.status_code": True,
            },
        },
    },
)

fga = OpenFgaClient(configuration)

Example Integration

An example integration is provided that also demonstrates how to configure an application with OpenFGA and OpenTelemetry. Please refer to the README for more information.