Skip to content

Commit

Permalink
refactor(js-sdk): remove usage of patch file for handling telemetry a…
Browse files Browse the repository at this point in the history
…ttributes (#442)
  • Loading branch information
ewanharris authored Nov 4, 2024
2 parents 28121e9 + 984f427 commit f37bb5b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 139 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ build-client-js:
sed -i -e "s|_this|this|g" ${CLIENTS_OUTPUT_DIR}/fga-js-sdk/*.md
rm -rf ${CLIENTS_OUTPUT_DIR}/fga-js-sdk/*-e
make run-in-docker sdk_language=js image=node:${NODE_DOCKER_TAG} command="/bin/sh -c 'npm i --lockfile-version 2 && npm run lint:fix -- --quiet'"
make run-in-docker sdk_language=js image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'patch -p1 api.ts /config/clients/js/patches/add-method-specific-attributes.patch'"
make run-in-docker sdk_language=js image=node:${NODE_DOCKER_TAG} command="/bin/sh -c 'npm run lint:fix && npm run build;'"

### Go
Expand Down
138 changes: 0 additions & 138 deletions config/clients/js/patches/add-method-specific-attributes.patch

This file was deleted.

6 changes: 6 additions & 0 deletions config/clients/js/template/apiInner.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ export const {{classname}}Fp = function(configuration: Configuration, credential
const localVarAxiosArgs = localVarAxiosParamCreator.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options);
return createRequestFunction(localVarAxiosArgs, globalAxios, configuration, credentials, {
[TelemetryAttribute.FgaClientRequestMethod]: "{{operationIdCamelCase}}",
{{#pathParams.0}}
[TelemetryAttribute.FgaClientRequestStoreId]: storeId ?? "",
{{/pathParams.0}}
{{#bodyParam}}
...TelemetryAttributes.fromRequestBody(body)
{{/bodyParam}}
});
},
{{/operation}}
Expand Down
12 changes: 12 additions & 0 deletions config/clients/js/template/telemetry/attributes.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,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 f37bb5b

Please sign in to comment.