Skip to content

Commit

Permalink
add batch check size telemetry attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Dec 18, 2024
1 parent c9c1fb1 commit bdab51c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions example/opentelemetry/opentelemetry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const telemetryConfig = {
},
[TelemetryMetric.HistogramRequestDuration]: {
attributes: new Set([
TelemetryAttribute.FgaClientRequestBatchCheckSize,
TelemetryAttribute.HttpResponseStatusCode,
TelemetryAttribute.UserAgentOriginal,
TelemetryAttribute.FgaClientRequestMethod,
Expand Down Expand Up @@ -99,6 +100,34 @@ async function main () {
}
}

console.log("Calling BatcCheck")
const { responses } = await fgaClient.batchCheck({
checks: [
{
object: "doc:roadmap",
relation: "can_read",
user: "user:anne",
},
{
object: "doc:roadmap",
relation: "can_read",
user: "user:dan",
},
{
object: "doc:finances",
relation: "can_read",
user: "user:dan"
},
{
object: "doc:finances",
relation: "can_reads",
user: "user:anne",
}
]
}, {
authorizationModelId: "01JC6KPJ0CKSZ69C5Z26CYWX2N"
});

console.log("writing tuple");
await fgaClient.write({
writes: [
Expand Down
4 changes: 4 additions & 0 deletions telemetry/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { URL } from "url";

export enum TelemetryAttribute {
FgaClientRequestBatchCheckSize = "fga-client.request.batch_check_size",
FgaClientRequestClientId = "fga-client.request.client_id",
FgaClientRequestMethod = "fga-client.request.method",
FgaClientRequestModelId = "fga-client.request.model_id",
Expand Down Expand Up @@ -121,6 +122,9 @@ export class TelemetryAttributes {
attributes[TelemetryAttribute.FgaClientUser] = body.tuple_key.user;
}

if (body?.checks?.length) {
attributes[TelemetryAttribute.FgaClientRequestBatchCheckSize] = body.checks.length;
}
return attributes;
}
}
2 changes: 2 additions & 0 deletions telemetry/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class TelemetryConfiguration implements TelemetryConfig {

// This not included by default as it has a very high cardinality which could increase costs for users
// TelemetryAttribute.FgaClientUser
// TelemetryAttribute.FgaClientRequestBatchCheckSize
]);

/**
Expand All @@ -97,6 +98,7 @@ export class TelemetryConfiguration implements TelemetryConfig {
TelemetryAttribute.HttpClientRequestDuration,
TelemetryAttribute.HttpServerRequestDuration,
TelemetryAttribute.FgaClientUser,
TelemetryAttribute.FgaClientRequestBatchCheckSize,
]);

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/telemetry/attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,30 @@ describe("TelemetryAttributes", () => {
expect(attributes[TelemetryAttribute.FgaClientRequestModelId]).toEqual("model-id");
expect(attributes[TelemetryAttribute.FgaClientUser]).toBeUndefined();
});

test("should create attributes from a batchCheck request body correctly", () => {
const body = {
authorization_model_id: "model-id",
checks: [
{
tuple_key: {
user: "user:anne",
object: "doc:123",
relation: "can_view"
}
},
{
tuple_key: {
user: "user:anne",
object: "doc:789",
relation: "can_view"
}
}
]
};
const attributes = TelemetryAttributes.fromRequestBody(body);

expect(attributes[TelemetryAttribute.FgaClientRequestModelId]).toEqual("model-id");
expect(attributes[TelemetryAttribute.FgaClientRequestBatchCheckSize]).toEqual(2);
});
});

0 comments on commit bdab51c

Please sign in to comment.