|
| 1 | +# OpenTelemetry Example for OpenFGA Java SDK |
| 2 | + |
| 3 | +This example demonstrates two approaches for using OpenTelemetry metrics with the OpenFGA Java SDK: |
| 4 | + |
| 5 | +1. **Manual Configuration** (`./gradlew run`) - Code-based OpenTelemetry setup |
| 6 | +2. **Java Agent** (`./gradlew runWithAgent`) - Zero-code automatic instrumentation |
| 7 | + |
| 8 | +Both approaches generate the same metrics: |
| 9 | +- `fga-client.request.duration` - Total request time for FGA requests |
| 10 | +- `fga-client.query.duration` - Time taken by FGA server to process requests |
| 11 | +- `fga-client.credentials.request` - Number of token requests (if using client credentials) |
| 12 | + |
| 13 | +## SDK Version Configuration |
| 14 | + |
| 15 | +**By default**, this example uses a published version of the OpenFGA Java SDK. |
| 16 | + |
| 17 | +If you're contributing to the SDK or testing unreleased features: |
| 18 | + |
| 19 | +1. **Enable local SDK** in `settings.gradle`: |
| 20 | + ```gradle |
| 21 | + // Uncomment this line: |
| 22 | + includeBuild '../..' |
| 23 | + ``` |
| 24 | + |
| 25 | +2. **Update dependency** in `build.gradle`: |
| 26 | + ```gradle |
| 27 | + // Comment out the versioned dependency: |
| 28 | + // implementation("dev.openfga:openfga-sdk:$fgaSdkVersion") |
| 29 | + |
| 30 | + // Uncomment the local dependency: |
| 31 | + implementation("dev.openfga:openfga-sdk") |
| 32 | + ``` |
| 33 | + |
| 34 | +3. **Build the main SDK first** (from repository root): |
| 35 | + ```bash |
| 36 | + cd ../.. |
| 37 | + ./gradlew build |
| 38 | + cd examples/opentelemetry |
| 39 | + ``` |
| 40 | + |
| 41 | +## Prerequisites |
| 42 | + |
| 43 | +- Java 11 or higher |
| 44 | +- Docker and Docker Compose |
| 45 | +- OpenFGA server running (or use the provided docker-compose setup) |
| 46 | + |
| 47 | +## Quick Start |
| 48 | + |
| 49 | +### 1. Start the OpenTelemetry Stack |
| 50 | + |
| 51 | +```bash |
| 52 | +# Clone the OpenTelemetry Collector setup |
| 53 | +git clone https://github.com/ewanharris/opentelemetry-collector-dev-setup.git otel-collector |
| 54 | +cd otel-collector |
| 55 | + |
| 56 | +# Start the services |
| 57 | +docker-compose up -d |
| 58 | +``` |
| 59 | + |
| 60 | +This provides: |
| 61 | +- **Jaeger** at http://localhost:16686 - Distributed tracing UI |
| 62 | +- **Prometheus** at http://localhost:9090 - Metrics collection and querying |
| 63 | +- **Grafana** at http://localhost:3001 - Metrics visualization (admin:admin) |
| 64 | + |
| 65 | +### 2. Configure OpenFGA Connection |
| 66 | + |
| 67 | +Copy and edit the environment file: |
| 68 | +```bash |
| 69 | +cp .env.example .env |
| 70 | +# Edit .env with your OpenFGA store details |
| 71 | +``` |
| 72 | + |
| 73 | +### 3. Choose Your Approach |
| 74 | + |
| 75 | +#### Option A: Manual Configuration (./gradlew run) |
| 76 | +```bash |
| 77 | +./gradlew run |
| 78 | +``` |
| 79 | + |
| 80 | +**Pros:** |
| 81 | +- Full control over OpenTelemetry configuration |
| 82 | +- Can customize metrics, exporters, and resources in code |
| 83 | +- No external dependencies beyond your application |
| 84 | + |
| 85 | +**Cons:** |
| 86 | +- Requires OpenTelemetry SDK dependencies in your application |
| 87 | +- More code to write and maintain |
| 88 | + |
| 89 | +#### Option B: Java Agent (./gradlew runWithAgent) |
| 90 | +```bash |
| 91 | +./gradlew runWithAgent |
| 92 | +``` |
| 93 | + |
| 94 | +**Pros:** |
| 95 | +- Zero code changes required - completely automatic |
| 96 | +- No OpenTelemetry dependencies needed in your application |
| 97 | +- Easy to enable/disable by adding/removing the agent |
| 98 | + |
| 99 | +**Cons:** |
| 100 | +- Less control over configuration |
| 101 | +- Requires downloading and managing the agent JAR |
| 102 | + |
| 103 | +## Viewing Metrics |
| 104 | + |
| 105 | +Both approaches export metrics to the same OTLP endpoint. View them in: |
| 106 | + |
| 107 | +- **Prometheus**: http://localhost:9090/graph |
| 108 | + - Query: `fga_client_request_duration_bucket` |
| 109 | + - Query: `fga_client_query_duration_bucket` |
| 110 | + - Query: `fga_client_credentials_request_total` |
| 111 | + |
| 112 | +- **Grafana**: http://localhost:3001 (admin:admin) |
| 113 | + - Import dashboard from `grafana/` directory |
| 114 | + - Or create custom dashboards with the FGA metrics |
| 115 | + |
| 116 | +## Architecture |
| 117 | + |
| 118 | +### Manual Configuration Mode |
| 119 | +``` |
| 120 | +Your App → OpenTelemetry SDK → OTLP Exporter → Collector → Prometheus/Jaeger |
| 121 | +``` |
| 122 | + |
| 123 | +The application code: |
| 124 | +1. Configures OpenTelemetry SDK with OTLP exporter |
| 125 | +2. Creates OpenFGA client with default telemetry enabled |
| 126 | +3. Performs FGA operations which generate metrics |
| 127 | +4. Metrics are exported to the OTLP collector |
| 128 | + |
| 129 | +### Java Agent Mode |
| 130 | +``` |
| 131 | +Your App → OpenTelemetry Agent → OTLP Exporter → Collector → Prometheus/Jaeger |
| 132 | +``` |
| 133 | + |
| 134 | +The OpenTelemetry agent: |
| 135 | +1. Automatically detects and instruments the OpenFGA SDK |
| 136 | +2. Configures exporters based on system properties |
| 137 | +3. Collects metrics without any code changes |
| 138 | +4. Exports to the same OTLP collector |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +### No Metrics Appearing |
| 143 | +1. Verify OTLP collector is running on localhost:4317 |
| 144 | +2. Check the application logs for OpenTelemetry initialization messages |
| 145 | +3. Ensure FGA operations are actually being performed |
| 146 | + |
| 147 | +### Manual Configuration Issues |
| 148 | +- Verify all OpenTelemetry dependencies are included |
| 149 | +- Check that `buildAndRegisterGlobal()` is called before creating the FGA client |
| 150 | + |
| 151 | +### Java Agent Issues |
| 152 | +- Verify the agent JAR was downloaded successfully |
| 153 | +- Check that OTEL system properties are set correctly |
| 154 | +- Ensure the agent is being loaded (look for agent startup messages) |
| 155 | + |
| 156 | +### Connection Issues |
| 157 | +- Verify your `.env` file has correct FGA_STORE_ID and FGA_MODEL_ID |
| 158 | +- Check that your OpenFGA server is accessible |
| 159 | +- Verify authentication credentials if using a protected OpenFGA instance |
| 160 | + |
| 161 | +## Observing Metrics |
| 162 | + |
| 163 | +### Prometheus (http://localhost:9090) |
| 164 | + |
| 165 | +Query for OpenFGA metrics: |
| 166 | +- `fga_client_request_duration_bucket` - Request duration histogram |
| 167 | +- `fga_client_query_duration_bucket` - Query duration histogram |
| 168 | +- `fga_client_credentials_request_total` - Credentials request counter |
| 169 | + |
| 170 | +Example queries: |
| 171 | +```promql |
| 172 | +# Average request duration by method |
| 173 | +rate(fga_client_request_duration_sum[5m]) / rate(fga_client_request_duration_count[5m]) |
| 174 | +
|
| 175 | +# Request rate by HTTP status code |
| 176 | +rate(fga_client_request_duration_count[5m]) |
| 177 | +
|
| 178 | +# 95th percentile request duration |
| 179 | +histogram_quantile(0.95, rate(fga_client_request_duration_bucket[5m])) |
| 180 | +``` |
| 181 | + |
| 182 | +### Grafana (http://localhost:3001) |
| 183 | + |
| 184 | +Login with `admin:admin`. The collector setup includes pre-configured dashboards for OpenFGA metrics. |
| 185 | + |
| 186 | +## Next Steps |
| 187 | + |
| 188 | +- Explore the metrics in Grafana with custom dashboards |
| 189 | +- Try different telemetry configurations to see what works best for your use case |
| 190 | +- Consider which approach (manual vs agent) fits better with your deployment strategy |
| 191 | + |
| 192 | +## Cleanup |
| 193 | + |
| 194 | +To stop the OpenTelemetry stack: |
| 195 | + |
| 196 | +```bash |
| 197 | +cd otel-collector |
| 198 | +docker-compose down |
| 199 | +``` |
| 200 | + |
| 201 | +## Learn More |
| 202 | + |
| 203 | +- [OpenFGA Documentation](https://openfga.dev/docs) |
| 204 | +- [OpenFGA Java SDK Documentation](../../README.md) |
| 205 | +- [OpenTelemetry Java Documentation](https://opentelemetry.io/docs/languages/java/) |
| 206 | +- [OpenFGA Telemetry Documentation](../../docs/OpenTelemetry.md) |
0 commit comments