Skip to content

Commit

Permalink
refactor: remove usage of patch file for handling telemetry attributes (
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Nov 4, 2024
2 parents 49a9053 + 1da00b5 commit 97eac20
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
40 changes: 20 additions & 20 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "Check",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
[TelemetryAttribute.FgaClientRequestModelId]: body.authorization_model_id ?? "",
[TelemetryAttribute.FgaClientUser]: body.tuple_key.user
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -776,6 +775,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.createStore(body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "CreateStore",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -789,7 +789,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.deleteStore(storeId, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "DeleteStore",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -804,8 +804,8 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.expand(storeId, body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "Expand",
[TelemetryAttribute.FgaClientRequestModelId]: body.authorization_model_id ?? "",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -819,7 +819,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.getStore(storeId, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "GetStore",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -835,12 +835,11 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ListObjects",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
[TelemetryAttribute.FgaClientRequestModelId]: body.authorization_model_id ?? "",
[TelemetryAttribute.FgaClientUser]: body.user
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
* Returns a paginated list of OpenFGA stores and a continuation token to get additional stores. The continuation token will be empty if there are no more stores.
* Returns a paginated list of OpenFGA stores and a continuation token to get additional stores. The continuation token will be empty if there are no more stores.
* @summary List all stores
* @param {number} [pageSize]
* @param {string} [continuationToken]
Expand All @@ -866,7 +865,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ListUsers",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
[TelemetryAttribute.FgaClientRequestModelId]: body.authorization_model_id ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -881,11 +880,12 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.read(storeId, body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "Read",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
* @summary Read assertions for an authorization model ID
* @param {string} storeId
* @param {string} authorizationModelId
Expand All @@ -896,8 +896,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.readAssertions(storeId, authorizationModelId, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ReadAssertions",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestModelId]: authorizationModelId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -912,7 +911,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.readAuthorizationModel(storeId, id, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ReadAuthorizationModel",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -928,7 +927,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.readAuthorizationModels(storeId, pageSize, continuationToken, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ReadAuthorizationModels",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -945,7 +944,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.readChanges(storeId, type, pageSize, continuationToken, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "ReadChanges",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
});
},
/**
Expand All @@ -961,7 +960,7 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "Write",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
[TelemetryAttribute.FgaClientRequestModelId]: body.authorization_model_id ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -977,8 +976,8 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.writeAssertions(storeId, authorizationModelId, body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "WriteAssertions",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestModelId]: authorizationModelId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
/**
Expand All @@ -993,7 +992,8 @@ export const OpenFgaApiFp = function(configuration: Configuration, credentials:
const localVarAxiosArgs = localVarAxiosParamCreator.writeAuthorizationModel(storeId, body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "WriteAuthorizationModel",
[TelemetryAttribute.FgaClientRequestStoreId]: storeId,
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
...TelemetryAttributes.fromRequestBody(body)
});
},
};
Expand Down
12 changes: 12 additions & 0 deletions telemetry/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ export class TelemetryAttributes {

return attributes;
}

static fromRequestBody(body: any, attributes: Record<string, string | number> = {}): Record<string, string | number> {
if (body?.authorization_model_id) {
attributes[TelemetryAttribute.FgaClientRequestModelId] = body.authorization_model_id;
}

if (body?.tuple_key?.user) {
attributes[TelemetryAttribute.FgaClientUser] = body.tuple_key.user;
}

return attributes;
}
}
16 changes: 16 additions & 0 deletions tests/telemetry/attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,20 @@ describe("TelemetryAttributes", () => {
expect(result["fga-client.response.model_id"]).toEqual("model-id");
expect(result["http.server.request.duration"]).toEqual(10);
});

test("should create attributes from a request body correctly", () => {
const body = { authorization_model_id: "model-id", tuple_key: { user: "user:anne" } };
const attributes = TelemetryAttributes.fromRequestBody(body);

expect(attributes[TelemetryAttribute.FgaClientRequestModelId]).toEqual("model-id");
expect(attributes[TelemetryAttribute.FgaClientUser]).toEqual("user:anne");
});

test("should create attributes from a request body without tuple_key", () => {
const body = { authorization_model_id: "model-id" };
const attributes = TelemetryAttributes.fromRequestBody(body);

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

0 comments on commit 97eac20

Please sign in to comment.