OpenTelemetry

October 10, 2024 ยท View on GitHub

This SDK produces metrics using OpenTelemetry 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 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

Supported Attributes

Attribute NameTypeEnabled by DefaultDescription
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:

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:

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:

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:

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 is provided that also demonstrates how to configure an application with OpenFGA and OpenTelemetry. Please refer to the README for more information.