-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add opentelemetry example app (#130)
- Loading branch information
Showing
10 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-opentelemetry-example" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# OpenTelemetry usage with OpenFGA's JS SDK | ||
|
||
This example demonstrates how you can use OpenTelemetry with OpenFGA's JS SDK. | ||
|
||
## Prerequisites | ||
|
||
If you do not already have an OpenFGA instance running, you can start one using the following command: | ||
|
||
```bash | ||
docker run -d -p 8080:8080 openfga/openfga | ||
``` | ||
|
||
You need to have an OpenTelemetry collector running to receive data. A pre-configured collector is available using Docker: | ||
|
||
```bash | ||
git clone https://github.com/ewanharris/opentelemetry-collector-dev-setup.git | ||
cd opentelemetry-collector-dev-setup | ||
docker-compose up -d | ||
``` | ||
|
||
## Configure the example | ||
|
||
You need to configure the example for your environment: | ||
|
||
```bash | ||
cp .env.example .env | ||
``` | ||
|
||
Now edit the `.env` file and set the values as appropriate. | ||
|
||
## Running the example | ||
|
||
Begin by installing the required dependencies: | ||
|
||
```bash | ||
npm i | ||
``` | ||
|
||
Next, run the example: | ||
|
||
```bash | ||
npm start | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import "dotenv/config"; | ||
import { NodeSDK } from "@opentelemetry/sdk-node"; | ||
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; | ||
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto"; | ||
import process from "process"; | ||
|
||
const sdk = new NodeSDK({ | ||
metricReader: new PeriodicExportingMetricReader({ | ||
exportIntervalMillis: 5000, | ||
exporter: new OTLPMetricExporter({ | ||
concurrencyLimit: 1, | ||
}), | ||
}), | ||
}); | ||
sdk.start(); | ||
|
||
process.on("exit", () => { | ||
sdk | ||
.shutdown() | ||
.then( | ||
() => console.log("SDK shut down successfully"), | ||
(err) => console.log("Error shutting down SDK", err) | ||
) | ||
.finally(() => process.exit(0)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import "dotenv/config"; | ||
import { CredentialsMethod, FgaApiValidationError, OpenFgaClient } from "@openfga/sdk"; | ||
|
||
let credentials; | ||
if (process.env.FGA_CLIENT_ID) { | ||
credentials = { | ||
method: CredentialsMethod.ClientCredentials, | ||
config: { | ||
clientId: process.env.FGA_CLIENT_ID, | ||
clientSecret: process.env.FGA_CLIENT_SECRET, | ||
apiAudience: process.env.FGA_API_AUDIENCE, | ||
apiTokenIssuer: process.env.FGA_API_TOKEN_ISSUER | ||
} | ||
}; | ||
} | ||
|
||
const fgaClient = new OpenFgaClient({ | ||
apiUrl: process.env.FGA_API_URL, | ||
storeId: process.env.FGA_STORE_ID, | ||
authorizationModelId: process.env.FGA_MODEL_ID, | ||
credentials | ||
}); | ||
|
||
async function main () { | ||
|
||
setTimeout(async () => { | ||
try { | ||
await main(); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, 20000); | ||
|
||
console.log("Reading Authorization Models"); | ||
const { authorization_models } = await fgaClient.readAuthorizationModels(); | ||
console.log(`Models Count: ${authorization_models.length}`); | ||
|
||
console.log("Reading Tuples"); | ||
const { tuples } = await fgaClient.read(); | ||
console.log(`Tuples count: ${tuples.length}`); | ||
|
||
|
||
const checkRequests = Math.floor(Math.random() * 10); | ||
console.log(`Making ${checkRequests} checks`); | ||
for (let index = 0; index < checkRequests; index++) { | ||
console.log("Checking for access" + index); | ||
try { | ||
const { allowed } = await fgaClient.check({ | ||
user: "user:anne", | ||
relation: "owner", | ||
object: "folder:foo" | ||
}); | ||
console.log(`Allowed: ${allowed}`); | ||
} catch (error) { | ||
if (error instanceof FgaApiValidationError) { | ||
console.log(`Failed due to ${error.apiErrorMessage}`); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
console.log("writing tuple"); | ||
await fgaClient.write({ | ||
writes: [ | ||
{ | ||
user: "user:anne", | ||
relation: "owner", | ||
object: "folder:date-"+Date.now(), | ||
} | ||
] | ||
}); | ||
} | ||
|
||
|
||
main() | ||
.catch(error => { | ||
console.error(`error: ${error}`); | ||
process.exitCode = 1; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "openfga-opentelemetry-example", | ||
"private": "true", | ||
"version": "1.0.0", | ||
"description": "A bare bones example of using the OpenFGA SDK with OpenTelemetry metrics reporting", | ||
"author": "OpenFGA", | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"start": "node --import ./instrumentation.mjs opentelemetry.mjs" | ||
}, | ||
"dependencies": { | ||
"@openfga/sdk": "^0.6.1", | ||
"@opentelemetry/exporter-metrics-otlp-proto": "^0.52.1", | ||
"@opentelemetry/exporter-prometheus": "^0.52.1", | ||
"@opentelemetry/sdk-metrics": "^1.25.1", | ||
"@opentelemetry/sdk-node": "^0.52.1", | ||
"dotenv": "^16.4.5" | ||
}, | ||
"engines": { | ||
"node": ">=16.13.0" | ||
} | ||
} |